{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import tensorflow as tf" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# 创建变量\n", "a = tf.Variable([1, 0])\n", "b = tf.Variable([0, 1])\n", "# sub/add 两个 operation\n", "sub = tf.subtract(a, b)\n", "add = tf.add(a, b)\n", "\n", "# 初始化变量\n", "init = tf.global_variables_initializer()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1 -1]\n", "[1 1]\n" ] } ], "source": [ "# 执行 Session\n", "with tf.Session() as sess:\n", " sess.run(init)\n", " print(sess.run(sub))\n", " print(sess.run(add))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n" ] } ], "source": [ "# Counter\n", "counter = tf.Variable(0, name=\"counter\")\n", "add_one = tf.add(counter, 1)\n", "assign = tf.assign(counter, add_one)\n", "init = tf.global_variables_initializer()\n", "\n", "with tf.Session() as sess:\n", " sess.run(init)\n", " for _ in range(5):\n", " sess.run(assign)\n", " print(sess.run(counter))" ] } ], "metadata": { "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": 2 }