|
21 | 21 | { |
22 | 22 | "cell_type": "code", |
23 | 23 | "execution_count": 1, |
24 | | - "metadata": { |
25 | | - "collapsed": false |
26 | | - }, |
| 24 | + "metadata": {}, |
27 | 25 | "outputs": [ |
28 | 26 | { |
29 | 27 | "name": "stdout", |
|
37 | 35 | } |
38 | 36 | ], |
39 | 37 | "source": [ |
| 38 | + "from __future__ import print_function\n", |
| 39 | + "\n", |
40 | 40 | "# Import MINST data\n", |
41 | 41 | "from tensorflow.examples.tutorials.mnist import input_data\n", |
42 | 42 | "mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)\n", |
|
47 | 47 | { |
48 | 48 | "cell_type": "code", |
49 | 49 | "execution_count": 3, |
50 | | - "metadata": { |
51 | | - "collapsed": false |
52 | | - }, |
| 50 | + "metadata": {}, |
53 | 51 | "outputs": [], |
54 | 52 | "source": [ |
55 | 53 | "# Parameters\n", |
|
119 | 117 | { |
120 | 118 | "cell_type": "code", |
121 | 119 | "execution_count": 5, |
122 | | - "metadata": { |
123 | | - "collapsed": false |
124 | | - }, |
| 120 | + "metadata": {}, |
125 | 121 | "outputs": [ |
126 | 122 | { |
127 | 123 | "name": "stdout", |
|
139 | 135 | ], |
140 | 136 | "source": [ |
141 | 137 | "# Running first session\n", |
142 | | - "print \"Starting 1st session...\"\n", |
| 138 | + "print(\"Starting 1st session...\")\n", |
143 | 139 | "with tf.Session() as sess:\n", |
144 | 140 | " # Initialize variables\n", |
145 | 141 | " sess.run(init)\n", |
|
160 | 156 | " if epoch % display_step == 0:\n", |
161 | 157 | " print \"Epoch:\", '%04d' % (epoch+1), \"cost=\", \\\n", |
162 | 158 | " \"{:.9f}\".format(avg_cost)\n", |
163 | | - " print \"First Optimization Finished!\"\n", |
| 159 | + " print(\"First Optimization Finished!\")\n", |
164 | 160 | "\n", |
165 | 161 | " # Test model\n", |
166 | 162 | " correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))\n", |
167 | 163 | " # Calculate accuracy\n", |
168 | 164 | " accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\n", |
169 | | - " print \"Accuracy:\", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})\n", |
| 165 | + " print(\"Accuracy:\", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))\n", |
170 | 166 | "\n", |
171 | 167 | " # Save model weights to disk\n", |
172 | 168 | " save_path = saver.save(sess, model_path)\n", |
173 | | - " print \"Model saved in file: %s\" % save_path" |
| 169 | + " print(\"Model saved in file: %s\" % save_path)" |
174 | 170 | ] |
175 | 171 | }, |
176 | 172 | { |
177 | 173 | "cell_type": "code", |
178 | 174 | "execution_count": 6, |
179 | | - "metadata": { |
180 | | - "collapsed": false |
181 | | - }, |
| 175 | + "metadata": {}, |
182 | 176 | "outputs": [ |
183 | 177 | { |
184 | 178 | "name": "stdout", |
|
200 | 194 | ], |
201 | 195 | "source": [ |
202 | 196 | "# Running a new session\n", |
203 | | - "print \"Starting 2nd session...\"\n", |
| 197 | + "print(\"Starting 2nd session...\")\n", |
204 | 198 | "with tf.Session() as sess:\n", |
205 | 199 | " # Initialize variables\n", |
206 | 200 | " sess.run(init)\n", |
207 | 201 | "\n", |
208 | 202 | " # Restore model weights from previously saved model\n", |
209 | 203 | " load_path = saver.restore(sess, model_path)\n", |
210 | | - " print \"Model restored from file: %s\" % save_path\n", |
| 204 | + " print(\"Model restored from file: %s\" % save_path)\n", |
211 | 205 | "\n", |
212 | 206 | " # Resume training\n", |
213 | 207 | " for epoch in range(7):\n", |
|
223 | 217 | " avg_cost += c / total_batch\n", |
224 | 218 | " # Display logs per epoch step\n", |
225 | 219 | " if epoch % display_step == 0:\n", |
226 | | - " print \"Epoch:\", '%04d' % (epoch + 1), \"cost=\", \\\n", |
227 | | - " \"{:.9f}\".format(avg_cost)\n", |
228 | | - " print \"Second Optimization Finished!\"\n", |
| 220 | + " print(\"Epoch:\", '%04d' % (epoch + 1), \"cost=\", \\\n", |
| 221 | + " \"{:.9f}\".format(avg_cost))\n", |
| 222 | + " print(\"Second Optimization Finished!\")\n", |
229 | 223 | "\n", |
230 | 224 | " # Test model\n", |
231 | 225 | " correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))\n", |
232 | 226 | " # Calculate accuracy\n", |
233 | 227 | " accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))\n", |
234 | | - " print \"Accuracy:\", accuracy.eval(\n", |
235 | | - " {x: mnist.test.images, y: mnist.test.labels})" |
| 228 | + " print(\"Accuracy:\", accuracy.eval(\n", |
| 229 | + " {x: mnist.test.images, y: mnist.test.labels}))" |
236 | 230 | ] |
237 | 231 | }, |
238 | 232 | { |
|
247 | 241 | ], |
248 | 242 | "metadata": { |
249 | 243 | "kernelspec": { |
250 | | - "display_name": "Python 2", |
| 244 | + "display_name": "Python 3", |
251 | 245 | "language": "python", |
252 | | - "name": "python2" |
| 246 | + "name": "python3" |
253 | 247 | }, |
254 | 248 | "language_info": { |
255 | 249 | "codemirror_mode": { |
256 | 250 | "name": "ipython", |
257 | | - "version": 2 |
| 251 | + "version": 3 |
258 | 252 | }, |
259 | 253 | "file_extension": ".py", |
260 | 254 | "mimetype": "text/x-python", |
261 | 255 | "name": "python", |
262 | 256 | "nbconvert_exporter": "python", |
263 | | - "pygments_lexer": "ipython2", |
264 | | - "version": "2.7.13" |
| 257 | + "pygments_lexer": "ipython3", |
| 258 | + "version": "3.5.3" |
265 | 259 | } |
266 | 260 | }, |
267 | 261 | "nbformat": 4, |
268 | | - "nbformat_minor": 0 |
| 262 | + "nbformat_minor": 1 |
269 | 263 | } |
0 commit comments