Skip to content

Commit e616341

Browse files
Created using Colab
1 parent 9c28d11 commit e616341

File tree

1 file changed

+244
-0
lines changed

1 file changed

+244
-0
lines changed

Classifier_for_Mnist_Dataset.ipynb

+244
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"authorship_tag": "ABX9TyOzH+SSjKU5c2ndBao5CBcp",
8+
"include_colab_link": true
9+
},
10+
"kernelspec": {
11+
"name": "python3",
12+
"display_name": "Python 3"
13+
},
14+
"language_info": {
15+
"name": "python"
16+
}
17+
},
18+
"cells": [
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {
22+
"id": "view-in-github",
23+
"colab_type": "text"
24+
},
25+
"source": [
26+
"<a href=\"https://colab.research.google.com/github/masoudshahrian/Deep-Learning-Code/blob/main/Classifier_for_Mnist_Dataset.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"source": [
32+
"import tensorflow as tf\n",
33+
"from tensorflow.keras.models import Sequential\n",
34+
"from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\n",
35+
"from tensorflow.keras.datasets import mnist"
36+
],
37+
"metadata": {
38+
"id": "yE1K-WRsk2Pm"
39+
},
40+
"execution_count": 1,
41+
"outputs": []
42+
},
43+
{
44+
"cell_type": "code",
45+
"source": [
46+
"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n"
47+
],
48+
"metadata": {
49+
"colab": {
50+
"base_uri": "https://localhost:8080/"
51+
},
52+
"id": "TIuUzUBik2Me",
53+
"outputId": "2c3d4b63-ab41-4629-fe9c-5df14d7b1ec5"
54+
},
55+
"execution_count": 2,
56+
"outputs": [
57+
{
58+
"output_type": "stream",
59+
"name": "stdout",
60+
"text": [
61+
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n",
62+
"\u001b[1m11490434/11490434\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 0us/step\n"
63+
]
64+
}
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"source": [
70+
"x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)\n",
71+
"x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)"
72+
],
73+
"metadata": {
74+
"id": "lWbBBinDk2I-"
75+
},
76+
"execution_count": 3,
77+
"outputs": []
78+
},
79+
{
80+
"cell_type": "code",
81+
"source": [
82+
"x_train = x_train.astype('float32')\n",
83+
"x_test = x_test.astype('float32')"
84+
],
85+
"metadata": {
86+
"id": "VxXmiExQk2FW"
87+
},
88+
"execution_count": 4,
89+
"outputs": []
90+
},
91+
{
92+
"cell_type": "code",
93+
"source": [
94+
"y_train = tf.keras.utils.to_categorical(y_train, 10)\n",
95+
"y_test = tf.keras.utils.to_categorical(y_test, 10)"
96+
],
97+
"metadata": {
98+
"id": "vN4Lt0Kdk14_"
99+
},
100+
"execution_count": 5,
101+
"outputs": []
102+
},
103+
{
104+
"cell_type": "code",
105+
"source": [
106+
"\n",
107+
"model = Sequential()"
108+
],
109+
"metadata": {
110+
"id": "w7XpclkHlGn3"
111+
},
112+
"execution_count": 6,
113+
"outputs": []
114+
},
115+
{
116+
"cell_type": "code",
117+
"source": [
118+
"model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))\n",
119+
"model.add(MaxPooling2D((2, 2)))\n",
120+
"model.add(Flatten())\n",
121+
"model.add(Dense(128, activation='relu'))\n",
122+
"model.add(Dense(10, activation='softmax'))"
123+
],
124+
"metadata": {
125+
"id": "DzmDPJyBlGkW",
126+
"colab": {
127+
"base_uri": "https://localhost:8080/"
128+
},
129+
"outputId": "61267d92-7b65-41e9-ee2d-42dc1c00b0fe"
130+
},
131+
"execution_count": 7,
132+
"outputs": [
133+
{
134+
"output_type": "stream",
135+
"name": "stderr",
136+
"text": [
137+
"/usr/local/lib/python3.10/dist-packages/keras/src/layers/convolutional/base_conv.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
138+
" super().__init__(activity_regularizer=activity_regularizer, **kwargs)\n"
139+
]
140+
}
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"source": [
146+
"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])"
147+
],
148+
"metadata": {
149+
"id": "KPdqtn1UlGg-"
150+
},
151+
"execution_count": 8,
152+
"outputs": []
153+
},
154+
{
155+
"cell_type": "code",
156+
"source": [
157+
"model.fit(x_train, y_train, epochs=10, batch_size=128)"
158+
],
159+
"metadata": {
160+
"colab": {
161+
"base_uri": "https://localhost:8080/"
162+
},
163+
"id": "_RcLXkDWlGdo",
164+
"outputId": "b5a357a1-e021-4c33-c351-749b59c15c6c"
165+
},
166+
"execution_count": 9,
167+
"outputs": [
168+
{
169+
"output_type": "stream",
170+
"name": "stdout",
171+
"text": [
172+
"Epoch 1/10\n",
173+
"\u001b[1m469/469\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m34s\u001b[0m 69ms/step - accuracy: 0.8196 - loss: 5.2767\n",
174+
"Epoch 2/10\n",
175+
"\u001b[1m469/469\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m39s\u001b[0m 66ms/step - accuracy: 0.9649 - loss: 0.1228\n",
176+
"Epoch 3/10\n",
177+
"\u001b[1m469/469\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m41s\u001b[0m 66ms/step - accuracy: 0.9786 - loss: 0.0723\n",
178+
"Epoch 4/10\n",
179+
"\u001b[1m469/469\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m45s\u001b[0m 76ms/step - accuracy: 0.9864 - loss: 0.0424\n",
180+
"Epoch 5/10\n",
181+
"\u001b[1m469/469\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m31s\u001b[0m 66ms/step - accuracy: 0.9890 - loss: 0.0344\n",
182+
"Epoch 6/10\n",
183+
"\u001b[1m469/469\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m41s\u001b[0m 67ms/step - accuracy: 0.9917 - loss: 0.0243\n",
184+
"Epoch 7/10\n",
185+
"\u001b[1m469/469\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m40s\u001b[0m 66ms/step - accuracy: 0.9929 - loss: 0.0220\n",
186+
"Epoch 8/10\n",
187+
"\u001b[1m469/469\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m44s\u001b[0m 72ms/step - accuracy: 0.9927 - loss: 0.0217\n",
188+
"Epoch 9/10\n",
189+
"\u001b[1m469/469\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m39s\u001b[0m 67ms/step - accuracy: 0.9926 - loss: 0.0215\n",
190+
"Epoch 10/10\n",
191+
"\u001b[1m469/469\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m41s\u001b[0m 67ms/step - accuracy: 0.9926 - loss: 0.0227\n"
192+
]
193+
},
194+
{
195+
"output_type": "execute_result",
196+
"data": {
197+
"text/plain": [
198+
"<keras.src.callbacks.history.History at 0x79e13b44b430>"
199+
]
200+
},
201+
"metadata": {},
202+
"execution_count": 9
203+
}
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"source": [
209+
"score = model.evaluate(x_test, y_test, verbose=0)"
210+
],
211+
"metadata": {
212+
"id": "J6N_Vxu-lPIW"
213+
},
214+
"execution_count": 10,
215+
"outputs": []
216+
},
217+
{
218+
"cell_type": "code",
219+
"source": [
220+
"\n",
221+
"print('Test loss:', score[0])\n",
222+
"print('Test accuracy:', score[1])"
223+
],
224+
"metadata": {
225+
"colab": {
226+
"base_uri": "https://localhost:8080/"
227+
},
228+
"id": "btszLDOplPEl",
229+
"outputId": "9c191cc6-ff5b-46b2-d816-f2179f0292f7"
230+
},
231+
"execution_count": 11,
232+
"outputs": [
233+
{
234+
"output_type": "stream",
235+
"name": "stdout",
236+
"text": [
237+
"Test loss: 0.09840740263462067\n",
238+
"Test accuracy: 0.9787999987602234\n"
239+
]
240+
}
241+
]
242+
}
243+
]
244+
}

0 commit comments

Comments
 (0)