Skip to content

Commit 9140c1e

Browse files
committed
adding convolution on batches
1 parent 51b4b24 commit 9140c1e

File tree

2 files changed

+693
-0
lines changed

2 files changed

+693
-0
lines changed

batch_conv.ipynb

+348
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,348 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {
7+
"collapsed": true,
8+
"deletable": true,
9+
"editable": true
10+
},
11+
"outputs": [],
12+
"source": [
13+
"import numpy as np"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 2,
19+
"metadata": {
20+
"collapsed": false,
21+
"deletable": true,
22+
"editable": true
23+
},
24+
"outputs": [],
25+
"source": [
26+
"batch_size, dim_x, dim_y, colors=2, 10,11,5\n",
27+
"kernel_x, kernel_y, in_colors, out_colors=2,3,colors, 4"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 3,
33+
"metadata": {
34+
"collapsed": false,
35+
"deletable": true,
36+
"editable": true
37+
},
38+
"outputs": [],
39+
"source": [
40+
"im=np.random.rand(batch_size, dim_x, dim_y, colors)\n",
41+
"weights=np.random.rand(kernel_x, kernel_y, in_colors, out_colors)*0.1\n",
42+
"\n",
43+
"debug_weights=np.random.rand(*(batch_size,dim_x-kernel_x+1, dim_y-kernel_y+1, out_colors))*0.1"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 4,
49+
"metadata": {
50+
"collapsed": false,
51+
"deletable": true,
52+
"editable": true
53+
},
54+
"outputs": [
55+
{
56+
"data": {
57+
"text/plain": [
58+
"(2, 10, 11, 5)"
59+
]
60+
},
61+
"execution_count": 4,
62+
"metadata": {},
63+
"output_type": "execute_result"
64+
}
65+
],
66+
"source": [
67+
"im.shape"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": 5,
73+
"metadata": {
74+
"collapsed": true,
75+
"deletable": true,
76+
"editable": true
77+
},
78+
"outputs": [],
79+
"source": [
80+
"def forward(im, weights):\n",
81+
" kernel_x, kernel_y, in_colors, out_colors=weights.shape\n",
82+
" batch_size, dim_x, dim_y, colors=im.shape\n",
83+
" \n",
84+
" \n",
85+
" out=np.empty((batch_size, dim_x-kernel_x+1, dim_y-kernel_y+1, out_colors))\n",
86+
" \n",
87+
" for i in range(out.shape[1]):\n",
88+
" for j in range(out.shape[2]):\n",
89+
" crop=im[:,i:i+kernel_x, j:j+kernel_y]\n",
90+
" #expand crops so the dimensions match\n",
91+
" crop=np.expand_dims(crop, axis=-1)\n",
92+
" \n",
93+
" res=crop*weights\n",
94+
" \n",
95+
" #sum everything except the output\n",
96+
" res=np.apply_over_axes(np.sum, res, [1,2,3]).reshape(batch_size,-1)\n",
97+
" \n",
98+
" out[:,i,j]=res\n",
99+
" \n",
100+
" return out"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 6,
106+
"metadata": {
107+
"collapsed": false,
108+
"deletable": true,
109+
"editable": true
110+
},
111+
"outputs": [],
112+
"source": [
113+
"out=forward(im, weights)"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 7,
119+
"metadata": {
120+
"collapsed": false,
121+
"deletable": true,
122+
"editable": true
123+
},
124+
"outputs": [
125+
{
126+
"data": {
127+
"text/plain": [
128+
"1.1859898389506995"
129+
]
130+
},
131+
"execution_count": 7,
132+
"metadata": {},
133+
"output_type": "execute_result"
134+
}
135+
],
136+
"source": [
137+
"out.max()"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": 8,
143+
"metadata": {
144+
"collapsed": false,
145+
"deletable": true,
146+
"editable": true
147+
},
148+
"outputs": [],
149+
"source": [
150+
"#debug_out=np.apply_over_axes(np.sum, out*debug_weights, [1,2,3])\n",
151+
"debug_out=np.sum(out*debug_weights)"
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": 9,
157+
"metadata": {
158+
"collapsed": false,
159+
"deletable": true,
160+
"editable": true
161+
},
162+
"outputs": [
163+
{
164+
"data": {
165+
"text/plain": [
166+
"25.960989431769082"
167+
]
168+
},
169+
"execution_count": 9,
170+
"metadata": {},
171+
"output_type": "execute_result"
172+
}
173+
],
174+
"source": [
175+
"debug_out"
176+
]
177+
},
178+
{
179+
"cell_type": "code",
180+
"execution_count": 10,
181+
"metadata": {
182+
"collapsed": false,
183+
"deletable": true,
184+
"editable": true
185+
},
186+
"outputs": [],
187+
"source": [
188+
"debug_shape=debug_weights.shape\n",
189+
"\n",
190+
"padded=np.zeros((batch_size, debug_shape[1]+2*(kernel_x-1), debug_shape[2]+2*(kernel_y-1), out_colors))\n",
191+
"padded[:, kernel_x-1:kernel_x-1+debug_shape[1], kernel_y-1:kernel_y-1+debug_shape[2]]=debug_weights"
192+
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": 11,
197+
"metadata": {
198+
"collapsed": false,
199+
"deletable": true,
200+
"editable": true
201+
},
202+
"outputs": [],
203+
"source": [
204+
"import matplotlib.pyplot as plt\n",
205+
"%matplotlib inline"
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": 12,
211+
"metadata": {
212+
"collapsed": false,
213+
"deletable": true,
214+
"editable": true
215+
},
216+
"outputs": [
217+
{
218+
"data": {
219+
"text/plain": [
220+
"<matplotlib.image.AxesImage at 0x7f7102474a90>"
221+
]
222+
},
223+
"execution_count": 12,
224+
"metadata": {},
225+
"output_type": "execute_result"
226+
},
227+
{
228+
"data": {
229+
"image/png": "iVBORw0KGgoAAAANSUhEUgAAASYAAAD8CAYAAADaFgknAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAACx9JREFUeJzt3W+o3YV9x/H3Z7kxadJSO1bKTGTmgTiCdFouna3QB8ZR\n24ruwR4os7TbIE/W1pZCUfagz8ZgpbSw0hHsP2hQRuqYlK7q+ocx2KTXKK1J+kdsq9E4U7a10oGJ\n9LsH97gld4kJ5/czv6/9vV8Qcs/Jyfl98Hrf/s65x3tSVUhSJ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+BfxxyB53C9KqQ5LXAV4APV9Uvpt4DkORG4Lmqenjq\nLWexArwF+GxVXQ38kibvRbh4ruZm1uN5CbA9yW3Trnp553oDkCkl+QvWn/bYP+R+OoXpaeDSUy7v\nXFzXRpLNrEdpf1XdO/WeU1wL3JTkJ6w/BL4uyZennXSao8DRqnrpDPMA66Hq4Hrgx1V1vKpOAvcC\nb59405mc9xuATCXJ+4EbgT+uga9D6hSm7wCXJ9mV5CLWn4C8b+JN/ytJWH+O5EhVfXLqPaeqqjur\namdVXcb6P7dvVlWb/+pX1bPAU0muWFy1Bzg84aRTPQlck2Tb4nO8hyZPzG/Q+g1AktzA+lMJN1XV\nfw+9vzZhWjxx9gHgftb/xfi7qjo07arTXAu8l/WzkUcXv9499ahXkQ8C+5N8F7gK+MuJ9wCwOIs7\nABwEvsf618Skr7Lu/gYgZ9n3N8DrgAcXXxt/O+gYvvJbUjdtzpgk6SWGSVI7hklSO4ZJUjuGSVI7\nhklSO4ZJUjuGSVI7/wPX24/f+sdr0AAAAABJRU5ErkJggg==\n",
230+
"text/plain": [
231+
"<matplotlib.figure.Figure at 0x7f7104766198>"
232+
]
233+
},
234+
"metadata": {},
235+
"output_type": "display_data"
236+
}
237+
],
238+
"source": [
239+
"plt.imshow((padded[0]>0).sum(axis=-1))"
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"execution_count": 13,
245+
"metadata": {
246+
"collapsed": true,
247+
"deletable": true,
248+
"editable": true
249+
},
250+
"outputs": [],
251+
"source": [
252+
"backward_weights=weights[::-1,::-1].transpose((0,1,3,2))"
253+
]
254+
},
255+
{
256+
"cell_type": "code",
257+
"execution_count": 14,
258+
"metadata": {
259+
"collapsed": false,
260+
"deletable": true,
261+
"editable": true
262+
},
263+
"outputs": [],
264+
"source": [
265+
"grads=forward(padded, backward_weights)"
266+
]
267+
},
268+
{
269+
"cell_type": "code",
270+
"execution_count": 15,
271+
"metadata": {
272+
"collapsed": false,
273+
"deletable": true,
274+
"editable": true
275+
},
276+
"outputs": [
277+
{
278+
"data": {
279+
"text/plain": [
280+
"(2, 10, 11, 5)"
281+
]
282+
},
283+
"execution_count": 15,
284+
"metadata": {},
285+
"output_type": "execute_result"
286+
}
287+
],
288+
"source": [
289+
"grads.shape"
290+
]
291+
},
292+
{
293+
"cell_type": "code",
294+
"execution_count": 16,
295+
"metadata": {
296+
"collapsed": false,
297+
"deletable": true,
298+
"editable": true
299+
},
300+
"outputs": [],
301+
"source": [
302+
"eps=1e-4\n",
303+
"tol=1e-8\n",
304+
"\n",
305+
"for idx in np.ndindex(im.shape):\n",
306+
" \n",
307+
" d_im=im.copy()\n",
308+
" d_im[idx]+=eps\n",
309+
" d_out=forward(d_im, weights)\n",
310+
" d_debug_out=np.sum(d_out*debug_weights)\n",
311+
"\n",
312+
" grad=(d_debug_out-debug_out)/eps\n",
313+
" \n",
314+
" assert np.abs(grad-grads[idx])<tol, idx"
315+
]
316+
},
317+
{
318+
"cell_type": "code",
319+
"execution_count": null,
320+
"metadata": {
321+
"collapsed": true
322+
},
323+
"outputs": [],
324+
"source": []
325+
}
326+
],
327+
"metadata": {
328+
"kernelspec": {
329+
"display_name": "Python 3",
330+
"language": "python",
331+
"name": "python3"
332+
},
333+
"language_info": {
334+
"codemirror_mode": {
335+
"name": "ipython",
336+
"version": 3
337+
},
338+
"file_extension": ".py",
339+
"mimetype": "text/x-python",
340+
"name": "python",
341+
"nbconvert_exporter": "python",
342+
"pygments_lexer": "ipython3",
343+
"version": "3.5.2"
344+
}
345+
},
346+
"nbformat": 4,
347+
"nbformat_minor": 2
348+
}

0 commit comments

Comments
 (0)