{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import tensorflow as tf" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# 一行两列\n", "matrix1 = tf.constant([[1, 0]])\n", "# 两行一列\n", "matrix2 = tf.constant([[1], [0]])\n", "# 矩阵相乘\n", "matmul = tf.matmul(matrix1, matrix2)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Tensor(\"MatMul:0\", shape=(1, 1), dtype=int32)\n" ] } ], "source": [ "# 打印结果 (Tensor而不是结果 2)\n", "print(matmul)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1]]\n" ] } ], "source": [ "# 定义会话\n", "sess = tf.Session()\n", "# 真正开始执行\n", "print(sess.run(matmul))\n", "sess.close()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1]]\n" ] } ], "source": [ "# with ... as ...的写法，可以保证 session会自动关闭\n", "with tf.Session() as sess:\n", " print(sess.run(matmul))" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.3" } }, "nbformat": 4, "nbformat_minor": 1 }