Browse Source

Refactor multilayer_perceptron for TF1.0

Signed-off-by: Norman Heckscher <norman.heckscher@gmail.com>
Norman Heckscher 8 years ago
parent
commit
90f22bc4eb

+ 2 - 2
examples/3_NeuralNetworks/multilayer_perceptron.py

@@ -60,11 +60,11 @@ biases = {
 pred = multilayer_perceptron(x, weights, biases)
 
 # Define loss and optimizer
-cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
+cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
 optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
 
 # Initializing the variables
-init = tf.initialize_all_variables()
+init = tf.global_variables_initializer()
 
 # Launch the graph
 with tf.Session() as sess:

+ 38 - 29
notebooks/3_NeuralNetworks/multilayer_perceptron.ipynb

@@ -29,17 +29,17 @@
      "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"
+      "Extracting MNIST_data/train-images-idx3-ubyte.gz\n",
+      "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n",
+      "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n",
+      "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n"
      ]
     }
    ],
    "source": [
     "# Import MINST data\n",
     "from tensorflow.examples.tutorials.mnist import input_data\n",
-    "mnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=True)\n",
+    "mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)\n",
     "\n",
     "import tensorflow as tf"
    ]
@@ -92,9 +92,9 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
+   "execution_count": 5,
    "metadata": {
-    "collapsed": true
+    "collapsed": false
    },
    "outputs": [],
    "source": [
@@ -114,16 +114,16 @@
     "pred = multilayer_perceptron(x, weights, biases)\n",
     "\n",
     "# Define loss and optimizer\n",
-    "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))\n",
+    "cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))\n",
     "optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)\n",
     "\n",
     "# Initializing the variables\n",
-    "init = tf.initialize_all_variables()"
+    "init = tf.global_variables_initializer()"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": 6,
    "metadata": {
     "collapsed": false
    },
@@ -132,23 +132,23 @@
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "Epoch: 0001 cost= 185.342230390\n",
-      "Epoch: 0002 cost= 44.266946572\n",
-      "Epoch: 0003 cost= 27.999560453\n",
-      "Epoch: 0004 cost= 19.655567043\n",
-      "Epoch: 0005 cost= 14.284429696\n",
-      "Epoch: 0006 cost= 10.640310403\n",
-      "Epoch: 0007 cost= 7.904047886\n",
-      "Epoch: 0008 cost= 5.989115090\n",
-      "Epoch: 0009 cost= 4.689374613\n",
-      "Epoch: 0010 cost= 3.455884229\n",
-      "Epoch: 0011 cost= 2.733002625\n",
-      "Epoch: 0012 cost= 2.101091420\n",
-      "Epoch: 0013 cost= 1.496508092\n",
-      "Epoch: 0014 cost= 1.245452015\n",
-      "Epoch: 0015 cost= 0.912072906\n",
+      "Epoch: 0001 cost= 173.056566575\n",
+      "Epoch: 0002 cost= 44.054413928\n",
+      "Epoch: 0003 cost= 27.455470655\n",
+      "Epoch: 0004 cost= 19.008652363\n",
+      "Epoch: 0005 cost= 13.654873594\n",
+      "Epoch: 0006 cost= 10.059267435\n",
+      "Epoch: 0007 cost= 7.436018432\n",
+      "Epoch: 0008 cost= 5.587794416\n",
+      "Epoch: 0009 cost= 4.209882509\n",
+      "Epoch: 0010 cost= 3.203879515\n",
+      "Epoch: 0011 cost= 2.319920681\n",
+      "Epoch: 0012 cost= 1.676204545\n",
+      "Epoch: 0013 cost= 1.248805338\n",
+      "Epoch: 0014 cost= 1.052676844\n",
+      "Epoch: 0015 cost= 0.890117338\n",
       "Optimization Finished!\n",
-      "Accuracy: 0.9422\n"
+      "Accuracy: 0.9459\n"
      ]
     }
    ],
@@ -181,6 +181,15 @@
     "    accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\n",
     "    print \"Accuracy:\", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})"
    ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": []
   }
  ],
  "metadata": {
@@ -192,16 +201,16 @@
   "language_info": {
    "codemirror_mode": {
     "name": "ipython",
-    "version": 2.0
+    "version": 2
    },
    "file_extension": ".py",
    "mimetype": "text/x-python",
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython2",
-   "version": "2.7.11"
+   "version": "2.7.13"
   }
  },
  "nbformat": 4,
  "nbformat_minor": 0
-}
+}