Skip to content

Instantly share code, notes, and snippets.

@huseinzol05
Created February 18, 2018 03:29
Show Gist options
  • Save huseinzol05/472d51d356b88195d1d00984cb774559 to your computer and use it in GitHub Desktop.
Save huseinzol05/472d51d356b88195d1d00984cb774559 to your computer and use it in GitHub Desktop.
convolution-2d
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"x = np.ones((1,7,7,3))\n",
"w = np.ones((3,3,3,7))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"rf = w.shape[0]\n",
"nf = w.shape[3]\n",
"s = 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"h_range = int((x.shape[1] - rf) / s) + 1 \n",
"w_range = int((x.shape[2] - rf) / s) + 1\n",
"i0 = np.repeat(np.arange(rf), rf)\n",
"i0 = np.tile(i0, x.shape[3])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1,\n",
" 1, 2, 2, 2])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"i0"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"i1 = s * np.repeat(np.arange(h_range), w_range)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 0, 0, 2, 2, 2, 4, 4, 4])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"i1"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"i=i0.reshape(-1, 1) + i1.reshape(1, -1)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0, 0, 2, 2, 2, 4, 4, 4],\n",
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n",
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n",
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n",
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n",
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n",
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n",
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n",
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n",
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n",
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n",
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n",
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n",
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n",
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n",
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n",
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n",
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n",
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n",
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n",
" [0, 0, 0, 2, 2, 2, 4, 4, 4],\n",
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n",
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n",
" [1, 1, 1, 3, 3, 3, 5, 5, 5],\n",
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n",
" [2, 2, 2, 4, 4, 4, 6, 6, 6],\n",
" [2, 2, 2, 4, 4, 4, 6, 6, 6]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"i"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1,\n",
" 2, 0, 1, 2])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"j0 = np.tile(np.arange(rf), rf * x.shape[3])\n",
"j0"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 2, 4, 0, 2, 4, 0, 2, 4])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"j1 = s * np.tile(np.arange(w_range), h_range)\n",
"j1"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 2, 4, 0, 2, 4, 0, 2, 4],\n",
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n",
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n",
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n",
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n",
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n",
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n",
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n",
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n",
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n",
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n",
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n",
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n",
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n",
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n",
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n",
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n",
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n",
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n",
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n",
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n",
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n",
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n",
" [2, 4, 6, 2, 4, 6, 2, 4, 6],\n",
" [0, 2, 4, 0, 2, 4, 0, 2, 4],\n",
" [1, 3, 5, 1, 3, 5, 1, 3, 5],\n",
" [2, 4, 6, 2, 4, 6, 2, 4, 6]])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"j = j0.reshape(-1, 1) + j1.reshape(1, -1)\n",
"j"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0],\n",
" [0],\n",
" [0],\n",
" [0],\n",
" [0],\n",
" [0],\n",
" [0],\n",
" [0],\n",
" [0],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [2],\n",
" [2],\n",
" [2],\n",
" [2],\n",
" [2],\n",
" [2],\n",
" [2],\n",
" [2],\n",
" [2]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k = np.repeat(np.arange(x.shape[3]), rf * rf).reshape(-1, 1)\n",
"k"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(27, 9)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[:,i,j,k].reshape((rf * rf * x.shape[3], -1)).shape"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 27., 27., 27., 27., 27., 27., 27., 27., 27.],\n",
" [ 27., 27., 27., 27., 27., 27., 27., 27., 27.],\n",
" [ 27., 27., 27., 27., 27., 27., 27., 27., 27.],\n",
" [ 27., 27., 27., 27., 27., 27., 27., 27., 27.],\n",
" [ 27., 27., 27., 27., 27., 27., 27., 27., 27.],\n",
" [ 27., 27., 27., 27., 27., 27., 27., 27., 27.],\n",
" [ 27., 27., 27., 27., 27., 27., 27., 27., 27.]])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"output = w.reshape((nf,-1)).dot(x[:,i,j,k].reshape((rf * rf * x.shape[3], -1)))\n",
"output"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[[ 27., 27., 27., 27., 27., 27., 27.],\n",
" [ 27., 27., 27., 27., 27., 27., 27.],\n",
" [ 27., 27., 27., 27., 27., 27., 27.]],\n",
"\n",
" [[ 27., 27., 27., 27., 27., 27., 27.],\n",
" [ 27., 27., 27., 27., 27., 27., 27.],\n",
" [ 27., 27., 27., 27., 27., 27., 27.]],\n",
"\n",
" [[ 27., 27., 27., 27., 27., 27., 27.],\n",
" [ 27., 27., 27., 27., 27., 27., 27.],\n",
" [ 27., 27., 27., 27., 27., 27., 27.]]]])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"output.reshape((-1, h_range, w_range, nf))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 3, 3, 7)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"output.reshape((-1, h_range, w_range, nf)).shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.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