Skip to content

Instantly share code, notes, and snippets.

@entron
Created September 18, 2015 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save entron/e4778d9c0b0d241d8c91 to your computer and use it in GitHub Desktop.
Save entron/e4778d9c0b0d241d8c91 to your computer and use it in GitHub Desktop.
Show different convolution results between numpy and theano
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import theano\n",
"import numpy\n",
"from theano.sandbox.cuda import dnn\n",
"import theano.tensor as T"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the input image x0:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x0 = numpy.array([[[[ 7.61323881, 0. , 0. , 0. ,\n",
" 0. , 0. ],\n",
" [ 25.58142853, 0. , 0. , 0. ,\n",
" 0. , 0. ],\n",
" [ 7.51445341, 0. , 0. , 0. ,\n",
" 0. , 0. ],\n",
" [ 0. , 12.74498367, 4.96315479, 0. ,\n",
" 0. , 0. ],\n",
" [ 0. , 0. , 0. , 0. ,\n",
" 0. , 0. ],\n",
" [ 0. , 0. , 0. , 0. ,\n",
" 0. , 0. ]]]], dtype='float32')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(1, 1, 6, 6)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x0.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the convolution kernel:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"w0 = numpy.array([[[[-0.0015835 , -0.00088091, 0.00226375, 0.00378434, 0.00032208,\n",
" -0.00396959],\n",
" [-0.000179 , 0.00030951, 0.00113849, 0.00012536, -0.00017198,\n",
" -0.00318825],\n",
" [-0.00263921, -0.00383847, -0.00225416, -0.00250589, -0.00149073,\n",
" -0.00287099],\n",
" [-0.00149283, -0.00312137, -0.00431571, -0.00394508, -0.00165113,\n",
" -0.0012118 ],\n",
" [-0.00167376, -0.00169753, -0.00373235, -0.00337372, -0.00025546,\n",
" 0.00072154],\n",
" [-0.00141197, -0.00099017, -0.00091934, -0.00226817, -0.0024105 ,\n",
" -0.00333713]]]], dtype='float32')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(1, 1, 6, 6)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"w0.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Caculate the convolution with theano and cudnn:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[[-0.04749081]]]], dtype=float32)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X = T.tensor4('input')\n",
"W = T.tensor4('W')\n",
"conv_out = dnn.dnn_conv(img=X, kerns=W)\n",
"convolution = theano.function([X, W], conv_out)\n",
"numpy.array(convolution(x0, w0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Calculate convolutoin with numpy (note the result is different):"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"-0.097668208"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numpy.sum(x0 * w0)"
]
}
],
"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.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment