Skip to content

Instantly share code, notes, and snippets.

@analyticsindiamagazine
Last active June 26, 2018 06:26
Show Gist options
  • Save analyticsindiamagazine/4ae1f5dcf7ab3a839e6d10e6a4ee6089 to your computer and use it in GitHub Desktop.
Save analyticsindiamagazine/4ae1f5dcf7ab3a839e6d10e6a4ee6089 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tensorflow MatMul on Intel i5-4210U"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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: 16911156579215077165\n",
"]\n"
]
}
],
"source": [
"from tensorflow.python.client import device_lib\n",
"print(device_lib.list_local_devices())"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" 8000 x 8000 matmul took: 16.16 sec, 63.36 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(\"/cpu: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",
"\n",
"config = tf.ConfigProto(graph_options=tf.GraphOptions(optimizer_options=tf.OptimizerOptions(opt_level=tf.OptimizerOptions.L0)))\n",
"sess = tf.Session(config=config)\n",
"\n",
"sess.run(tf.global_variables_initializer())\n",
"iters = 10\n",
"\n",
"sess.run(product.op)\n",
"#file_writer = tf.summary.FileWriter('/path/to/logs', sess.graph)\n",
"start = time.time()\n",
"for i in range(iters):\n",
" sess.run(product.op)\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