Last active
March 22, 2018 08:04
-
-
Save hoto17296/5415c3577ce5f73129d949426788c5a3 to your computer and use it in GitHub Desktop.
GPU Practice
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
run_jupyter: | |
docker run --runtime=nvidia --rm -d -p 80:8888 -v /notebooks:/notebooks \ | |
tensorflow/tensorflow:latest-gpu-py3 jupyter notebook \ | |
--notebook-dir=/notebooks --allow-root --NotebookApp.token='' |
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": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"/usr/local/lib/python3.5/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n", | |
" from ._conv import register_converters as _register_converters\n" | |
] | |
} | |
], | |
"source": [ | |
"from tensorflow.contrib import keras" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"num_classes = 10" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz\n", | |
"11493376/11490434 [==============================]11493376/11490434 [==============================] - 6s 1us/step\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()\n", | |
"\n", | |
"input_shape = x_train.shape[1:] + (1,)\n", | |
"x_train = x_train.reshape(x_train.shape[0], *input_shape).astype('float32') / 0xff\n", | |
"x_test = x_test.reshape(x_test.shape[0], *input_shape).astype('float32') / 0xff\n", | |
"y_train = keras.utils.to_categorical(y_train, num_classes)\n", | |
"y_test = keras.utils.to_categorical(y_test, num_classes)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"model = keras.models.Sequential()\n", | |
"model.add(keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))\n", | |
"model.add(keras.layers.Conv2D(64, (3, 3), activation='relu'))\n", | |
"model.add(keras.layers.MaxPooling2D(pool_size=(2, 2)))\n", | |
"model.add(keras.layers.Dropout(0.25))\n", | |
"model.add(keras.layers.Flatten())\n", | |
"model.add(keras.layers.Dense(128, activation='relu'))\n", | |
"model.add(keras.layers.Dropout(0.5))\n", | |
"model.add(keras.layers.Dense(num_classes, activation='softmax'))\n", | |
"\n", | |
"model.compile(loss=keras.losses.categorical_crossentropy,\n", | |
" optimizer=keras.optimizers.Adam(),\n", | |
" metrics=['accuracy'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Train on 60000 samples, validate on 10000 samples\n", | |
"Epoch 1/12\n", | |
"60000/60000 [==============================]60000/60000 [==============================] - 8s 130us/step - loss: 0.2440 - acc: 0.9263 - val_loss: 0.0503 - val_acc: 0.9843\n", | |
"\n", | |
"Epoch 2/12\n", | |
"60000/60000 [==============================]60000/60000 [==============================] - 5s 81us/step - loss: 0.0862 - acc: 0.9744 - val_loss: 0.0363 - val_acc: 0.9870\n", | |
"\n", | |
"Epoch 3/12\n", | |
"60000/60000 [==============================]60000/60000 [==============================] - 5s 81us/step - loss: 0.0647 - acc: 0.9801 - val_loss: 0.0349 - val_acc: 0.9878\n", | |
"\n", | |
"Epoch 4/12\n", | |
"60000/60000 [==============================]60000/60000 [==============================] - 4s 74us/step - loss: 0.0510 - acc: 0.9846 - val_loss: 0.0304 - val_acc: 0.9901\n", | |
"\n", | |
"Epoch 5/12\n", | |
"60000/60000 [==============================]60000/60000 [==============================] - 5s 76us/step - loss: 0.0434 - acc: 0.9869 - val_loss: 0.0287 - val_acc: 0.9901\n", | |
"\n", | |
"Epoch 6/12\n", | |
"60000/60000 [==============================]60000/60000 [==============================] - 4s 71us/step - loss: 0.0381 - acc: 0.9881 - val_loss: 0.0287 - val_acc: 0.9900\n", | |
"\n", | |
"Epoch 7/12\n", | |
"60000/60000 [==============================]60000/60000 [==============================] - 5s 77us/step - loss: 0.0341 - acc: 0.9893 - val_loss: 0.0298 - val_acc: 0.9900\n", | |
"\n", | |
"Epoch 8/12\n", | |
"60000/60000 [==============================]60000/60000 [==============================] - 4s 71us/step - loss: 0.0292 - acc: 0.9907 - val_loss: 0.0281 - val_acc: 0.9913\n", | |
"\n", | |
"Epoch 9/12\n", | |
"60000/60000 [==============================]60000/60000 [==============================] - 4s 72us/step - loss: 0.0260 - acc: 0.9914 - val_loss: 0.0310 - val_acc: 0.9909\n", | |
"\n", | |
"Epoch 10/12\n", | |
"60000/60000 [==============================]60000/60000 [==============================] - 5s 81us/step - loss: 0.0247 - acc: 0.9919 - val_loss: 0.0283 - val_acc: 0.9915\n", | |
"\n", | |
"Epoch 11/12\n", | |
"60000/60000 [==============================]60000/60000 [==============================] - 5s 77us/step - loss: 0.0224 - acc: 0.9927 - val_loss: 0.0322 - val_acc: 0.9912\n", | |
"\n", | |
"Epoch 12/12\n", | |
"60000/60000 [==============================]60000/60000 [==============================] - 5s 76us/step - loss: 0.0222 - acc: 0.9925 - val_loss: 0.0293 - val_acc: 0.9919\n", | |
"\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"<tensorflow.python.keras._impl.keras.callbacks.History at 0x7f3ab55df320>" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"model.fit(x_train, y_train,\n", | |
" batch_size=128,\n", | |
" epochs=12,\n", | |
" verbose=1,\n", | |
" validation_data=(x_test, y_test))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Test loss: 0.02925125805952248\n", | |
"Test accuracy: 0.9919\n" | |
] | |
} | |
], | |
"source": [ | |
"score = model.evaluate(x_test, y_test, verbose=0)\n", | |
"print('Test loss: {}\\nTest accuracy: {}'.format(*score))" | |
] | |
} | |
], | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment