Browse Source

Refactor train and save for TF1.0

Signed-off-by: Norman Heckscher <norman.heckscher@gmail.com>
Norman Heckscher 8 years ago
parent
commit
6a26f9794e
2 changed files with 31 additions and 33 deletions
  1. 3 3
      examples/4_Utils/save_restore_model.py
  2. 28 30
      notebooks/4_Utils/save_restore_model.ipynb

+ 3 - 3
examples/4_Utils/save_restore_model.py

@@ -11,7 +11,7 @@ from __future__ import print_function
 
 # Import MNIST data
 from tensorflow.examples.tutorials.mnist import input_data
-mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
+mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
 
 import tensorflow as tf
 
@@ -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()
 
 # 'Saver' op to save and restore all the variables
 saver = tf.train.Saver()

+ 28 - 30
notebooks/4_Utils/save_restore_model.ipynb

@@ -29,26 +29,26 @@
      "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"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": 2,
+   "execution_count": 3,
    "metadata": {
-    "collapsed": true
+    "collapsed": false
    },
    "outputs": [],
    "source": [
@@ -97,16 +97,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": 3,
+   "execution_count": 4,
    "metadata": {
     "collapsed": true
    },
@@ -118,7 +118,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
+   "execution_count": 5,
    "metadata": {
     "collapsed": false
    },
@@ -128,11 +128,11 @@
      "output_type": "stream",
      "text": [
       "Starting 1st session...\n",
-      "Epoch: 0001 cost= 182.770135574\n",
-      "Epoch: 0002 cost= 44.863718596\n",
-      "Epoch: 0003 cost= 27.965412349\n",
+      "Epoch: 0001 cost= 187.778896380\n",
+      "Epoch: 0002 cost= 42.367902536\n",
+      "Epoch: 0003 cost= 26.488964058\n",
       "First Optimization Finished!\n",
-      "Accuracy: 0.906\n",
+      "Accuracy: 0.9075\n",
       "Model saved in file: /tmp/model.ckpt\n"
      ]
     }
@@ -175,7 +175,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": 6,
    "metadata": {
     "collapsed": false
    },
@@ -186,15 +186,15 @@
      "text": [
       "Starting 2nd session...\n",
       "Model restored from file: /tmp/model.ckpt\n",
-      "Epoch: 0001 cost= 19.658836002\n",
-      "Epoch: 0002 cost= 14.354811554\n",
-      "Epoch: 0003 cost= 10.580801367\n",
-      "Epoch: 0004 cost= 8.012172253\n",
-      "Epoch: 0005 cost= 5.985675981\n",
-      "Epoch: 0006 cost= 4.572637980\n",
-      "Epoch: 0007 cost= 3.329074899\n",
+      "Epoch: 0001 cost= 18.292712951\n",
+      "Epoch: 0002 cost= 13.404136196\n",
+      "Epoch: 0003 cost= 9.855191723\n",
+      "Epoch: 0004 cost= 7.276933088\n",
+      "Epoch: 0005 cost= 5.564581285\n",
+      "Epoch: 0006 cost= 4.165259939\n",
+      "Epoch: 0007 cost= 3.139393926\n",
       "Second Optimization Finished!\n",
-      "Accuracy: 0.9371\n"
+      "Accuracy: 0.9385\n"
      ]
     }
    ],
@@ -242,9 +242,7 @@
     "collapsed": true
    },
    "outputs": [],
-   "source": [
-    ""
-   ]
+   "source": []
   }
  ],
  "metadata": {
@@ -256,16 +254,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
-}
+}