Skip to content

Instantly share code, notes, and snippets.

@arokem
Created July 24, 2017 00:33
Show Gist options
  • Select an option

  • Save arokem/ce4e1a1c07495e9d392927ae3a97d2b3 to your computer and use it in GitHub Desktop.

Select an option

Save arokem/ce4e1a1c07495e9d392927ae3a97d2b3 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import numpy as np\n",
"\n",
"I = [1, 0, 2, 3, 0, 1, 1] * 100\n",
"K = [2, 1, 3] * 10\n",
"\n",
"i = tf.constant(I, dtype=tf.float32, name='i')\n",
"k = tf.constant(K, dtype=tf.float32, name='k')\n",
"\n",
"data = tf.reshape(i, [1, int(i.shape[0]), 1], name='data')\n",
"kernel = tf.reshape(k, [int(k.shape[0]), 1, 1], name='kernel')\n",
"\n",
"res = tf.squeeze(tf.nn.conv1d(data, kernel, 1, 'VALID'))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sess = tf.Session()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"109 µs ± 4.34 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"result_tf = sess.run(res)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"76.3 µs ± 1.67 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"result_np = np.convolve(I, K[::-1], 'VALID')"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment