yasirfaizahmed commited on
Commit
1e3d4ab
1 Parent(s): 4020c78
Files changed (1) hide show
  1. notes/mnist.ipynb +316 -28
notes/mnist.ipynb CHANGED
@@ -2,29 +2,176 @@
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
- "execution_count": 14,
6
  "id": "b2d6f096-6123-4dd0-ae4f-c9abf70889c4",
7
  "metadata": {
8
  "execution": {
9
- "iopub.execute_input": "2024-04-03T17:21:13.044450Z",
10
- "iopub.status.busy": "2024-04-03T17:21:13.043912Z",
11
- "iopub.status.idle": "2024-04-03T17:21:13.374087Z",
12
- "shell.execute_reply": "2024-04-03T17:21:13.373277Z",
13
- "shell.execute_reply.started": "2024-04-03T17:21:13.044419Z"
14
  }
15
  },
16
  "outputs": [
17
  {
18
- "name": "stderr",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  "output_type": "stream",
20
  "text": [
21
- "Reusing dataset mnist (/root/.cache/huggingface/datasets/mnist/mnist/1.0.0/fda16c03c4ecfb13f165ba7e29cf38129ce035011519968cdaf74894ce91c9d4)\n"
22
  ]
23
  },
24
  {
25
  "data": {
26
  "application/vnd.jupyter.widget-view+json": {
27
- "model_id": "cd2dd3c8b22b49718b3c66585ca3559c",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  "version_major": 2,
29
  "version_minor": 0
30
  },
@@ -44,15 +191,15 @@
44
  },
45
  {
46
  "cell_type": "code",
47
- "execution_count": 16,
48
  "id": "7c5ea43d-6a70-4deb-a90e-1d7758c961a3",
49
  "metadata": {
50
  "execution": {
51
- "iopub.execute_input": "2024-04-03T17:21:27.628082Z",
52
- "iopub.status.busy": "2024-04-03T17:21:27.627048Z",
53
- "iopub.status.idle": "2024-04-03T17:21:27.632687Z",
54
- "shell.execute_reply": "2024-04-03T17:21:27.632002Z",
55
- "shell.execute_reply.started": "2024-04-03T17:21:27.628051Z"
56
  }
57
  },
58
  "outputs": [],
@@ -60,40 +207,181 @@
60
  "import numpy as np\n",
61
  "from PIL import Image\n",
62
  "import io\n",
 
63
  "\n",
64
- "def convert(byte_like_str_image):\n",
65
- " return np.asarray(Image.open(io.BytesIO(byte_like_str_image['bytes'])))\n"
66
  ]
67
  },
68
  {
69
  "cell_type": "code",
70
- "execution_count": 18,
71
  "id": "daafde17-8100-4f49-b27e-8aad43b129c6",
72
  "metadata": {
73
  "execution": {
74
- "iopub.execute_input": "2024-04-03T17:22:20.343090Z",
75
- "iopub.status.busy": "2024-04-03T17:22:20.341876Z",
76
- "iopub.status.idle": "2024-04-03T17:22:27.888815Z",
77
- "shell.execute_reply": "2024-04-03T17:22:27.887812Z",
78
- "shell.execute_reply.started": "2024-04-03T17:22:20.343047Z"
79
  }
80
  },
81
  "outputs": [],
82
  "source": [
 
 
83
  "dataset_train = dataset['train'].to_pandas()\n",
84
- "dataset_train['image'] = dataset_train['image'].map(convert)\n",
85
  "\n",
86
  "dataset_test = dataset['test'].to_pandas()\n",
87
- "dataset_test['image'] = dataset_test['image'].map(convert)\n"
 
 
 
 
 
 
 
 
 
 
88
  ]
89
  },
90
  {
91
  "cell_type": "code",
92
- "execution_count": null,
93
  "id": "72022fd2-000d-4d5c-88d5-9afc62c283d5",
94
- "metadata": {},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  "outputs": [],
96
- "source": []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  }
98
  ],
