Skip to content

Commit d86455e

Browse files
committed
Add comparision model
1 parent 490c7df commit d86455e

File tree

1 file changed

+152
-27
lines changed

1 file changed

+152
-27
lines changed

Chapter 7/Exercise 3.ipynb

Lines changed: 152 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 11,
5+
"execution_count": 1,
66
"metadata": {},
7-
"outputs": [],
7+
"outputs": [
8+
{
9+
"name": "stderr",
10+
"output_type": "stream",
11+
"text": [
12+
"Using TensorFlow backend.\n"
13+
]
14+
}
15+
],
816
"source": [
917
"import pandas as pd\n",
1018
"import numpy as np\n",
@@ -21,25 +29,16 @@
2129
},
2230
{
2331
"cell_type": "code",
24-
"execution_count": 5,
32+
"execution_count": 2,
2533
"metadata": {},
26-
"outputs": [
27-
{
28-
"name": "stdout",
29-
"output_type": "stream",
30-
"text": [
31-
"Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz\n",
32-
"170500096/170498071 [==============================] - 263s 2us/step\n"
33-
]
34-
}
35-
],
34+
"outputs": [],
3635
"source": [
3736
"(x_train, y_train), (x_test, y_test) = cifar10.load_data()"
3837
]
3938
},
4039
{
4140
"cell_type": "code",
42-
"execution_count": 6,
41+
"execution_count": 3,
4342
"metadata": {},
4443
"outputs": [
4544
{
@@ -48,7 +47,7 @@
4847
"(50000, 32, 32, 3)"
4948
]
5049
},
51-
"execution_count": 6,
50+
"execution_count": 3,
5251
"metadata": {},
5352
"output_type": "execute_result"
5453
}
@@ -59,7 +58,7 @@
5958
},
6059
{
6160
"cell_type": "code",
62-
"execution_count": 7,
61+
"execution_count": 4,
6362
"metadata": {},
6463
"outputs": [
6564
{
@@ -68,7 +67,7 @@
6867
"(50000, 1)"
6968
]
7069
},
71-
"execution_count": 7,
70+
"execution_count": 4,
7271
"metadata": {},
7372
"output_type": "execute_result"
7473
}
@@ -79,7 +78,7 @@
7978
},
8079
{
8180
"cell_type": "code",
82-
"execution_count": 8,
81+
"execution_count": 5,
8382
"metadata": {},
8483
"outputs": [],
8584
"source": [
@@ -89,7 +88,7 @@
8988
},
9089
{
9190
"cell_type": "code",
92-
"execution_count": 9,
91+
"execution_count": 6,
9392
"metadata": {},
9493
"outputs": [],
9594
"source": [
@@ -99,6 +98,137 @@
9998
"y_test = label_binarizer.transform(y_test)"
10099
]
101100
},
101+
{
102+
"cell_type": "markdown",
103+
"metadata": {},
104+
"source": [
105+
"## Model without regularization"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 7,
111+
"metadata": {},
112+
"outputs": [
113+
{
114+
"name": "stdout",
115+
"output_type": "stream",
116+
"text": [
117+
"WARNING:tensorflow:From C:\\Users\\Maedr3\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\framework\\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
118+
"Instructions for updating:\n",
119+
"Colocations handled automatically by placer.\n",
120+
"_________________________________________________________________\n",
121+
"Layer (type) Output Shape Param # \n",
122+
"=================================================================\n",
123+
"conv2d_1 (Conv2D) (None, 32, 32, 32) 896 \n",
124+
"_________________________________________________________________\n",
125+
"conv2d_2 (Conv2D) (None, 30, 30, 32) 9248 \n",
126+
"_________________________________________________________________\n",
127+
"max_pooling2d_1 (MaxPooling2 (None, 15, 15, 32) 0 \n",
128+
"_________________________________________________________________\n",
129+
"flatten_1 (Flatten) (None, 7200) 0 \n",
130+
"_________________________________________________________________\n",
131+
"dense_1 (Dense) (None, 512) 3686912 \n",
132+
"_________________________________________________________________\n",
133+
"dense_2 (Dense) (None, 10) 5130 \n",
134+
"=================================================================\n",
135+
"Total params: 3,702,186\n",
136+
"Trainable params: 3,702,186\n",
137+
"Non-trainable params: 0\n",
138+
"_________________________________________________________________\n"
139+
]
140+
}
141+
],
142+
"source": [
143+
"model = Sequential()\n",
144+
" \n",
145+
"model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(32,32,3))) \n",
146+
"model.add(Conv2D(32, (3, 3), activation='relu')) \n",
147+
"model.add(MaxPool2D(pool_size=(2, 2)))\n",
148+
"\n",
149+
"model.add(Flatten())\n",
150+
"\n",
151+
"model.add(Dense(512, activation='relu'))\n",
152+
"\n",
153+
"model.add(Dense(10, activation='softmax'))\n",
154+
"\n",
155+
"model.summary()\n",
156+
"\n",
157+
"model.compile(loss='categorical_crossentropy', \n",
158+
" optimizer='adam',\n",
159+
" metrics = ['accuracy'])"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": 8,
165+
"metadata": {},
166+
"outputs": [
167+
{
168+
"name": "stdout",
169+
"output_type": "stream",
170+
"text": [
171+
"WARNING:tensorflow:From C:\\Users\\Maedr3\\AppData\\Roaming\\Python\\Python36\\site-packages\\tensorflow\\python\\ops\\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
172+
"Instructions for updating:\n",
173+
"Use tf.cast instead.\n",
174+
"Train on 50000 samples, validate on 10000 samples\n",
175+
"Epoch 1/10\n",
176+
"50000/50000 [==============================] - 318s 6ms/step - loss: 14.4860 - acc: 0.1003 - val_loss: 14.5063 - val_acc: 0.1000\n",
177+
"Epoch 2/10\n",
178+
"50000/50000 [==============================] - 329s 7ms/step - loss: 14.5063 - acc: 0.1000 - val_loss: 14.5063 - val_acc: 0.1000\n",
179+
"Epoch 3/10\n",
180+
"50000/50000 [==============================] - 344s 7ms/step - loss: 14.5063 - acc: 0.1000 - val_loss: 14.5063 - val_acc: 0.1000\n",
181+
"Epoch 4/10\n",
182+
"50000/50000 [==============================] - 320s 6ms/step - loss: 14.5063 - acc: 0.1000 - val_loss: 14.5063 - val_acc: 0.1000\n",
183+
"Epoch 5/10\n",
184+
"50000/50000 [==============================] - 304s 6ms/step - loss: 14.5063 - acc: 0.1000 - val_loss: 14.5063 - val_acc: 0.1000\n",
185+
"Epoch 6/10\n",
186+
"50000/50000 [==============================] - 306s 6ms/step - loss: 14.5063 - acc: 0.1000 - val_loss: 14.5063 - val_acc: 0.1000\n",
187+
"Epoch 7/10\n",
188+
"50000/50000 [==============================] - 312s 6ms/step - loss: 14.5063 - acc: 0.1000 - val_loss: 14.5063 - val_acc: 0.1000\n",
189+
"Epoch 8/10\n",
190+
"50000/50000 [==============================] - 317s 6ms/step - loss: 14.5063 - acc: 0.1000 - val_loss: 14.5063 - val_acc: 0.1000\n",
191+
"Epoch 9/10\n",
192+
"50000/50000 [==============================] - 311s 6ms/step - loss: 14.5063 - acc: 0.1000 - val_loss: 14.5063 - val_acc: 0.1000\n",
193+
"Epoch 10/10\n",
194+
"50000/50000 [==============================] - 306s 6ms/step - loss: 14.5063 - acc: 0.1000 - val_loss: 14.5063 - val_acc: 0.1000\n"
195+
]
196+
}
197+
],
198+
"source": [
199+
"model_details = model.fit(x_train, y_train,\n",
200+
" batch_size = 512, \n",
201+
" epochs = 10, \n",
202+
" validation_data= (x_test, y_test),\n",
203+
" verbose=1)"
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": 10,
209+
"metadata": {},
210+
"outputs": [
211+
{
212+
"name": "stdout",
213+
"output_type": "stream",
214+
"text": [
215+
"10000/10000 [==============================] - 21s 2ms/step\n",
216+
"Accuracy: 10.00%\n"
217+
]
218+
}
219+
],
220+
"source": [
221+
"score = model.evaluate(x_test, y_test)\n",
222+
"print(\"Accuracy: {0:.2f}%\".format(score[1]*100))"
223+
]
224+
},
225+
{
226+
"cell_type": "markdown",
227+
"metadata": {},
228+
"source": [
229+
"## Model with regularization"
230+
]
231+
},
102232
{
103233
"cell_type": "code",
104234
"execution_count": 19,
@@ -272,7 +402,9 @@
272402
{
273403
"cell_type": "code",
274404
"execution_count": 33,
275-
"metadata": {},
405+
"metadata": {
406+
"scrolled": true
407+
},
276408
"outputs": [
277409
{
278410
"data": {
@@ -332,13 +464,6 @@
332464
"plt.show()\n",
333465
"print(\"Prediction: {0}\".format(labels[np.argmax(y_pred[correct_indices[image]])]))"
334466
]
335-
},
336-
{
337-
"cell_type": "code",
338-
"execution_count": null,
339-
"metadata": {},
340-
"outputs": [],
341-
"source": []
342467
}
343468
],
344469
"metadata": {

0 commit comments

Comments
 (0)