Skip to content

Commit 0cf1015

Browse files
committed
MNIST
1 parent dde31b9 commit 0cf1015

5 files changed

+351
-0
lines changed

Diff for: 23.Tensorflow Basics.ipynb

+351
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,357 @@
151151
"source": [
152152
"sess.close()"
153153
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": 10,
158+
"metadata": {},
159+
"outputs": [],
160+
"source": [
161+
"import numpy as np"
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": 11,
167+
"metadata": {},
168+
"outputs": [],
169+
"source": [
170+
"X_1 = tf.placeholder(tf.float32, name = \"X_1\")"
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": 12,
176+
"metadata": {},
177+
"outputs": [],
178+
"source": [
179+
"X_2 = tf.placeholder(tf.float32, name = \"X_2\")"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": 13,
185+
"metadata": {},
186+
"outputs": [],
187+
"source": [
188+
"multiply = tf.multiply(X_1, X_2, name = \"multiply\")"
189+
]
190+
},
191+
{
192+
"cell_type": "code",
193+
"execution_count": 14,
194+
"metadata": {},
195+
"outputs": [
196+
{
197+
"name": "stdout",
198+
"output_type": "stream",
199+
"text": [
200+
"[ 4. 10. 18.]\n"
201+
]
202+
}
203+
],
204+
"source": [
205+
"with tf.Session() as session:\n",
206+
" result = session.run(multiply, feed_dict={X_1:[1,2,3], X_2:[4,5,6]})\n",
207+
" print(result)"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": 15,
213+
"metadata": {},
214+
"outputs": [
215+
{
216+
"name": "stdout",
217+
"output_type": "stream",
218+
"text": [
219+
"[ 5 12 21 32]\n"
220+
]
221+
}
222+
],
223+
"source": [
224+
"# Import `tensorflow`\n",
225+
"import tensorflow as tf\n",
226+
"\n",
227+
"# Initialize two constants\n",
228+
"x1 = tf.constant([1,2,3,4])\n",
229+
"x2 = tf.constant([5,6,7,8])\n",
230+
"\n",
231+
"# Multiply\n",
232+
"result = tf.multiply(x1, x2)\n",
233+
"\n",
234+
"# Initialize Session and run `result`\n",
235+
"with tf.Session() as sess:\n",
236+
" output = sess.run(result)\n",
237+
" print(output)"
238+
]
239+
},
240+
{
241+
"cell_type": "code",
242+
"execution_count": 16,
243+
"metadata": {},
244+
"outputs": [],
245+
"source": [
246+
"matrix1 = np.array([(2,2,2),(2,2,2),(2,2,2)],dtype = 'int32')\n",
247+
"matrix2 = np.array([(1,1,1),(1,1,1),(1,1,1)],dtype = 'int32')"
248+
]
249+
},
250+
{
251+
"cell_type": "code",
252+
"execution_count": 17,
253+
"metadata": {},
254+
"outputs": [
255+
{
256+
"data": {
257+
"text/plain": [
258+
"array([[2, 2, 2],\n",
259+
" [2, 2, 2],\n",
260+
" [2, 2, 2]], dtype=int32)"
261+
]
262+
},
263+
"execution_count": 17,
264+
"metadata": {},
265+
"output_type": "execute_result"
266+
}
267+
],
268+
"source": [
269+
"matrix1"
270+
]
271+
},
272+
{
273+
"cell_type": "code",
274+
"execution_count": 18,
275+
"metadata": {},
276+
"outputs": [
277+
{
278+
"data": {
279+
"text/plain": [
280+
"array([[1, 1, 1],\n",
281+
" [1, 1, 1],\n",
282+
" [1, 1, 1]], dtype=int32)"
283+
]
284+
},
285+
"execution_count": 18,
286+
"metadata": {},
287+
"output_type": "execute_result"
288+
}
289+
],
290+
"source": [
291+
"matrix2"
292+
]
293+
},
294+
{
295+
"cell_type": "code",
296+
"execution_count": 19,
297+
"metadata": {},
298+
"outputs": [],
299+
"source": [
300+
"matrix1 = tf.constant(matrix1)\n",
301+
"matrix2 = tf.constant(matrix2)"
302+
]
303+
},
304+
{
305+
"cell_type": "code",
306+
"execution_count": 20,
307+
"metadata": {},
308+
"outputs": [],
309+
"source": [
310+
"matrix_product = tf.matmul(matrix1, matrix2)"
311+
]
312+
},
313+
{
314+
"cell_type": "code",
315+
"execution_count": 21,
316+
"metadata": {},
317+
"outputs": [],
318+
"source": [
319+
"matrix_sum = tf.add(matrix1,matrix2)"
320+
]
321+
},
322+
{
323+
"cell_type": "code",
324+
"execution_count": 22,
325+
"metadata": {},
326+
"outputs": [],
327+
"source": [
328+
"matrix_3 = np.array([(2,7,2),(1,4,2),(9,0,2)],dtype = 'float32')"
329+
]
330+
},
331+
{
332+
"cell_type": "code",
333+
"execution_count": 23,
334+
"metadata": {},
335+
"outputs": [
336+
{
337+
"name": "stdout",
338+
"output_type": "stream",
339+
"text": [
340+
"[[2. 7. 2.]\n",
341+
" [1. 4. 2.]\n",
342+
" [9. 0. 2.]]\n"
343+
]
344+
}
345+
],
346+
"source": [
347+
"print(matrix_3)"
348+
]
349+
},
350+
{
351+
"cell_type": "code",
352+
"execution_count": 24,
353+
"metadata": {},
354+
"outputs": [],
355+
"source": [
356+
"matrix_det = tf.matrix_determinant(matrix_3)"
357+
]
358+
},
359+
{
360+
"cell_type": "code",
361+
"execution_count": 25,
362+
"metadata": {},
363+
"outputs": [],
364+
"source": [
365+
"with tf.Session() as sess:\n",
366+
" result1 = sess.run(matrix_product)\n",
367+
" result2 = sess.run(matrix_sum)\n",
368+
" result3 = sess.run(matrix_det)"
369+
]
370+
},
371+
{
372+
"cell_type": "code",
373+
"execution_count": 26,
374+
"metadata": {},
375+
"outputs": [
376+
{
377+
"name": "stdout",
378+
"output_type": "stream",
379+
"text": [
380+
"[[6 6 6]\n",
381+
" [6 6 6]\n",
382+
" [6 6 6]]\n"
383+
]
384+
}
385+
],
386+
"source": [
387+
"print (result1)"
388+
]
389+
},
390+
{
391+
"cell_type": "code",
392+
"execution_count": 27,
393+
"metadata": {},
394+
"outputs": [
395+
{
396+
"name": "stdout",
397+
"output_type": "stream",
398+
"text": [
399+
"[[3 3 3]\n",
400+
" [3 3 3]\n",
401+
" [3 3 3]]\n"
402+
]
403+
}
404+
],
405+
"source": [
406+
"print (result2)"
407+
]
408+
},
409+
{
410+
"cell_type": "code",
411+
"execution_count": 28,
412+
"metadata": {},
413+
"outputs": [
414+
{
415+
"name": "stdout",
416+
"output_type": "stream",
417+
"text": [
418+
"55.999992\n"
419+
]
420+
}
421+
],
422+
"source": [
423+
"print (result3)"
424+
]
425+
},
426+
{
427+
"cell_type": "code",
428+
"execution_count": 29,
429+
"metadata": {},
430+
"outputs": [],
431+
"source": [
432+
"import tensorflow as tf\n",
433+
"import numpy as np\n",
434+
"from tensorflow.examples.tutorials.mnist import input_data"
435+
]
436+
},
437+
{
438+
"cell_type": "code",
439+
"execution_count": 30,
440+
"metadata": {},
441+
"outputs": [],
442+
"source": [
443+
"def run_cnn():\n",
444+
" mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot = True)\n",
445+
" learning_rate = 0.0001\n",
446+
" epochs = 10\n",
447+
" batch_size = 50"
448+
]
449+
},
450+
{
451+
"cell_type": "code",
452+
"execution_count": 31,
453+
"metadata": {},
454+
"outputs": [],
455+
"source": [
456+
"x = tf.placeholder(tf.float32, [None, 784])"
457+
]
458+
},
459+
{
460+
"cell_type": "code",
461+
"execution_count": 32,
462+
"metadata": {},
463+
"outputs": [],
464+
"source": [
465+
"x_shaped = tf.reshape(x, [-1, 28, 28, 1])"
466+
]
467+
},
468+
{
469+
"cell_type": "code",
470+
"execution_count": 33,
471+
"metadata": {},
472+
"outputs": [],
473+
"source": [
474+
"y = tf.placeholder(tf.float32, [None, 10])"
475+
]
476+
},
477+
{
478+
"cell_type": "code",
479+
"execution_count": 34,
480+
"metadata": {},
481+
"outputs": [],
482+
"source": [
483+
"x = tf.constant(-2.0, name=\"i\", dtype=tf.float32)\n",
484+
"a = tf.constant(5.0, name=\"j\", dtype=tf.float32)\n",
485+
"b = tf.constant(13.0, name=\"k\", dtype=tf.float32)"
486+
]
487+
},
488+
{
489+
"cell_type": "code",
490+
"execution_count": 35,
491+
"metadata": {},
492+
"outputs": [
493+
{
494+
"name": "stdout",
495+
"output_type": "stream",
496+
"text": [
497+
"-2.0\n"
498+
]
499+
}
500+
],
501+
"source": [
502+
"sess = tf.Session()\n",
503+
"print(sess.run(x))"
504+
]
154505
}
155506
],
156507
"metadata": {

Diff for: MNIST_data/t10k-images-idx3-ubyte.gz

1.57 MB
Binary file not shown.

Diff for: MNIST_data/t10k-labels-idx1-ubyte.gz

4.44 KB
Binary file not shown.

Diff for: MNIST_data/train-images-idx3-ubyte.gz

9.45 MB
Binary file not shown.

Diff for: MNIST_data/train-labels-idx1-ubyte.gz

28.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)