Skip to content

Instantly share code, notes, and snippets.

@twiecki
Last active January 5, 2019 13:24
Show Gist options
  • Save twiecki/43d6b78455ef5812bb90b5522fe7686c to your computer and use it in GitHub Desktop.
Save twiecki/43d6b78455ef5812bb90b5522fe7686c to your computer and use it in GitHub Desktop.
theano_tensorflow_speed_comparison
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import numpy as np\n",
"import theano\n",
"import theano.tensor as tt\n",
"\n",
"tf.enable_eager_execution()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.13.0-dev20190104'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tf.__version__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.0.3'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"theano.__version__"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"x = np.random.randn(100, 100)\n",
"y = np.random.randn(100, 100)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"@tf.contrib.eager.defun\n",
"def f_tf_defun(x, y):\n",
" return tf.reduce_mean(tf.multiply(x ** 2, 3) + y)\n",
"\n",
"def f_tf(x, y):\n",
" return tf.reduce_mean(tf.multiply(x ** 2, 3) + y)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<tf.Tensor: id=24420, shape=(), dtype=float64, numpy=3.06705004392501>"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f_tf_defun(x, y)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"212 µs ± 31.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"f_tf_defun(x, y)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"84.4 µs ± 3.7 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"f_tf(x, y)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"x_t = tt.dmatrix()\n",
"y_t = tt.dmatrix()\n",
"expr = tt.mean(tt.mul(x_t ** 2, 3) + y_t)\n",
"\n",
"f_theano = theano.function([x_t, y_t], expr)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"34.7 µs ± 1.58 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"f_theano(x, y)"
]
}
],
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment