Skip to content

Commit e6f6175

Browse files
committed
15 / 036 : add GPU version of the Keras MNIST example for Colab
measure training time return np.array() instead of pd.DataFrame for keras sample data consistency set n_epoch = 1 if CI to save time Clean ipynb
1 parent 8aac35f commit e6f6175

File tree

1 file changed

+338
-0
lines changed

1 file changed

+338
-0
lines changed
Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
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/keras-example/15_optimization/036_colab_mnist_keras_example_GPU.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+
"* Help from Gemini for the acceleration code.\n",
23+
"\n"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"import os\n",
33+
"import pathlib\n",
34+
"\n",
35+
"import numpy as np\n",
36+
"import pandas as pd\n",
37+
"import tensorflow as tf\n",
38+
"\n"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": null,
44+
"metadata": {},
45+
"outputs": [],
46+
"source": [
47+
"physical_devices = tf.config.list_physical_devices('GPU')\n",
48+
"try:\n",
49+
" tf.config.experimental.set_memory_growth(physical_devices[0], True)\n",
50+
"except:\n",
51+
" # Invalid device or cannot modify virtual devices once initialized.\n",
52+
" print('not successful : tf.config.experimental.set_memory_growth()')\n",
53+
"else:\n",
54+
" print('Using GPU')\n",
55+
"\n"
56+
]
57+
},
58+
{
59+
"cell_type": "markdown",
60+
"metadata": {},
61+
"source": [
62+
"### Listing 2.1 Loading the MNIST dataset in Keras\n",
63+
"\n"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": null,
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"try:\n",
73+
" data_folder = pathlib.Path('sample_data')\n",
74+
" assert data_folder.exists()\n",
75+
" assert data_folder.is_dir()\n",
76+
"\n",
77+
" def read_data(data_path):\n",
78+
" df = pd.read_csv(\n",
79+
" data_path,\n",
80+
" header=None\n",
81+
" )\n",
82+
" labels = np.array(df.iloc[:, 0])\n",
83+
" images = np.array(df.iloc[:, 1:])\n",
84+
"\n",
85+
" return images, labels\n",
86+
"\n",
87+
"\n",
88+
" train_images, train_labels = read_data(data_folder / 'mnist_train_small.csv')\n",
89+
" test_images, test_labels = read_data(data_folder / 'mnist_test.csv')\n",
90+
"\n",
91+
"except AssertionError:\n",
92+
" (train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()\n",
93+
"\n"
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"### Listing 2.2 The training data\n"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": null,
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"train_images.shape\n",
110+
"\n"
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": null,
116+
"metadata": {},
117+
"outputs": [],
118+
"source": [
119+
"n_train = len(train_labels)\n",
120+
"n_train\n",
121+
"\n"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"metadata": {},
128+
"outputs": [],
129+
"source": [
130+
"train_labels\n",
131+
"\n"
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {},
137+
"source": [
138+
"### Listing 2.3 The test data\n"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": null,
144+
"metadata": {},
145+
"outputs": [],
146+
"source": [
147+
"test_images.shape\n",
148+
"\n"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": null,
154+
"metadata": {},
155+
"outputs": [],
156+
"source": [
157+
"n_test = len(test_labels)\n",
158+
"n_test\n",
159+
"\n"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": null,
165+
"metadata": {},
166+
"outputs": [],
167+
"source": [
168+
"test_labels\n",
169+
"\n"
170+
]
171+
},
172+
{
173+
"cell_type": "markdown",
174+
"metadata": {},
175+
"source": [
176+
"### Listing 2.4 The network architecture\n",
177+
"\n"
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"execution_count": null,
183+
"metadata": {},
184+
"outputs": [],
185+
"source": [
186+
"network = tf.keras.models.Sequential()\n",
187+
"network.add(tf.keras.layers.Input(shape=(28 * 28,))) # Define input shape using Input layer\n",
188+
"network.add(tf.keras.layers.Dense(512, activation='relu'))\n",
189+
"network.add(tf.keras.layers.Dense(10, activation='softmax'))\n",
190+
"\n"
191+
]
192+
},
193+
{
194+
"cell_type": "markdown",
195+
"metadata": {},
196+
"source": [
197+
"### 2.5 The compilation step\n",
198+
"\n"
199+
]
200+
},
201+
{
202+
"cell_type": "code",
203+
"execution_count": null,
204+
"metadata": {},
205+
"outputs": [],
206+
"source": [
207+
"network.compile(\n",
208+
" optimizer='rmsprop',\n",
209+
" loss='categorical_crossentropy',\n",
210+
" metrics=['accuracy']\n",
211+
")\n",
212+
"\n"
213+
]
214+
},
215+
{
216+
"cell_type": "markdown",
217+
"metadata": {},
218+
"source": [
219+
"### 2.6 Preparing the image data\n",
220+
"\n"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": null,
226+
"metadata": {},
227+
"outputs": [],
228+
"source": [
229+
"train_images = np.array(train_images).reshape((n_train, 28 * 28))\n",
230+
"train_images = np.array(train_images).astype('float32') / 255\n",
231+
"\n",
232+
"test_images = np.array(test_images).reshape((n_test, 28 * 28))\n",
233+
"test_images = np.array(test_images).astype('float32') / 255\n",
234+
"\n"
235+
]
236+
},
237+
{
238+
"cell_type": "markdown",
239+
"metadata": {},
240+
"source": [
241+
"### 2.7 Preparing the labels\n",
242+
"\n"
243+
]
244+
},
245+
{
246+
"cell_type": "code",
247+
"execution_count": null,
248+
"metadata": {},
249+
"outputs": [],
250+
"source": [
251+
"train_labels = tf.keras.utils.to_categorical(train_labels)\n",
252+
"test_labels = tf.keras.utils.to_categorical(test_labels)\n",
253+
"\n"
254+
]
255+
},
256+
{
257+
"cell_type": "markdown",
258+
"metadata": {},
259+
"source": [
260+
"### 2.8 Training the network\n",
261+
"\n"
262+
]
263+
},
264+
{
265+
"cell_type": "code",
266+
"execution_count": null,
267+
"metadata": {},
268+
"outputs": [],
269+
"source": [
270+
"n_epoch = 1 if os.getenv('GITHUB_ACTIONS', False) else 5\n",
271+
"\n"
272+
]
273+
},
274+
{
275+
"cell_type": "code",
276+
"execution_count": null,
277+
"metadata": {},
278+
"outputs": [],
279+
"source": [
280+
"%%time\n",
281+
"network.fit(train_images, train_labels, epochs=n_epoch, batch_size=128)\n",
282+
"\n"
283+
]
284+
},
285+
{
286+
"cell_type": "markdown",
287+
"metadata": {},
288+
"source": [
289+
"### 2.9 Evaluating the network\n",
290+
"\n"
291+
]
292+
},
293+
{
294+
"cell_type": "code",
295+
"execution_count": null,
296+
"metadata": {},
297+
"outputs": [],
298+
"source": [
299+
"test_loss, test_acc = network.evaluate(test_images, test_labels)\n",
300+
"print('test_acc:', test_acc)\n",
301+
"\n"
302+
]
303+
},
304+
{
305+
"cell_type": "code",
306+
"execution_count": null,
307+
"metadata": {},
308+
"outputs": [],
309+
"source": []
310+
}
311+
],
312+
"metadata": {
313+
"accelerator": "GPU",
314+
"colab": {
315+
"gpuType": "T4",
316+
"provenance": []
317+
},
318+
"kernelspec": {
319+
"display_name": "Python 3 (ipykernel)",
320+
"language": "python",
321+
"name": "python3"
322+
},
323+
"language_info": {
324+
"codemirror_mode": {
325+
"name": "ipython",
326+
"version": 3
327+
},
328+
"file_extension": ".py",
329+
"mimetype": "text/x-python",
330+
"name": "python",
331+
"nbconvert_exporter": "python",
332+
"pygments_lexer": "ipython3",
333+
"version": "3.10.0"
334+
}
335+
},
336+
"nbformat": 4,
337+
"nbformat_minor": 4
338+
}

0 commit comments

Comments
 (0)