tensorflow-keras-cpu-gpu/02. Simple Neural Network t...

189 lines
10 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"import matplotlib\n",
"%matplotlib inline\n",
"matplotlib.rcParams['figure.figsize'] = (20, 10)\n",
"matplotlib.rcParams['font.size'] = 24\n",
"matplotlib.rcParams['lines.linewidth'] = 5\n",
"matplotlib.rcParams['lines.markersize'] = 20\n",
"\n",
"import tensorflow as tf\n",
"from tensorflow.python.client import device_lib\n",
"\n",
"import time\n",
"import numpy as np\n",
"from matplotlib import pyplot\n",
"from collections import defaultdict\n",
"from ipy_table import make_table, set_row_style, set_column_style\n",
"from jupyter_progressbar import ProgressBar\n",
"\n",
"from sklearn.preprocessing import LabelEncoder\n",
"from sklearn.model_selection import train_test_split\n",
"from keras.models import Sequential\n",
"from keras.layers import Activation\n",
"from keras.optimizers import SGD, Adam\n",
"from keras.layers import Dense, Dropout, Flatten\n",
"from keras.regularizers import l1, l2\n",
"from keras import regularizers\n",
"import numpy\n",
"import pickle\n",
"import tensorflow as tf"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/html": [
"<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" style=\"border:black; border-collapse:collapse;\"><tr><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\"><b>name</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\"><b>type</b></td></tr><tr><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">/device:CPU:0</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">CPU</td></tr><tr><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">/device:GPU:0</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">GPU</td></tr></table>"
],
"text/plain": [
"<ipy_table.ipy_table.IpyTable at 0x7f8d84227cc0>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"devices = device_lib.list_local_devices()\n",
"\n",
"make_table([[\"name\", \"type\"]] + [\n",
" [device.name, device.device_type]\n",
" for device in devices\n",
"])\n",
"set_row_style(0, bold=True)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6250/6250 [==============================] - 1s 167us/step\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b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b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\n"
]
}
],
"source": [
"results = defaultdict(dict)\n",
"\n",
"for device in devices:\n",
" with tf.device(device.name):\n",
" with open(\"CatsAndDogs.pickle\", 'rb') as f:\n",
" data = pickle.load(f)\n",
"\n",
" X = data[\"dataset\"]\n",
" y = data[\"labels\"]\n",
" del data\n",
"\n",
" (X_train, X_test, y_train, y_test) = train_test_split(\n",
" X, y, test_size = 0.25, random_state = 42)\n",
"\n",
" model = Sequential()\n",
" model.add(Flatten(input_shape=X_train.shape[1:]))\n",
"\n",
" model.add(Dense(768, kernel_initializer=\"uniform\", activation=\"relu\"))\n",
" model.add(Dropout(0.2))\n",
"\n",
" model.add(Dense(384, kernel_initializer=\"uniform\", activation=\"relu\"))\n",
" model.add(Dropout(0.2))\n",
"\n",
" model.add(Dense(128, kernel_initializer=\"uniform\", activation=\"relu\"))\n",
" model.add(Dropout(0.2))\n",
"\n",
" model.add(Dense(2, activation=\"softmax\"))\n",
"\n",
" model.compile(loss=\"binary_crossentropy\", optimizer=Adam(lr=0.001), metrics=[\"accuracy\"])\n",
" \n",
" t0 = time.time()\n",
" model.fit(X_train, y_train, validation_split=0.25, epochs=5, batch_size=128, verbose=0)\n",
" t1 = time.time()\n",
" (loss, accuracy) = model.evaluate(X_test, y_test, verbose=0)\n",
" t2 = time.time()\n",
" \n",
" results[device.name]['training'] = t1 - t0\n",
" results[device.name]['evaluating'] = t2 - t1"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" style=\"border:black; border-collapse:collapse;\"><tr><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;text-align:right;\"><b>device</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;text-align:right;\"><b>execution&nbsp;time&nbsp;(s)&nbsp;training</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;text-align:right;\"><b>execution&nbsp;time&nbsp;(s)&nbsp;evaluating</b></td></tr><tr><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;text-align:right;\"><b>/device:GPU:0</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">8.3381</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">0.3781</td></tr><tr><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;text-align:right;\"><b>/device:CPU:0</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">38.5678</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">1.0451</td></tr></table>"
],
"text/plain": [
"<ipy_table.ipy_table.IpyTable at 0x7f8d7faa7b00>"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"make_table([[\"device\", \"execution time (s) training\", \"execution time (s) evaluating\"]] + [\n",
" [device, result['training'], result['evaluating']]\n",
" for device, result in results.items()\n",
"])\n",
"set_column_style(0, bold=True, align='right')\n",
"set_row_style(0, bold=True, align='right')"
]
}
],
"metadata": {
"hide_input": false,
"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
}