Last active
May 8, 2019 22:20
-
-
Save analyticsindiamagazine/e129ab277290a58d2fd52cd5b38ce1df to your computer and use it in GitHub Desktop.
This file contains hidden or 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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Tensorflow MatMul - GeForce Nvidia 1060 6GB" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[name: \"/device:CPU:0\"\n", | |
"device_type: \"CPU\"\n", | |
"memory_limit: 268435456\n", | |
"locality {\n", | |
"}\n", | |
"incarnation: 11791682687431765861\n", | |
", name: \"/device:GPU:0\"\n", | |
"device_type: \"GPU\"\n", | |
"memory_limit: 4971180851\n", | |
"locality {\n", | |
" bus_id: 1\n", | |
" links {\n", | |
" }\n", | |
"}\n", | |
"incarnation: 10419784556444299121\n", | |
"physical_device_desc: \"device: 0, name: GeForce GTX 1060 6GB, pci bus id: 0000:01:00.0, compute capability: 6.1\"\n", | |
"]\n" | |
] | |
} | |
], | |
"source": [ | |
"from tensorflow.python.client import device_lib\n", | |
"print(device_lib.list_local_devices())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
" 8000 x 8000 matmul took: 0.29 sec, 3588.87 G ops/sec\n" | |
] | |
} | |
], | |
"source": [ | |
"import os\n", | |
"import sys\n", | |
"os.environ[\"CUDA_VISIBLE_DEVICES\"]=\"1\"\n", | |
"import tensorflow as tf\n", | |
"import time\n", | |
"\n", | |
"n = 8000\n", | |
"dtype = tf.float32\n", | |
"with tf.device(\"/GPU:0\"):\n", | |
" matrix1 = tf.Variable(tf.ones((n, n), dtype=dtype))\n", | |
" matrix2 = tf.Variable(tf.ones((n, n), dtype=dtype))\n", | |
" product = tf.matmul(matrix1, matrix2)\n", | |
"\n", | |
"config = tf.ConfigProto(graph_options=tf.GraphOptions(optimizer_options=tf.OptimizerOptions(opt_level=tf.OptimizerOptions.L0)))\n", | |
"\n", | |
"with tf.Session(config=config) as sess1:\n", | |
" sess1.run(tf.global_variables_initializer())\n", | |
" iters = 10\n", | |
" start = time.time()\n", | |
" for i in range(iters):\n", | |
" sess1.run(product) \n", | |
"end = time.time()\n", | |
"ops = n**3 + (n-1)*n**2 # n^2*(n-1) additions, n^3 multiplications\n", | |
"elapsed = (end - start)\n", | |
"rate = iters*ops/elapsed/10**9\n", | |
"print('\\n %d x %d matmul took: %.2f sec, %.2f G ops/sec' % (n, n,\n", | |
" elapsed/iters,\n", | |
" rate,))" | |
] | |
} | |
], | |
"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.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment