tensorflow-keras-cpu-gpu/01. Matrix multiplication.ipynb

287 lines
27 KiB
Plaintext
Raw Normal View History

2018-01-18 09:41:18 +01:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"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"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"# Devices\n",
"\n",
"lists the devices found by tensorflow"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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 0x7f58b3ab14a8>"
]
},
"execution_count": 2,
"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": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"# Use all devices once such that they are initialized"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"for device in devices:\n",
" with tf.device(device.name):\n",
" c = tf.constant(5.0)\n",
" x = tf.placeholder(tf.float32)\n",
" product = c * x\n",
"\n",
" with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as session:\n",
" result = session.run(product, feed_dict={x: 5.0})\n",
" assert result == 25.0, \"c * x for x == 5.0 should result in 25.0, but resulted in {} on device {} ({})\".format(\n",
" result, device.name, device.device_type)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Benchmark"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"start /device:CPU:0 10\n",
"start /device:GPU:0 10\n",
"start /device:CPU:0 20\n",
"start /device:GPU:0 20\n",
"start /device:CPU:0 50\n",
"start /device:GPU:0 50\n",
"start /device:CPU:0 100\n",
"start /device:GPU:0 100\n",
"start /device:CPU:0 1000\n",
"start /device:GPU:0 1000\n",
"start /device:CPU:0 5000\n",
"start /device:GPU:0 5000\n",
"start /device:CPU:0 10000\n",
"start /device:GPU:0 10000\n"
]
}
],
"source": [
"results = defaultdict(list)\n",
"\n",
"matrix_sizes = list(map(int, [10, 20, 50, 100, 1000, 5000, 1e4]))\n",
"\n",
"for size in ProgressBar(matrix_sizes):\n",
" for device in devices:\n",
" print('start', device.name, size)\n",
" with tf.device(device.name):\n",
" A = tf.random_uniform(shape=(size, size), minval=0, maxval=1, dtype=tf.float32)\n",
" B = tf.random_uniform(shape=(size, size), minval=0, maxval=1, dtype=tf.float32)\n",
" product = tf.matmul(A, B)\n",
" \n",
" with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as session:\n",
" t0 = time.time()\n",
" result = session.run(product)\n",
" t1 = time.time()\n",
" results[device.name].append(t1 - t0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Results"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.text.Text at 0x7f58b018b710>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYwAAAEPCAYAAABRHfM8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XeYVOX5//H3Ta+7LAiIdMQSCyLWXGhcRFQ0sYIFK5ZYozExavRrIJoGJrHFkhDUWLCgoCgQVGRtPzGKEBCxQAAREYSlC7Ll/v1xZpfZndllWOacmd39vK5rLmbOM3POPWeHuecp53nM3REREdmRBpkOQEREagclDBERSYkShoiIpEQJQ0REUqKEISIiKVHCEBGRlISaMMysi5m9YWbzzWyemf0stn2EmX1lZh/FbieGGYeIiOw6C/M6DDPbHdjd3eeYWStgFnAqcDaw0d3/GtrBRUQkrRqFuXN3/wb4JnZ/k5ktADrHii3MY4uISHpF1odhZj2AvsD7sU3XmNkcM/unmeVGFYeIiNRMJAkj1hz1PHC9u28CHgT2dPe+BDUQNU2JiGS5UPswAMysEfAKMNXd701S3h142d37JCnTRFciIjXg7mlv9o+ihvEI8El8soh1hpc5A/i4qhe7u27ujBgxIuMxZMtN50LnQuei+ltYQu30NrP+wHnAPDObDThwKzDMzPoCpcAS4Iow4xARkV0X9iipd4GGSYr+HeZxRUQk/XSldy2Rn5+f6RCyhs7FdjoX2+lchC/0Tu9dYWaezfGJiGQjM8ND6PQOtUlKRMLXo0cPli5dmukwJAO6d+/OkiVLIjueahgitVzs12Smw5AMqOpvH1YNQ30YIiKSEiUMERFJiRKGiIikRAlDRDJm+PDh/OY3v9nl/Zx00kk88cQTaYhIqqOEISKhWrFiBV27dg31GFOmTOGCCy5I+34///xzzjrrLNq3b09eXh59+/bl7rvvxt1ZunQpDRo0ICcnh5ycHHr16sWoUaMAystKS0sr7G9HCXLcuHH06NGD1q1bc8YZZ7Bu3bq0v6ddoYQhIqGaMmUKgwcPznQYO23RokUceeSRdO/enY8//pi1a9cyfvx4Zs2axcaNG4FgNNL69evZsGED48aN44477uDVV18tL9sZ8+fP58orr+Spp55i5cqVNG/enKuuuirt72tXKGGI1FVm4dx20pQpUzjppJMAmD17Nocccgi5ubmcc845bN26tcJzX3nlFQ4++GDy8vI46qijmDdvHgCjR49m6NChFZ57/fXX8/Of/xyAAQMG8Mgjj5SXjRkzhv3224+cnBwOOOAA5syZAwS1nSFDhtChQwf23HNP7r///irjHjlyJP379+euu+6iY8eOAOy11148+eST5OTklD+vbFjrkUceyf7778/HH1c5l2q1xo0bxymnnEL//v1p0aIFd955JxMmTGDz5s012l8YlDBEJDTFxcW89dZbDBo0iKKiIk4//XQuuugiCgsLGTp0KC+88EL5c2fPns2ll17KmDFjKCws5IorruCUU06hqKiIc845h6lTp5Z/eZaWljJ+/HjOO++8hGOOHz+eO+64gyeffJINGzYwadIk2rVrh7vzk5/8hIMPPpgVK1Ywffp07r33Xl577TUA3n33Xdq2bVu+n9dff50hQ4bs8D2WJYx3332XTz75hH79+qV0bpYtW0bbtm356quvgKCGcdBBB5WX9+rViyZNmvD555+ntL8oKGGISGjeeust+vbtS8uWLXnvvfcoLi7muuuuo2HDhpx55pkcdthh5c8dM2YMV155JYceeihmxgUXXEDTpk2ZOXMm3bp1o1+/fkycOBGA6dOn07JlywqvLzN27Fhuuumm8i/uXr160bVrVz744ANWr17NbbfdRsOGDenRoweXXXYZzzzzDAD9+/ensLCwfD9r1qyhU6dO1b4/d6d9+/a0a9eOn/70p4waNSrlOa26du1KYWEhXbp0AWDTpk3k5lZcfDQ3N7e8+SsbaGoQEQlNfHPUihUr6Ny5c4Xy7t27l99funQpjz/+eHkzkbtTVFTE119/DcC5557L008/zfnnn8/TTz/NsGHDkh5z2bJl7Lnnngnbly5dyvLly8trEe5OaWkpP/rRj5Lup127dqxYsaLa92dmrFmzJqG/olGj4Ku1qKiIpk2blm8vKiqicePGSffVqlUrNmzYUGHbhg0baN26dbUxREk1DBEJTXzC6NSpE8uXL69Q/uWXX5bf79q1K7fddhuFhYUUFhaydu1aNm3axNlnnw3A0KFDKSgoYPny5UycOLHKhNG1a1cWLVqUdHuvXr0q7H/9+vW8/PLLSfdz3HHHVWgyq0qyqTk6depE48aNE+Z5Wrx4cYUkGW///ffnv//9b/nj//3vf2zbto299957hzFUEOa8YpleGWoHq0a5iFQvW/+fLF682Pfcc8/yx9u2bfPu3bv7fffd50VFRf7CCy9448aN/fbbb3d39w8//NC7devm77//vru7b9q0ySdPnuybNm0q38fgwYN90KBB3q9fvwrHys/P97Fjx7q7+/jx471bt24+a9Ysd3dfuHChf/nll15SUuKHHHKIjxo1yrds2eLFxcX+8ccf+wcffJA0/kWLFnm7du38pptu8m+++cbd3b/44gs///zzff369b5kyRI3My8pKUn6+mHDhvmZZ57pa9as8aKiIh83bpzn5eX5qlWrkj5//vz5npub6++8845v2rTJzz//fB82bFi15zjhb//BB+6tW5dtT/t3smoYIhKKyZMnl9cuABo3bsyECRN49NFHadeuHePHj+fMM88sLz/kkEMYM2YM1157LW3btmXvvffmX//6V4V9Dhs2jOnTpyd0dsc3CQ0ZMoTbbruNYcOGkZOTw+mnn05hYSENGjTglVdeYc6cOfTs2ZMOHTpw+eWXlzcDvfPOOxVGP/Xq1Yv33nuPxYsXs//++5OXl8fQoUM57LDDypuJqhs6++CDD9K2bVv69OlDx44defDBB5kyZQrt27cHgqaznJyc8k7v/fbbj4cffphhw4ax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ib.figure.Figure at 0x7f58b025e390>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"colors = 'rgbcmyk'\n",
"marks = ['-', '--', '..']\n",
"\n",
"styles = (color+mark for mark in marks for color in colors)\n",
"\n",
"for style, (name, times) in zip(styles, results.items()):\n",
" pyplot.plot(matrix_sizes, times, style, label=name)\n",
"\n",
"pyplot.legend()\n",
"\n",
"pyplot.ylabel('Calculation time')\n",
"pyplot.xlabel('Matrix size')"
]
},
{
"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;text-align:right;\"><b>matrix&nbsp;size</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">time&nbsp;(s)&nbsp;on&nbsp;<b>/device:CPU:0</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">time&nbsp;(s)&nbsp;on&nbsp;<b>/device:GPU:0</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>10</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">0.0054</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">0.1705</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>20</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">0.0045</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">0.0059</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>50</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">0.0053</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">0.0081</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>100</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">0.0103</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">0.0081</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>1000</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">0.0437</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">0.0106</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>5000</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">2.8766</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">0.2573</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>10000</b></td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">22.4699</td><td style=\"border-left: 1px solid;border-right: 1px solid;border-top: 1px solid;border-bottom: 1px solid;\">1.6363</td></tr></table>"
],
"text/plain": [
"<ipy_table.ipy_table.IpyTable at 0x7f58b021afd0>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"devices = device_lib.list_local_devices()\n",
"\n",
"\n",
"make_table([[\"matrix size\"] + [\"time (s) on <b>{}</b>\".format(device.name) for device in devices]] + [\n",
" [size] + [results[device.name][size_index] for device in devices]\n",
" for size_index, size in enumerate(matrix_sizes)\n",
"])\n",
"set_column_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"
},
"widgets": {
"state": {
"7ba735bb37af497cac93b37f587e1d16": {
"views": [
{
"cell_index": 5
}
]
}
},
"version": "1.2.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}