| 
 | 1 | +{  | 
 | 2 | + "cells": [  | 
 | 3 | +  {  | 
 | 4 | +   "cell_type": "code",  | 
 | 5 | +   "execution_count": null,  | 
 | 6 | +   "metadata": {  | 
 | 7 | +    "collapsed": true  | 
 | 8 | +   },  | 
 | 9 | +   "outputs": [],  | 
 | 10 | +   "source": [  | 
 | 11 | +    "import tensorflow as tf\n",  | 
 | 12 | +    "import numpy as np"  | 
 | 13 | +   ]  | 
 | 14 | +  },  | 
 | 15 | +  {  | 
 | 16 | +   "cell_type": "code",  | 
 | 17 | +   "execution_count": null,  | 
 | 18 | +   "metadata": {  | 
 | 19 | +    "collapsed": true  | 
 | 20 | +   },  | 
 | 21 | +   "outputs": [],  | 
 | 22 | +   "source": [  | 
 | 23 | +    "# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3\n",  | 
 | 24 | +    "x_data = np.random.rand(100).astype(np.float32)\n",  | 
 | 25 | +    "y_data = x_data * 0.1 + 0.3"  | 
 | 26 | +   ]  | 
 | 27 | +  },  | 
 | 28 | +  {  | 
 | 29 | +   "cell_type": "code",  | 
 | 30 | +   "execution_count": null,  | 
 | 31 | +   "metadata": {  | 
 | 32 | +    "collapsed": true  | 
 | 33 | +   },  | 
 | 34 | +   "outputs": [],  | 
 | 35 | +   "source": [  | 
 | 36 | +    "# Try to find values for W and b that compute y_data = W * x_data + b\n",  | 
 | 37 | +    "# (We know that W should be 0.1 and b 0.3, but TensorFlow will\n",  | 
 | 38 | +    "# figure that out for us.)\n",  | 
 | 39 | +    "W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))\n",  | 
 | 40 | +    "b = tf.Variable(tf.zeros([1]))\n",  | 
 | 41 | +    "y = W * x_data + b"  | 
 | 42 | +   ]  | 
 | 43 | +  },  | 
 | 44 | +  {  | 
 | 45 | +   "cell_type": "code",  | 
 | 46 | +   "execution_count": null,  | 
 | 47 | +   "metadata": {  | 
 | 48 | +    "collapsed": true  | 
 | 49 | +   },  | 
 | 50 | +   "outputs": [],  | 
 | 51 | +   "source": [  | 
 | 52 | +    "# Minimize the mean squared errors.\n",  | 
 | 53 | +    "loss = tf.reduce_mean(tf.square(y - y_data))\n",  | 
 | 54 | +    "optimizer = tf.train.GradientDescentOptimizer(0.5)\n",  | 
 | 55 | +    "train = optimizer.minimize(loss)"  | 
 | 56 | +   ]  | 
 | 57 | +  },  | 
 | 58 | +  {  | 
 | 59 | +   "cell_type": "code",  | 
 | 60 | +   "execution_count": null,  | 
 | 61 | +   "metadata": {  | 
 | 62 | +    "collapsed": true  | 
 | 63 | +   },  | 
 | 64 | +   "outputs": [],  | 
 | 65 | +   "source": [  | 
 | 66 | +    "# Before starting, initialize the variables.  We will 'run' this first.\n",  | 
 | 67 | +    "init = tf.initialize_all_variables()"  | 
 | 68 | +   ]  | 
 | 69 | +  },  | 
 | 70 | +  {  | 
 | 71 | +   "cell_type": "code",  | 
 | 72 | +   "execution_count": null,  | 
 | 73 | +   "metadata": {  | 
 | 74 | +    "collapsed": true  | 
 | 75 | +   },  | 
 | 76 | +   "outputs": [],  | 
 | 77 | +   "source": [  | 
 | 78 | +    "# Launch the graph.\n",  | 
 | 79 | +    "sess = tf.Session()\n",  | 
 | 80 | +    "sess.run(init)"  | 
 | 81 | +   ]  | 
 | 82 | +  },  | 
 | 83 | +  {  | 
 | 84 | +   "cell_type": "code",  | 
 | 85 | +   "execution_count": null,  | 
 | 86 | +   "metadata": {  | 
 | 87 | +    "collapsed": false  | 
 | 88 | +   },  | 
 | 89 | +   "outputs": [],  | 
 | 90 | +   "source": [  | 
 | 91 | +    "# Fit the line.\n",  | 
 | 92 | +    "for step in range(201):\n",  | 
 | 93 | +    "    sess.run(train)\n",  | 
 | 94 | +    "    if step % 20 == 0:\n",  | 
 | 95 | +    "        print(step, sess.run(W), sess.run(b))"  | 
 | 96 | +   ]  | 
 | 97 | +  }  | 
 | 98 | + ],  | 
 | 99 | + "metadata": {  | 
 | 100 | +  "kernelspec": {  | 
 | 101 | +   "display_name": "Python 2",  | 
 | 102 | +   "language": "python",  | 
 | 103 | +   "name": "python2"  | 
 | 104 | +  },  | 
 | 105 | +  "language_info": {  | 
 | 106 | +   "codemirror_mode": {  | 
 | 107 | +    "name": "ipython",  | 
 | 108 | +    "version": 2  | 
 | 109 | +   },  | 
 | 110 | +   "file_extension": ".py",  | 
 | 111 | +   "mimetype": "text/x-python",  | 
 | 112 | +   "name": "python",  | 
 | 113 | +   "nbconvert_exporter": "python",  | 
 | 114 | +   "pygments_lexer": "ipython2",  | 
 | 115 | +   "version": "2.7.10"  | 
 | 116 | +  }  | 
 | 117 | + },  | 
 | 118 | + "nbformat": 4,  | 
 | 119 | + "nbformat_minor": 1  | 
 | 120 | +}  | 
0 commit comments