{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Use shift-enter to execute a code block and move to the next one." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# 1.1 Import tensorflow and other libraries.\n", "import tensorflow as tf\n", "import numpy as np\n", "\n", "%matplotlib inline\n", "import pylab" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 1.2 Create input data using NumPy. y = x * 0.1 + 0.3 + noise\n", "x_data = np.random.rand(100).astype(np.float32)\n", "noise = np.random.normal(scale=0.01, size=len(x_data))\n", "y_data = x_data * 0.1 + 0.3 + noise\n", "\n", "# Uncomment the following line to plot our input data.\n", "# pylab.plot(x_data, y_data, '.')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 1.3 Buld inference graph.\n", "# Create Variables W and b that compute y_data = W * x_data + b\n", "W = tf.Variable(tf.random_uniform([1], 0.0, 1.0))\n", "b = tf.Variable(tf.zeros([1]))\n", "y = W * x_data + b\n", "\n", "# Uncomment the following lines to see W and b are.\n", "# print(W)\n", "# print(b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "# 1.4 Build training graph.\n", "loss = tf.reduce_mean(tf.square(y - y_data)) # Create an operation that calculates loss.\n", "optimizer = tf.train.GradientDescentOptimizer(0.5) # Create an optimizer.\n", "train = optimizer.minimize(loss) # Create an operation that minimizes loss.\n", "init = tf.initialize_all_variables() # Create an operation initializes all the variables.\n", "\n", "# Uncomment the following 3 lines to see what 'loss', 'optimizer' and 'train' are.\n", "# print(\"loss:\", loss)\n", "# print(\"optimizer:\", optimizer)\n", "# print(\"train:\", train)\n", "# print(init)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 1.5 Uncomment the following line to see what we have built.\n", "# print(tf.get_default_graph().as_graph_def())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 1.6 Create a session and launch the graph.\n", "sess = tf.Session()\n", "sess.run(init)\n", "y_initial_values = sess.run(y) # Save initial values for plotting later.\n", "\n", "# Uncomment the following line to see the initial W and b values.\n", "# print(sess.run([W, b]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 1.7 Perform training.\n", "for step in range(201):\n", " sess.run(train)\n", " # Uncomment the following two lines to watch training happen real time.\n", " # if step % 20 == 0:\n", " # print(step, sess.run([W, b]))\n", "\n", "print(sess.run([W, b]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# 1.8 Uncomment the following lines to compare.\n", "# pylab.plot(x_data, y_data, '.', label=\"target_values\")\n", "# pylab.plot(x_data, y_initial_values, \".\", label=\"initial_values\")\n", "# pylab.plot(x_data, sess.run(y), \".\", label=\"trained_values\")\n", "# pylab.legend()\n", "# pylab.ylim(0, 1.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Back to [0_tf_hello_world.ipynb](0_tf_hello_world.ipynb).

\n", "

Next to [2_mnist.ipynb](2_mnist.ipynb).

" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 1 }