{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# A linear regression learning algorithm example using TensorFlow library.\n", "\n", "# Author: Aymeric Damien\n", "# Project: https://github.com/aymericdamien/TensorFlow-Examples/" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import tensorflow as tf\n", "import numpy\n", "import matplotlib.pyplot as plt\n", "rng = numpy.random" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Parameters\n", "learning_rate = 0.01\n", "training_epochs = 1000\n", "display_step = 50" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Training Data\n", "train_X = numpy.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,\n", " 7.042,10.791,5.313,7.997,5.654,9.27,3.1])\n", "train_Y = numpy.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,\n", " 2.827,3.465,1.65,2.904,2.42,2.94,1.3])\n", "n_samples = train_X.shape[0]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# tf Graph Input\n", "X = tf.placeholder(\"float\")\n", "Y = tf.placeholder(\"float\")\n", "\n", "# Set model weights\n", "W = tf.Variable(rng.randn(), name=\"weight\")\n", "b = tf.Variable(rng.randn(), name=\"bias\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Construct a linear model\n", "pred = tf.add(tf.mul(X, W), b)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Mean squared error\n", "cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)\n", "# Gradient descent\n", "optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Initializing the variables\n", "init = tf.initialize_all_variables()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch: 0050 cost= 0.207037717 W= 0.451217 b= -0.649001\n", "Epoch: 0100 cost= 0.192011863 W= 0.439226 b= -0.562735\n", "Epoch: 0150 cost= 0.178721249 W= 0.427948 b= -0.481599\n", "Epoch: 0200 cost= 0.166965470 W= 0.41734 b= -0.40529\n", "Epoch: 0250 cost= 0.156567261 W= 0.407363 b= -0.333518\n", "Epoch: 0300 cost= 0.147369981 W= 0.39798 b= -0.266015\n", "Epoch: 0350 cost= 0.139234960 W= 0.389155 b= -0.202527\n", "Epoch: 0400 cost= 0.132039562 W= 0.380854 b= -0.142815\n", "Epoch: 0450 cost= 0.125675321 W= 0.373048 b= -0.0866538\n", "Epoch: 0500 cost= 0.120046206 W= 0.365705 b= -0.0338331\n", "Epoch: 0550 cost= 0.115067400 W= 0.3588 b= 0.0158462\n", "Epoch: 0600 cost= 0.110663772 W= 0.352305 b= 0.0625707\n", "Epoch: 0650 cost= 0.106768914 W= 0.346196 b= 0.106516\n", "Epoch: 0700 cost= 0.103324078 W= 0.340451 b= 0.147848\n", "Epoch: 0750 cost= 0.100277305 W= 0.335047 b= 0.186722\n", "Epoch: 0800 cost= 0.097582638 W= 0.329965 b= 0.223284\n", "Epoch: 0850 cost= 0.095199391 W= 0.325184 b= 0.257671\n", "Epoch: 0900 cost= 0.093091547 W= 0.320689 b= 0.290013\n", "Epoch: 0950 cost= 0.091227390 W= 0.31646 b= 0.320432\n", "Epoch: 1000 cost= 0.089578770 W= 0.312484 b= 0.349041\n", "Optimization Finished!\n", "Training cost= 0.0895788 W= 0.312484 b= 0.349041 \n", "\n" ] } ], "source": [ "# Launch the graph\n", "with tf.Session() as sess:\n", " sess.run(init)\n", "\n", " # Fit all training data\n", " for epoch in range(training_epochs):\n", " for (x, y) in zip(train_X, train_Y):\n", " sess.run(optimizer, feed_dict={X: x, Y: y})\n", "\n", " #Display logs per epoch step\n", " if (epoch+1) % display_step == 0:\n", " c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})\n", " print \"Epoch:\", '%04d' % (epoch+1), \"cost=\", \"{:.9f}\".format(c), \\\n", " \"W=\", sess.run(W), \"b=\", sess.run(b)\n", "\n", " print \"Optimization Finished!\"\n", " training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})\n", " print \"Training cost=\", training_cost, \"W=\", sess.run(W), \"b=\", sess.run(b), '\\n'\n", "\n", " #Graphic display\n", " plt.plot(train_X, train_Y, 'ro', label='Original data')\n", " plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')\n", " plt.legend()\n", " plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2.0 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }