From 47c1483f46851c58e9057f1cc2653a86af8e5b06 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Mon, 28 Dec 2015 07:53:25 -0500 Subject: [PATCH] Add TensorFlow logistic regression notebook. --- README.md | 1 + .../logistic_regression.ipynb | 227 ++++++++++++++++++ 2 files changed, 228 insertions(+) create mode 100644 deep-learning/tensor-flow-examples/notebooks/2_basic_classifiers/logistic_regression.ipynb diff --git a/README.md b/README.md index f9cfa46..55e6836 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ IPython Notebook(s) demonstrating deep learning functionality. |--------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [tsf-basics](http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/deep-learning/tensor-flow-examples/1_intro/basic_operations.ipynb) | Learn basic operations in TensorFlow, a library for various kinds of perceptual and language understanding tasks from Google. | | [tsf-linear](http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/deep-learning/tensor-flow-examples/2_basic_classifiers/linear_regression.ipynb) | Implement linear regression in TensorFlow. | +| [tsf-logistic](http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/deep-learning/tensor-flow-examples/2_basic_classifiers/logistic_regression.ipynb) | Implement logistic regression in TensorFlow. | ### tensor-flow-exercises diff --git a/deep-learning/tensor-flow-examples/notebooks/2_basic_classifiers/logistic_regression.ipynb b/deep-learning/tensor-flow-examples/notebooks/2_basic_classifiers/logistic_regression.ipynb new file mode 100644 index 0000000..d269b97 --- /dev/null +++ b/deep-learning/tensor-flow-examples/notebooks/2_basic_classifiers/logistic_regression.ipynb @@ -0,0 +1,227 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Logistic Regression in TensorFlow\n", + "\n", + "Credits: Forked from [TensorFlow-Examples](https://github.com/aymericdamien/TensorFlow-Examples) by Aymeric Damien\n", + "\n", + "## Setup\n", + "\n", + "Refer to the [setup instructions](http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/deep-learning/tensor-flow-examples/Setup_TensorFlow.md)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Extracting /tmp/data/train-images-idx3-ubyte.gz\n", + "Extracting /tmp/data/train-labels-idx1-ubyte.gz\n", + "Extracting /tmp/data/t10k-images-idx3-ubyte.gz\n", + "Extracting /tmp/data/t10k-labels-idx1-ubyte.gz\n" + ] + } + ], + "source": [ + "# Import MINST data\n", + "import input_data\n", + "mnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import tensorflow as tf" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Parameters\n", + "learning_rate = 0.01\n", + "training_epochs = 25\n", + "batch_size = 100\n", + "display_step = 1" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# tf Graph Input\n", + "x = tf.placeholder(\"float\", [None, 784]) # mnist data image of shape 28*28=784\n", + "y = tf.placeholder(\"float\", [None, 10]) # 0-9 digits recognition => 10 classes" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Create model\n", + "\n", + "# Set model weights\n", + "W = tf.Variable(tf.zeros([784, 10]))\n", + "b = tf.Variable(tf.zeros([10]))" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Construct model\n", + "activation = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Minimize error using cross entropy\n", + "# Cross entropy\n", + "cost = -tf.reduce_sum(y*tf.log(activation)) \n", + "# Gradient Descent\n", + "optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Initializing the variables\n", + "init = tf.initialize_all_variables()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch: 0001 cost= 29.860479714\n", + "Epoch: 0002 cost= 22.080549484\n", + "Epoch: 0003 cost= 21.237104595\n", + "Epoch: 0004 cost= 20.460196280\n", + "Epoch: 0005 cost= 20.185128237\n", + "Epoch: 0006 cost= 19.940297202\n", + "Epoch: 0007 cost= 19.645111119\n", + "Epoch: 0008 cost= 19.507218031\n", + "Epoch: 0009 cost= 19.389794492\n", + "Epoch: 0010 cost= 19.177005816\n", + "Epoch: 0011 cost= 19.082493615\n", + "Epoch: 0012 cost= 19.072873598\n", + "Epoch: 0013 cost= 18.938005402\n", + "Epoch: 0014 cost= 18.891806430\n", + "Epoch: 0015 cost= 18.839480221\n", + "Epoch: 0016 cost= 18.769349510\n", + "Epoch: 0017 cost= 18.590865587\n", + "Epoch: 0018 cost= 18.623413677\n", + "Epoch: 0019 cost= 18.546149085\n", + "Epoch: 0020 cost= 18.432274895\n", + "Epoch: 0021 cost= 18.358189004\n", + "Epoch: 0022 cost= 18.380014628\n", + "Epoch: 0023 cost= 18.499993471\n", + "Epoch: 0024 cost= 18.386477311\n", + "Epoch: 0025 cost= 18.258080609\n", + "Optimization Finished!\n", + "Accuracy: 0.9048\n" + ] + } + ], + "source": [ + "# Launch the graph\n", + "with tf.Session() as sess:\n", + " sess.run(init)\n", + "\n", + " # Training cycle\n", + " for epoch in range(training_epochs):\n", + " avg_cost = 0.\n", + " total_batch = int(mnist.train.num_examples/batch_size)\n", + " # Loop over all batches\n", + " for i in range(total_batch):\n", + " batch_xs, batch_ys = mnist.train.next_batch(batch_size)\n", + " # Fit training using batch data\n", + " sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})\n", + " # Compute average loss\n", + " avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})/total_batch\n", + " # Display logs per epoch step\n", + " if epoch % display_step == 0:\n", + " print \"Epoch:\", '%04d' % (epoch+1), \"cost=\", \"{:.9f}\".format(avg_cost)\n", + "\n", + " print \"Optimization Finished!\"\n", + "\n", + " # Test model\n", + " correct_prediction = tf.equal(tf.argmax(activation, 1), tf.argmax(y, 1))\n", + " # Calculate accuracy\n", + " accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\n", + " print \"Accuracy:\", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})" + ] + } + ], + "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.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}