99
  "metadata": {
 
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
+ "execution_count": 1,
6
  "id": "b2d6f096-6123-4dd0-ae4f-c9abf70889c4",
7
  "metadata": {
8
  "execution": {
9
+ "iopub.execute_input": "2024-04-04T08:47:31.680497Z",
10
+ "iopub.status.busy": "2024-04-04T08:47:31.678982Z",
11
+ "iopub.status.idle": "2024-04-04T08:47:52.012034Z",
12
+ "shell.execute_reply": "2024-04-04T08:47:52.010508Z",
13
+ "shell.execute_reply.started": "2024-04-04T08:47:31.680435Z"
14
  }
15
  },
16
  "outputs": [
17
  {
18
+ "data": {
19
+ "application/vnd.jupyter.widget-view+json": {
20
+ "model_id": "f0c8978e9991468cb37176b7ee5e7f40",
21
+ "version_major": 2,
22
+ "version_minor": 0
23
+ },
24
+ "text/plain": [
25
+ "Downloading builder script: 0%| | 0.00/1.63k [00:00<?, ?B/s]"
26
+ ]
27
+ },
28
+ "metadata": {},
29
+ "output_type": "display_data"
30
+ },
31
+ {
32
+ "data": {
33
+ "application/vnd.jupyter.widget-view+json": {
34
+ "model_id": "1821c061525d4be1b088a0b1624ba0f7",
35
+ "version_major": 2,
36
+ "version_minor": 0
37
+ },
38
+ "text/plain": [
39
+ "Downloading metadata: 0%| | 0.00/1.01k [00:00<?, ?B/s]"
40
+ ]
41
+ },
42
+ "metadata": {},
43
+ "output_type": "display_data"
44
+ },
45
+ {
46
+ "name": "stdout",
47
  "output_type": "stream",
48
  "text": [
49
+ "Downloading and preparing dataset mnist/mnist (download: 11.06 MiB, generated: 19.44 MiB, post-processed: Unknown size, total: 30.50 MiB) to /root/.cache/huggingface/datasets/mnist/mnist/1.0.0/fda16c03c4ecfb13f165ba7e29cf38129ce035011519968cdaf74894ce91c9d4...\n"
50
  ]
51
  },
52
  {
53
  "data": {
54
  "application/vnd.jupyter.widget-view+json": {
55
+ "model_id": "eb3da6f2299d4012862a28dd3a74565d",
56
+ "version_major": 2,
57
+ "version_minor": 0
58
+ },
59
+ "text/plain": [
60
+ "Downloading data files: 0%| | 0/4 [00:00<?, ?it/s]"
61
+ ]
62
+ },
63
+ "metadata": {},
64
+ "output_type": "display_data"
65
+ },
66
+ {
67
+ "data": {
68
+ "application/vnd.jupyter.widget-view+json": {
69
+ "model_id": "e53657e2c6ad4c108ad703dae1f87efe",
70
+ "version_major": 2,
71
+ "version_minor": 0
72
+ },
73
+ "text/plain": [
74
+ "Downloading data: 0%| | 0.00/9.91M [00:00<?, ?B/s]"
75
+ ]
76
+ },
77
+ "metadata": {},
78
+ "output_type": "display_data"
79
+ },
80
+ {
81
+ "data": {
82
+ "application/vnd.jupyter.widget-view+json": {
83
+ "model_id": "2407ea55dc5348619ad666a16a3b97ff",
84
+ "version_major": 2,
85
+ "version_minor": 0
86
+ },
87
+ "text/plain": [
88
+ "Downloading data: 0%| | 0.00/28.9k [00:00<?, ?B/s]"
89
+ ]
90
+ },
91
+ "metadata": {},
92
+ "output_type": "display_data"
93
+ },
94
+ {
95
+ "data": {
96
+ "application/vnd.jupyter.widget-view+json": {
97
+ "model_id": "04a14bdbb03044979afd7c44ba0f424e",
98
+ "version_major": 2,
99
+ "version_minor": 0
100
+ },
101
+ "text/plain": [
102
+ "Downloading data: 0%| | 0.00/1.65M [00:00<?, ?B/s]"
103
+ ]
104
+ },
105
+ "metadata": {},
106
+ "output_type": "display_data"
107
+ },
108
+ {
109
+ "data": {
110
+ "application/vnd.jupyter.widget-view+json": {
111
+ "model_id": "976dba4172974f01ae37ad90b1c6cb1d",
112
+ "version_major": 2,
113
+ "version_minor": 0
114
+ },
115
+ "text/plain": [
116
+ "Downloading data: 0%| | 0.00/4.54k [00:00<?, ?B/s]"
117
+ ]
118
+ },
119
+ "metadata": {},
120
+ "output_type": "display_data"
121
+ },
122
+ {
123
+ "data": {
124
+ "application/vnd.jupyter.widget-view+json": {
125
+ "model_id": "56d60d708f2549a5a0c6acc0e2499884",
126
+ "version_major": 2,
127
+ "version_minor": 0
128
+ },
129
+ "text/plain": [
130
+ "Extracting data files: 0%| | 0/4 [00:00<?, ?it/s]"
131
+ ]
132
+ },
133
+ "metadata": {},
134
+ "output_type": "display_data"
135
+ },
136
+ {
137
+ "data": {
138
+ "application/vnd.jupyter.widget-view+json": {
139
+ "model_id": "87ba67ec7d834407afd7fcf7a9d6ac13",
140
+ "version_major": 2,
141
+ "version_minor": 0
142
+ },
143
+ "text/plain": [
144
+ "Generating train split: 0%| | 0/60000 [00:00<?, ? examples/s]"
145
+ ]
146
+ },
147
+ "metadata": {},
148
+ "output_type": "display_data"
149
+ },
150
+ {
151
+ "data": {
152
+ "application/vnd.jupyter.widget-view+json": {
153
+ "model_id": "696c5019b4c24f049ce78744c1fecded",
154
+ "version_major": 2,
155
+ "version_minor": 0
156
+ },
157
+ "text/plain": [
158
+ "Generating test split: 0%| | 0/10000 [00:00<?, ? examples/s]"
159
+ ]
160
+ },
161
+ "metadata": {},
162
+ "output_type": "display_data"
163
+ },
164
+ {
165
+ "name": "stdout",
166
+ "output_type": "stream",
167
+ "text": [
168
+ "Dataset mnist downloaded and prepared to /root/.cache/huggingface/datasets/mnist/mnist/1.0.0/fda16c03c4ecfb13f165ba7e29cf38129ce035011519968cdaf74894ce91c9d4. Subsequent calls will reuse this data.\n"
169
+ ]
170
+ },
171
+ {
172
+ "data": {
173
+ "application/vnd.jupyter.widget-view+json": {
174
+ "model_id": "d5193a728c974100b02fcb82ac236d80",
175
  "version_major": 2,
176
  "version_minor": 0
177
  },
 
191
  },
192
  {
193
  "cell_type": "code",
194
+ "execution_count": 2,
195
  "id": "7c5ea43d-6a70-4deb-a90e-1d7758c961a3",
196
  "metadata": {
197
  "execution": {
198
+ "iopub.execute_input": "2024-04-04T08:47:52.015528Z",
199
+ "iopub.status.busy": "2024-04-04T08:47:52.014998Z",
200
+ "iopub.status.idle": "2024-04-04T08:47:56.923333Z",
201
+ "shell.execute_reply": "2024-04-04T08:47:56.922341Z",
202
+ "shell.execute_reply.started": "2024-04-04T08:47:52.015483Z"
203
  }
204
  },
205
  "outputs": [],
 
207
  "import numpy as np\n",
208
  "from PIL import Image\n",
209
  "import io\n",
210
+ "import tensorflow as tf\n",
211
  "\n",
212
+ "def convert_image(byte_like_str_image):\n",
213
+ " return tf.convert_to_tensor(np.asarray(Image.open(io.BytesIO(byte_like_str_image['bytes']))), dtype = tf.float32)"
214
  ]
215
  },
216
  {
217
  "cell_type": "code",
218
+ "execution_count": 3,
219
  "id": "daafde17-8100-4f49-b27e-8aad43b129c6",
220
  "metadata": {
221
  "execution": {
222
+ "iopub.execute_input": "2024-04-04T08:47:56.925474Z",
223
+ "iopub.status.busy": "2024-04-04T08:47:56.924961Z",
224
+ "iopub.status.idle": "2024-04-04T08:48:32.236745Z",
225
+ "shell.execute_reply": "2024-04-04T08:48:32.234178Z",
226
+ "shell.execute_reply.started": "2024-04-04T08:47:56.925446Z"
227
  }
228
  },
229
  "outputs": [],
230
  "source": [
231
+ "from keras.utils import to_categorical\n",
232
+ "\n",
233
  "dataset_train = dataset['train'].to_pandas()\n",
234
+ "dataset_train['image'] = dataset_train['image'].map(convert_image)\n",
235
  "\n",
236
  "dataset_test = dataset['test'].to_pandas()\n",
237
+ "dataset_test['image'] = dataset_test['image'].map(convert_image)\n",
238
+ "\n",
239
+ "# Convert labels to NumPy arrays\n",
240
+ "X_train = np.array(dataset_train['image'].tolist())\n",
241
+ "y_train = np.array(dataset_train['label'])\n",
242
+ "\n",
243
+ "X_test = np.array(dataset_test['image'].tolist())\n",
244
+ "y_test = np.array(dataset_test['label'])\n",
245
+ "# dataset_train['label'] = dataset_train['label'].astype('float32')\n",
246
+ "# dataset_test['label'] = dataset_test['label'].astype('float32')\n",
247
+ "\n"
248
  ]
249
  },
250
  {
251
  "cell_type": "code",
252
+ "execution_count": 4,
253
  "id": "72022fd2-000d-4d5c-88d5-9afc62c283d5",
254
+ "metadata": {
255
+ "execution": {
256
+ "iopub.execute_input": "2024-04-04T08:48:32.239111Z",
257
+ "iopub.status.busy": "2024-04-04T08:48:32.238769Z",
258
+ "iopub.status.idle": "2024-04-04T08:48:32.369358Z",
259
+ "shell.execute_reply": "2024-04-04T08:48:32.367989Z",
260
+ "shell.execute_reply.started": "2024-04-04T08:48:32.239071Z"
261
+ }
262
+ },
263
+ "outputs": [],
264
+ "source": [
265
+ "import tensorflow as tf\n",
266
+ "\n",
267
+ "model = tf.keras.models.Sequential([\n",
268
+ " tf.keras.layers.Flatten(input_shape=(28, 28)),\n",
269
+ " tf.keras.layers.Dense(128, activation='relu'),\n",
270
+ " tf.keras.layers.Dense(10)\n",
271
+ "])"
272
+ ]
273
+ },
274
+ {
275
+ "cell_type": "code",
276
+ "execution_count": 5,
277
+ "id": "dd7871ac-cacd-4866-bdda-67651f592262",
278
+ "metadata": {
279
+ "execution": {
280
+ "iopub.execute_input": "2024-04-04T08:48:32.371520Z",
281
+ "iopub.status.busy": "2024-04-04T08:48:32.371132Z",
282
+ "iopub.status.idle": "2024-04-04T08:48:32.391109Z",
283
+ "shell.execute_reply": "2024-04-04T08:48:32.390162Z",
284
+ "shell.execute_reply.started": "2024-04-04T08:48:32.371484Z"
285
+ }
286
+ },
287
  "outputs": [],
288
+ "source": [
289
+ "model.compile(\n",
290
+ " optimizer=tf.keras.optimizers.Adam(0.001),\n",
291
+ " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n",
292
+ " metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],\n",
293
+ ")"
294
+ ]
295
+ },
296
+ {
297
+ "cell_type": "code",
298
+ "execution_count": 6,
299
+ "id": "280e0d9d-d9e8-41d9-b9ad-666e84fc0bfa",
300
+ "metadata": {
301
+ "execution": {
302
+ "iopub.execute_input": "2024-04-04T08:48:32.392690Z",
303
+ "iopub.status.busy": "2024-04-04T08:48:32.392405Z",
304
+ "iopub.status.idle": "2024-04-04T08:49:02.402966Z",
305
+ "shell.execute_reply": "2024-04-04T08:49:02.402204Z",
306
+ "shell.execute_reply.started": "2024-04-04T08:48:32.392662Z"
307
+ }
308
+ },
309
+ "outputs": [
310
+ {
311
+ "name": "stdout",
312
+ "output_type": "stream",
313
+ "text": [
314
+ "Epoch 1/5\n",
315
+ "1875/1875 [==============================] - 7s 3ms/step - loss: 2.6159 - sparse_categorical_accuracy: 0.8597 - val_loss: 0.5951 - val_sparse_categorical_accuracy: 0.8762\n",
316
+ "Epoch 2/5\n",
317
+ "1875/1875 [==============================] - 6s 3ms/step - loss: 0.4041 - sparse_categorical_accuracy: 0.9071 - val_loss: 0.4567 - val_sparse_categorical_accuracy: 0.9024\n",
318
+ "Epoch 3/5\n",
319
+ "1875/1875 [==============================] - 6s 3ms/step - loss: 0.2977 - sparse_categorical_accuracy: 0.9247 - val_loss: 0.3300 - val_sparse_categorical_accuracy: 0.9237\n",
320
+ "Epoch 4/5\n",
321
+ "1875/1875 [==============================] - 5s 3ms/step - loss: 0.2646 - sparse_categorical_accuracy: 0.9344 - val_loss: 0.2909 - val_sparse_categorical_accuracy: 0.9368\n",
322
+ "Epoch 5/5\n",
323
+ "1875/1875 [==============================] - 5s 3ms/step - loss: 0.2418 - sparse_categorical_accuracy: 0.9391 - val_loss: 0.2886 - val_sparse_categorical_accuracy: 0.9330\n",
324
+ "<class 'pandas.core.frame.DataFrame'>\n",
325
+ "RangeIndex: 60000 entries, 0 to 59999\n",
326
+ "Data columns (total 2 columns):\n",
327
+ " # Column Non-Null Count Dtype \n",
328
+ "--- ------ -------------- ----- \n",
329
+ " 0 image 60000 non-null object\n",
330
+ " 1 label 60000 non-null int64 \n",
331
+ "dtypes: int64(1), object(1)\n",
332
+ "memory usage: 937.6+ KB\n"
333
+ ]
334
+ }
335
+ ],
336
+ "source": [
337
+ "# model.fit(\n",
338
+ "# dataset_train['image'], dataset_test['label'],\n",
339
+ "# epochs=10,\n",
340
+ "# validation_data=(dataset_test['image'], dataset_test['label']),\n",
341
+ "# )\n",
342
+ "# dataset_train['label'].head(50)\n",
343
+ "model.fit(X_train, y_train, epochs=5, validation_data=(X_test, y_test))\n",
344
+ "\n",
345
+ "dataset_train.info()\n",
346
+ "\n"
347
+ ]
348
+ },
349
+ {
350
+ "cell_type": "code",
351
+ "execution_count": 8,
352
+ "id": "c7317f9a-14f4-4908-9895-8bc085900e28",
353
+ "metadata": {
354
+ "execution": {
355
+ "iopub.execute_input": "2024-04-04T08:49:35.659063Z",
356
+ "iopub.status.busy": "2024-04-04T08:49:35.658449Z",
357
+ "iopub.status.idle": "2024-04-04T08:49:36.588926Z",
358
+ "shell.execute_reply": "2024-04-04T08:49:36.588088Z",
359
+ "shell.execute_reply.started": "2024-04-04T08:49:35.659019Z"
360
+ }
361
+ },
362
+ "outputs": [
363
+ {
364
+ "name": "stdout",
365
+ "output_type": "stream",
366
+ "text": [
367
+ "313/313 [==============================] - 1s 3ms/step - loss: 0.2886 - sparse_categorical_accuracy: 0.9330\n"
368
+ ]
369
+ },
370
+ {
371
+ "data": {
372
+ "text/plain": [
373
+ "0.9330000281333923"
374
+ ]
375
+ },
376
+ "execution_count": 8,
377
+ "metadata": {},
378
+ "output_type": "execute_result"
379
+ }
380
+ ],
381
+ "source": [
382
+ "loss, accuracy = model.evaluate(X_test, y_test)\n",
383
+ "accuracy"
384
+ ]
385
  }
386
  ],
387
  "metadata": {