Skip to content

Commit 6a26f97

Browse files
Refactor train and save for TF1.0
Signed-off-by: Norman Heckscher <norman.heckscher@gmail.com>
1 parent 2c43cfb commit 6a26f97

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

examples/4_Utils/save_restore_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
# Import MNIST data
1313
from tensorflow.examples.tutorials.mnist import input_data
14-
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
14+
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
1515

1616
import tensorflow as tf
1717

@@ -60,11 +60,11 @@ def multilayer_perceptron(x, weights, biases):
6060
pred = multilayer_perceptron(x, weights, biases)
6161

6262
# Define loss and optimizer
63-
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
63+
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
6464
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
6565

6666
# Initializing the variables
67-
init = tf.initialize_all_variables()
67+
init = tf.global_variables_initializer()
6868

6969
# 'Saver' op to save and restore all the variables
7070
saver = tf.train.Saver()

notebooks/4_Utils/save_restore_model.ipynb

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@
2929
"name": "stdout",
3030
"output_type": "stream",
3131
"text": [
32-
"Extracting /tmp/data/train-images-idx3-ubyte.gz\n",
33-
"Extracting /tmp/data/train-labels-idx1-ubyte.gz\n",
34-
"Extracting /tmp/data/t10k-images-idx3-ubyte.gz\n",
35-
"Extracting /tmp/data/t10k-labels-idx1-ubyte.gz\n"
32+
"Extracting MNIST_data/train-images-idx3-ubyte.gz\n",
33+
"Extracting MNIST_data/train-labels-idx1-ubyte.gz\n",
34+
"Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n",
35+
"Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n"
3636
]
3737
}
3838
],
3939
"source": [
4040
"# Import MINST data\n",
4141
"from tensorflow.examples.tutorials.mnist import input_data\n",
42-
"mnist = input_data.read_data_sets(\"/tmp/data/\", one_hot=True)\n",
42+
"mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)\n",
4343
"\n",
4444
"import tensorflow as tf"
4545
]
4646
},
4747
{
4848
"cell_type": "code",
49-
"execution_count": 2,
49+
"execution_count": 3,
5050
"metadata": {
51-
"collapsed": true
51+
"collapsed": false
5252
},
5353
"outputs": [],
5454
"source": [
@@ -97,16 +97,16 @@
9797
"pred = multilayer_perceptron(x, weights, biases)\n",
9898
"\n",
9999
"# Define loss and optimizer\n",
100-
"cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))\n",
100+
"cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))\n",
101101
"optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)\n",
102102
"\n",
103103
"# Initializing the variables\n",
104-
"init = tf.initialize_all_variables()"
104+
"init = tf.global_variables_initializer()"
105105
]
106106
},
107107
{
108108
"cell_type": "code",
109-
"execution_count": 3,
109+
"execution_count": 4,
110110
"metadata": {
111111
"collapsed": true
112112
},
@@ -118,7 +118,7 @@
118118
},
119119
{
120120
"cell_type": "code",
121-
"execution_count": 4,
121+
"execution_count": 5,
122122
"metadata": {
123123
"collapsed": false
124124
},
@@ -128,11 +128,11 @@
128128
"output_type": "stream",
129129
"text": [
130130
"Starting 1st session...\n",
131-
"Epoch: 0001 cost= 182.770135574\n",
132-
"Epoch: 0002 cost= 44.863718596\n",
133-
"Epoch: 0003 cost= 27.965412349\n",
131+
"Epoch: 0001 cost= 187.778896380\n",
132+
"Epoch: 0002 cost= 42.367902536\n",
133+
"Epoch: 0003 cost= 26.488964058\n",
134134
"First Optimization Finished!\n",
135-
"Accuracy: 0.906\n",
135+
"Accuracy: 0.9075\n",
136136
"Model saved in file: /tmp/model.ckpt\n"
137137
]
138138
}
@@ -175,7 +175,7 @@
175175
},
176176
{
177177
"cell_type": "code",
178-
"execution_count": 5,
178+
"execution_count": 6,
179179
"metadata": {
180180
"collapsed": false
181181
},
@@ -186,15 +186,15 @@
186186
"text": [
187187
"Starting 2nd session...\n",
188188
"Model restored from file: /tmp/model.ckpt\n",
189-
"Epoch: 0001 cost= 19.658836002\n",
190-
"Epoch: 0002 cost= 14.354811554\n",
191-
"Epoch: 0003 cost= 10.580801367\n",
192-
"Epoch: 0004 cost= 8.012172253\n",
193-
"Epoch: 0005 cost= 5.985675981\n",
194-
"Epoch: 0006 cost= 4.572637980\n",
195-
"Epoch: 0007 cost= 3.329074899\n",
189+
"Epoch: 0001 cost= 18.292712951\n",
190+
"Epoch: 0002 cost= 13.404136196\n",
191+
"Epoch: 0003 cost= 9.855191723\n",
192+
"Epoch: 0004 cost= 7.276933088\n",
193+
"Epoch: 0005 cost= 5.564581285\n",
194+
"Epoch: 0006 cost= 4.165259939\n",
195+
"Epoch: 0007 cost= 3.139393926\n",
196196
"Second Optimization Finished!\n",
197-
"Accuracy: 0.9371\n"
197+
"Accuracy: 0.9385\n"
198198
]
199199
}
200200
],
@@ -242,9 +242,7 @@
242242
"collapsed": true
243243
},
244244
"outputs": [],
245-
"source": [
246-
""
247-
]
245+
"source": []
248246
}
249247
],
250248
"metadata": {
@@ -256,16 +254,16 @@
256254
"language_info": {
257255
"codemirror_mode": {
258256
"name": "ipython",
259-
"version": 2.0
257+
"version": 2
260258
},
261259
"file_extension": ".py",
262260
"mimetype": "text/x-python",
263261
"name": "python",
264262
"nbconvert_exporter": "python",
265263
"pygments_lexer": "ipython2",
266-
"version": "2.7.11"
264+
"version": "2.7.13"
267265
}
268266
},
269267
"nbformat": 4,
270268
"nbformat_minor": 0
271-
}
269+
}

0 commit comments

Comments
 (0)