Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e67f1a
commit a1f9b08
Showing
30 changed files
with
4,614 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"Using TensorFlow backend.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"from keras.datasets import mnist\n", | ||
"import numpy as np\n", | ||
"from keras.models import Sequential\n", | ||
"from keras.utils import np_utils\n", | ||
"from keras.optimizers import SGD\n", | ||
"from keras.layers.core import Dense,Activation\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Train samples (60000, 28, 28), Train labels (60000,)\n", | ||
"Test samples (10000, 28, 28), Test labels (10000,)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"(X_train, y_train), (X_test, y_test) = mnist.load_data()\n", | ||
"print(\"Train samples {}, Train labels {}\".format(X_train.shape, y_train.shape))\n", | ||
"print(\"Test samples {}, Test labels {}\".format(X_test.shape, y_test.shape))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# reshape to batch size by height * width\n", | ||
"h, w = X_train.shape[1:]\n", | ||
"X_train = X_train.reshape(X_train.shape[0], h * w)\n", | ||
"X_test = X_test.reshape(X_test.shape[0], h * w)\n", | ||
"X_train = X_train.astype('float32')\n", | ||
"X_test = X_test.astype('float32')\n", | ||
"# scale to [0, 1], scale to [0, 2], offset by -1\n", | ||
"X_train = (X_train / 255.0) * 2 - 1\n", | ||
"X_test = (X_test - 255.0) * 2 - 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# convert class vectors to a matrix of one-hot vectors\n", | ||
"n_classes = 10\n", | ||
"y_train = np_utils.to_categorical(y_train, n_classes)\n", | ||
"y_test = np_utils.to_categorical(y_test, n_classes)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"_________________________________________________________________\n", | ||
"Layer (type) Output Shape Param # \n", | ||
"=================================================================\n", | ||
"dense_1 (Dense) (None, 128) 100480 \n", | ||
"_________________________________________________________________\n", | ||
"dense_2 (Dense) (None, 10) 1290 \n", | ||
"=================================================================\n", | ||
"Total params: 101,770\n", | ||
"Trainable params: 101,770\n", | ||
"Non-trainable params: 0\n", | ||
"_________________________________________________________________\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"n_hidden = 128\n", | ||
"model = Sequential()\n", | ||
"model.add(Dense(n_hidden, activation='tanh', input_dim=h*w))\n", | ||
"model.add(Dense(n_classes, activation='softmax'))\n", | ||
"model.summary()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"sgd = SGD(lr=0.001)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Epoch 1/10\n", | ||
"60000/60000 [==============================] - 1s 20us/step - loss: 1.7773 - acc: 0.4539\n", | ||
"Epoch 2/10\n", | ||
"60000/60000 [==============================] - 1s 16us/step - loss: 1.0989 - acc: 0.7330\n", | ||
"Epoch 3/10\n", | ||
"60000/60000 [==============================] - 1s 16us/step - loss: 0.8551 - acc: 0.7964\n", | ||
"Epoch 4/10\n", | ||
"60000/60000 [==============================] - 1s 17us/step - loss: 0.7306 - acc: 0.8256: 1s - loss: \n", | ||
"Epoch 5/10\n", | ||
"60000/60000 [==============================] - 1s 16us/step - loss: 0.6531 - acc: 0.8412\n", | ||
"Epoch 6/10\n", | ||
"60000/60000 [==============================] - 1s 16us/step - loss: 0.5996 - acc: 0.8526\n", | ||
"Epoch 7/10\n", | ||
"60000/60000 [==============================] - 1s 16us/step - loss: 0.5600 - acc: 0.8608\n", | ||
"Epoch 8/10\n", | ||
"60000/60000 [==============================] - 1s 17us/step - loss: 0.5294 - acc: 0.8663\n", | ||
"Epoch 9/10\n", | ||
"60000/60000 [==============================] - 1s 16us/step - loss: 0.5047 - acc: 0.8712\n", | ||
"Epoch 10/10\n", | ||
"60000/60000 [==============================] - 1s 16us/step - loss: 0.4844 - acc: 0.8752\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<keras.callbacks.History at 0xd4b5b70>" | ||
] | ||
}, | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"model.fit(X_train, y_train, epochs=10, batch_size=128)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"10000/10000 [==============================] - 0s 11us/step\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"score = model.evaluate(X_test, y_test, batch_size=128)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"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.6.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.