1+ {
2+ "cells" : [
3+ {
4+ "cell_type" : " markdown" ,
5+ "metadata" : {
6+ "id" : " view-in-github" ,
7+ "colab_type" : " text"
8+ },
9+ "source" : [
10+ " <a href=\" https://colab.research.google.com/github/kangwonlee/nmisp/blob/main/15_optimization/035_colab_mnist_keras_example.ipynb\" target=\" _parent\" ><img src=\" https://colab.research.google.com/assets/colab-badge.svg\" alt=\" Open In Colab\" /></a>\n "
11+ ]
12+ },
13+ {
14+ "cell_type" : " markdown" ,
15+ "metadata" : {},
16+ "source" : [
17+ " ## 2.1 A first look at a neural network\n " ,
18+ " \n " ,
19+ " * from : F. Chollet, Deep Learning with Python, ISBN 9781617294433, 2017\n " ,
20+ " * https://github.com/fchollet/deep-learning-with-python-notebooks\n " ,
21+ " * https://www.manning.com/books/deep-learning-with-python\n " ,
22+ " \n "
23+ ]
24+ },
25+ {
26+ "cell_type" : " code" ,
27+ "execution_count" : null ,
28+ "metadata" : {},
29+ "outputs" : [],
30+ "source" : [
31+ " import os\n " ,
32+ " import pathlib\n " ,
33+ " \n " ,
34+ " import numpy as np\n " ,
35+ " import pandas as pd\n " ,
36+ " import tensorflow as tf\n " ,
37+ " \n "
38+ ]
39+ },
40+ {
41+ "cell_type" : " markdown" ,
42+ "metadata" : {},
43+ "source" : [
44+ " ### Listing 2.1 Loading the MNIST dataset in Keras\n " ,
45+ " \n "
46+ ]
47+ },
48+ {
49+ "cell_type" : " code" ,
50+ "execution_count" : null ,
51+ "metadata" : {},
52+ "outputs" : [],
53+ "source" : [
54+ " try:\n " ,
55+ " data_folder = pathlib.Path('sample_data')\n " ,
56+ " assert data_folder.exists()\n " ,
57+ " assert data_folder.is_dir()\n " ,
58+ " \n " ,
59+ " def read_data(data_path):\n " ,
60+ " df = pd.read_csv(\n " ,
61+ " data_path,\n " ,
62+ " header=None\n " ,
63+ " )\n " ,
64+ " labels = np.array(df.iloc[:, 0])\n " ,
65+ " images = np.array(df.iloc[:, 1:])\n " ,
66+ " \n " ,
67+ " return images, labels\n " ,
68+ " \n " ,
69+ " \n " ,
70+ " train_images, train_labels = read_data(data_folder / 'mnist_train_small.csv')\n " ,
71+ " test_images, test_labels = read_data(data_folder / 'mnist_test.csv')\n " ,
72+ " \n " ,
73+ " except AssertionError:\n " ,
74+ " (train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()\n " ,
75+ " \n "
76+ ]
77+ },
78+ {
79+ "cell_type" : " markdown" ,
80+ "metadata" : {},
81+ "source" : [
82+ " ### Listing 2.2 The training data\n "
83+ ]
84+ },
85+ {
86+ "cell_type" : " code" ,
87+ "execution_count" : null ,
88+ "metadata" : {},
89+ "outputs" : [],
90+ "source" : [
91+ " train_images.shape\n " ,
92+ " \n "
93+ ]
94+ },
95+ {
96+ "cell_type" : " code" ,
97+ "execution_count" : null ,
98+ "metadata" : {},
99+ "outputs" : [],
100+ "source" : [
101+ " n_train = len(train_labels)\n " ,
102+ " \n "
103+ ]
104+ },
105+ {
106+ "cell_type" : " code" ,
107+ "execution_count" : null ,
108+ "metadata" : {},
109+ "outputs" : [],
110+ "source" : [
111+ " train_labels\n " ,
112+ " \n "
113+ ]
114+ },
115+ {
116+ "cell_type" : " markdown" ,
117+ "metadata" : {},
118+ "source" : [
119+ " ### Listing 2.3 The test data\n "
120+ ]
121+ },
122+ {
123+ "cell_type" : " code" ,
124+ "execution_count" : null ,
125+ "metadata" : {},
126+ "outputs" : [],
127+ "source" : [
128+ " test_images.shape\n "
129+ ]
130+ },
131+ {
132+ "cell_type" : " code" ,
133+ "execution_count" : null ,
134+ "metadata" : {},
135+ "outputs" : [],
136+ "source" : [
137+ " n_test = len(test_labels)\n " ,
138+ " \n "
139+ ]
140+ },
141+ {
142+ "cell_type" : " code" ,
143+ "execution_count" : null ,
144+ "metadata" : {},
145+ "outputs" : [],
146+ "source" : [
147+ " test_labels\n "
148+ ]
149+ },
150+ {
151+ "cell_type" : " markdown" ,
152+ "metadata" : {},
153+ "source" : [
154+ " ### Listing 2.4 The network architecture\n " ,
155+ " \n "
156+ ]
157+ },
158+ {
159+ "cell_type" : " code" ,
160+ "execution_count" : null ,
161+ "metadata" : {},
162+ "outputs" : [],
163+ "source" : [
164+ " import keras\n " ,
165+ " \n " ,
166+ " network = keras.models.Sequential()\n " ,
167+ " network.add(keras.layers.Input(shape=(28 * 28,))) # Define input shape using Input layer\n " ,
168+ " network.add(keras.layers.Dense(512, activation='relu'))\n " ,
169+ " network.add(keras.layers.Dense(10, activation='softmax'))\n " ,
170+ " \n "
171+ ]
172+ },
173+ {
174+ "cell_type" : " markdown" ,
175+ "metadata" : {},
176+ "source" : [
177+ " ### 2.5 The compilation step\n " ,
178+ " \n "
179+ ]
180+ },
181+ {
182+ "cell_type" : " code" ,
183+ "execution_count" : null ,
184+ "metadata" : {},
185+ "outputs" : [],
186+ "source" : [
187+ " network.compile(\n " ,
188+ " optimizer='rmsprop',\n " ,
189+ " loss='categorical_crossentropy',\n " ,
190+ " metrics=['accuracy']\n " ,
191+ " )\n " ,
192+ " \n "
193+ ]
194+ },
195+ {
196+ "cell_type" : " markdown" ,
197+ "metadata" : {},
198+ "source" : [
199+ " ### 2.6 Preparing the image data\n " ,
200+ " \n "
201+ ]
202+ },
203+ {
204+ "cell_type" : " code" ,
205+ "execution_count" : null ,
206+ "metadata" : {},
207+ "outputs" : [],
208+ "source" : [
209+ " train_images = np.array(train_images).reshape((n_train, 28 * 28))\n " ,
210+ " train_images = np.array(train_images).astype('float32') / 255\n " ,
211+ " \n " ,
212+ " test_images = np.array(test_images).reshape((n_test, 28 * 28))\n " ,
213+ " test_images = np.array(test_images).astype('float32') / 255\n " ,
214+ " \n "
215+ ]
216+ },
217+ {
218+ "cell_type" : " markdown" ,
219+ "metadata" : {},
220+ "source" : [
221+ " ### 2.7 Preparing the labels\n " ,
222+ " \n "
223+ ]
224+ },
225+ {
226+ "cell_type" : " code" ,
227+ "execution_count" : null ,
228+ "metadata" : {},
229+ "outputs" : [],
230+ "source" : [
231+ " train_labels = keras.utils.to_categorical(train_labels)\n " ,
232+ " test_labels = keras.utils.to_categorical(test_labels)\n " ,
233+ " \n "
234+ ]
235+ },
236+ {
237+ "cell_type" : " markdown" ,
238+ "metadata" : {},
239+ "source" : [
240+ " ### 2.8 Training the network\n " ,
241+ " \n "
242+ ]
243+ },
244+ {
245+ "cell_type" : " code" ,
246+ "execution_count" : null ,
247+ "metadata" : {},
248+ "outputs" : [],
249+ "source" : [
250+ " n_epoch = 1 if os.getenv('GITHUB_ACTIONS', False) else 5\n " ,
251+ " \n "
252+ ]
253+ },
254+ {
255+ "cell_type" : " code" ,
256+ "execution_count" : null ,
257+ "metadata" : {},
258+ "outputs" : [],
259+ "source" : [
260+ " %%time\n " ,
261+ " network.fit(train_images, train_labels, epochs=n_epoch, batch_size=128)\n " ,
262+ " \n "
263+ ]
264+ },
265+ {
266+ "cell_type" : " markdown" ,
267+ "metadata" : {},
268+ "source" : [
269+ " ### 2.9 Evaluating the network\n " ,
270+ " \n "
271+ ]
272+ },
273+ {
274+ "cell_type" : " code" ,
275+ "execution_count" : null ,
276+ "metadata" : {},
277+ "outputs" : [],
278+ "source" : [
279+ " test_loss, test_acc = network.evaluate(test_images, test_labels)\n " ,
280+ " print('test_acc:', test_acc)\n " ,
281+ " \n "
282+ ]
283+ },
284+ {
285+ "cell_type" : " code" ,
286+ "execution_count" : null ,
287+ "metadata" : {},
288+ "outputs" : [],
289+ "source" : []
290+ }
291+ ],
292+ "metadata" : {
293+ "colab" : {
294+ "provenance" : []
295+ },
296+ "kernelspec" : {
297+ "display_name" : " Python 3 (ipykernel)" ,
298+ "language" : " python" ,
299+ "name" : " python3"
300+ },
301+ "language_info" : {
302+ "codemirror_mode" : {
303+ "name" : " ipython" ,
304+ "version" : 3
305+ },
306+ "file_extension" : " .py" ,
307+ "mimetype" : " text/x-python" ,
308+ "name" : " python" ,
309+ "nbconvert_exporter" : " python" ,
310+ "pygments_lexer" : " ipython3" ,
311+ "version" : " 3.10.0"
312+ }
313+ },
314+ "nbformat" : 4 ,
315+ "nbformat_minor" : 4
316+ }
0 commit comments