Created
March 11, 2018 09:21
-
-
Save JannesKlaas/a4e1caeb0e787411a03249d96aa67b3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using TensorFlow backend.\n", | |
"Couldn't import dot_parser, loading of dot files will not be possible.\n" | |
] | |
} | |
], | |
"source": [ | |
"from keras.layers import Conv2D, Activation, MaxPool2D, Flatten, Dense\n", | |
"from keras.models import Sequential\n", | |
"\n", | |
"# Images fed into this model are 512 x 512 pixels with 3 channels\n", | |
"img_shape = (28,28,1)\n", | |
"\n", | |
"# Set up model\n", | |
"model = Sequential()\n", | |
"\n", | |
"# Add convolutional layer with 3, 3 by 3 filters and a stride size of 1\n", | |
"# Set padding so that input size equals output size\n", | |
"model.add(Conv2D(6,2,input_shape=img_shape))\n", | |
"# Add relu activation to the layer \n", | |
"model.add(Activation('relu'))\n", | |
"\n", | |
"model.add(MaxPool2D(2))\n", | |
"\n", | |
"model.add(Flatten())\n", | |
"\n", | |
"model.add(Dense(10))\n", | |
"\n", | |
"model.add(Activation('softmax'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"_________________________________________________________________\n", | |
"Layer (type) Output Shape Param # \n", | |
"=================================================================\n", | |
"conv2d_1 (Conv2D) (None, 27, 27, 6) 30 \n", | |
"_________________________________________________________________\n", | |
"activation_1 (Activation) (None, 27, 27, 6) 0 \n", | |
"_________________________________________________________________\n", | |
"max_pooling2d_1 (MaxPooling2 (None, 13, 13, 6) 0 \n", | |
"_________________________________________________________________\n", | |
"flatten_1 (Flatten) (None, 1014) 0 \n", | |
"_________________________________________________________________\n", | |
"dense_1 (Dense) (None, 10) 10150 \n", | |
"_________________________________________________________________\n", | |
"activation_2 (Activation) (None, 10) 0 \n", | |
"=================================================================\n", | |
"Total params: 10,180\n", | |
"Trainable params: 10,180\n", | |
"Non-trainable params: 0\n", | |
"_________________________________________________________________\n" | |
] | |
} | |
], | |
"source": [ | |
"model.summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"model.compile(loss='sparse_categorical_crossentropy', optimizer = 'adam', metrics=['acc'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from keras.datasets import mnist" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"(x_train, y_train), (x_test, y_test) = mnist.load_data()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(60000, 28, 28)" | |
] | |
}, | |
"execution_count": 37, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"x_train.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x_train = np.expand_dims(x_train,-1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x_test = np.expand_dims(x_test,-1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(60000, 28, 28, 1)" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"x_train.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(60000,)" | |
] | |
}, | |
"execution_count": 41, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"y_train.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Train on 60000 samples, validate on 10000 samples\n", | |
"Epoch 1/10\n", | |
"60000/60000 [==============================] - 10s 175us/step - loss: 4.0330 - acc: 0.7424 - val_loss: 3.5352 - val_acc: 0.7746\n", | |
"Epoch 2/10\n", | |
"60000/60000 [==============================] - 10s 169us/step - loss: 3.5208 - acc: 0.7746 - val_loss: 3.4403 - val_acc: 0.7794\n", | |
"Epoch 3/10\n", | |
"60000/60000 [==============================] - 11s 176us/step - loss: 2.4443 - acc: 0.8372 - val_loss: 1.9846 - val_acc: 0.8645\n", | |
"Epoch 4/10\n", | |
"60000/60000 [==============================] - 10s 173us/step - loss: 1.8943 - acc: 0.8691 - val_loss: 1.8478 - val_acc: 0.8713\n", | |
"Epoch 5/10\n", | |
"60000/60000 [==============================] - 10s 174us/step - loss: 1.7726 - acc: 0.8735 - val_loss: 1.7595 - val_acc: 0.8718\n", | |
"Epoch 6/10\n", | |
"60000/60000 [==============================] - 10s 174us/step - loss: 1.6943 - acc: 0.8765 - val_loss: 1.7150 - val_acc: 0.8745\n", | |
"Epoch 7/10\n", | |
"60000/60000 [==============================] - 10s 173us/step - loss: 1.6765 - acc: 0.8777 - val_loss: 1.7268 - val_acc: 0.8688\n", | |
"Epoch 8/10\n", | |
"60000/60000 [==============================] - 10s 173us/step - loss: 1.6676 - acc: 0.8799 - val_loss: 1.7110 - val_acc: 0.8749\n", | |
"Epoch 9/10\n", | |
"60000/60000 [==============================] - 10s 172us/step - loss: 1.4759 - acc: 0.8888 - val_loss: 0.1346 - val_acc: 0.9597\n", | |
"Epoch 10/10\n", | |
"60000/60000 [==============================] - 11s 177us/step - loss: 0.1026 - acc: 0.9681 - val_loss: 0.1144 - val_acc: 0.9693\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<keras.callbacks.History at 0x124eec2b0>" | |
] | |
}, | |
"execution_count": 44, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"model.fit(x_train,y_train,batch_size=32,epochs=10,validation_data=(x_test,y_test))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [default]", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice, clear and useful explanation ever. Thank you so much for sharing this information.