Skip to content

Commit 871586a

Browse files
committed
added some notebooks to study interactively
1 parent 8e9db1a commit 871586a

File tree

2 files changed

+411
-0
lines changed

2 files changed

+411
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Convolutional Neural Networks (LeNet)"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {
14+
"collapsed": true
15+
},
16+
"outputs": [],
17+
"source": [
18+
"import theano\n",
19+
"from theano import tensor as T\n",
20+
"from theano.tensor.nnet import conv\n",
21+
"\n",
22+
"import numpy\n",
23+
"\n",
24+
"rng = numpy.random.RandomState(23455)\n",
25+
"\n",
26+
"# instantiate 4D tensor for input\n",
27+
"input = T.tensor4(name='input')\n",
28+
"\n",
29+
"# initialize shared variable for weights.\n",
30+
"w_shp = (2, 3, 9, 9)\n",
31+
"w_bound = numpy.sqrt(3 * 9 * 9)\n",
32+
"W = theano.shared( numpy.asarray(\n",
33+
" rng.uniform(\n",
34+
" low=-1.0 / w_bound,\n",
35+
" high=1.0 / w_bound,\n",
36+
" size=w_shp),\n",
37+
" dtype=input.dtype), name ='W')\n",
38+
"\n",
39+
"# initialize shared variable for bias (1D tensor) with random values\n",
40+
"# IMPORTANT: biases are usually initialized to zero. However in this\n",
41+
"# particular application, we simply apply the convolutional layer to\n",
42+
"# an image without learning the parameters. We therefore initialize\n",
43+
"# them to random values to \"simulate\" learning.\n",
44+
"b_shp = (2,)\n",
45+
"b = theano.shared(numpy.asarray(\n",
46+
" rng.uniform(low=-.5, high=.5, size=b_shp),\n",
47+
" dtype=input.dtype), name ='b')\n",
48+
"\n",
49+
"# build symbolic expression that computes the convolution of input with filters in w\n",
50+
"conv_out = conv.conv2d(input, W)\n",
51+
"\n",
52+
"# build symbolic expression to add bias and apply activation function, i.e. produce neural net layer output\n",
53+
"# A few words on ``dimshuffle`` :\n",
54+
"# ``dimshuffle`` is a powerful tool in reshaping a tensor;\n",
55+
"# what it allows you to do is to shuffle dimension around\n",
56+
"# but also to insert new ones along which the tensor will be\n",
57+
"# broadcastable;\n",
58+
"# dimshuffle('x', 2, 'x', 0, 1)\n",
59+
"# This will work on 3d tensors with no broadcastable\n",
60+
"# dimensions. The first dimension will be broadcastable,\n",
61+
"# then we will have the third dimension of the input tensor as\n",
62+
"# the second of the resulting tensor, etc. If the tensor has\n",
63+
"# shape (20, 30, 40), the resulting tensor will have dimensions\n",
64+
"# (1, 40, 1, 20, 30). (AxBxC tensor is mapped to 1xCx1xAxB tensor)\n",
65+
"# More examples:\n",
66+
"# dimshuffle('x') -> make a 0d (scalar) into a 1d vector\n",
67+
"# dimshuffle(0, 1) -> identity\n",
68+
"# dimshuffle(1, 0) -> inverts the first and second dimensions\n",
69+
"# dimshuffle('x', 0) -> make a row out of a 1d vector (N to 1xN)\n",
70+
"# dimshuffle(0, 'x') -> make a column out of a 1d vector (N to Nx1)\n",
71+
"# dimshuffle(2, 0, 1) -> AxBxC to CxAxB\n",
72+
"# dimshuffle(0, 'x', 1) -> AxB to Ax1xB\n",
73+
"# dimshuffle(1, 'x', 0) -> AxB to Bx1xA\n",
74+
"output = T.nnet.sigmoid(conv_out + b.dimshuffle('x', 0, 'x', 'x'))\n",
75+
"\n",
76+
"# create theano function to compute filtered images\n",
77+
"f = theano.function([input], output)"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 3,
83+
"metadata": {
84+
"collapsed": false
85+
},
86+
"outputs": [],
87+
"source": [
88+
"import numpy\n",
89+
"import pylab\n",
90+
"from PIL import Image\n",
91+
"\n",
92+
"# open random image of dimensions 639x516\n",
93+
"img = Image.open(open('../doc/images/3wolfmoon.jpg'))\n",
94+
"# dimensions are (height, width, channel)\n",
95+
"img = numpy.asarray(img, dtype='float64') / 256.\n",
96+
"\n",
97+
"# put image in 4D tensor of shape (1, 3, height, width)\n",
98+
"img_ = img.transpose(2, 0, 1).reshape(1, 3, 639, 516)\n",
99+
"filtered_img = f(img_)\n",
100+
"\n",
101+
"# plot original image and first and second components of output\n",
102+
"pylab.subplot(1, 3, 1); pylab.axis('off'); pylab.imshow(img)\n",
103+
"pylab.gray();\n",
104+
"# recall that the convOp output (filtered image) is actually a \"minibatch\",\n",
105+
"# of size 1 here, so we take index 0 in the first dimension:\n",
106+
"pylab.subplot(1, 3, 2); pylab.axis('off'); pylab.imshow(filtered_img[0, 0, :, :])\n",
107+
"pylab.subplot(1, 3, 3); pylab.axis('off'); pylab.imshow(filtered_img[0, 1, :, :])\n",
108+
"pylab.show()"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": 5,
114+
"metadata": {
115+
"collapsed": false
116+
},
117+
"outputs": [
118+
{
119+
"data": {
120+
"text/plain": [
121+
"array([[[ 0.0859375 , 0.0546875 , 0.10546875],\n",
122+
" [ 0.109375 , 0.0859375 , 0.125 ],\n",
123+
" [ 0.08984375, 0.0859375 , 0.109375 ],\n",
124+
" ..., \n",
125+
" [ 0.07421875, 0.06640625, 0.0703125 ],\n",
126+
" [ 0.05859375, 0.0625 , 0.078125 ],\n",
127+
" [ 0.0703125 , 0.08203125, 0.1015625 ]],\n",
128+
"\n",
129+
" [[ 0.09375 , 0.06640625, 0.09765625],\n",
130+
" [ 0.10546875, 0.0859375 , 0.109375 ],\n",
131+
" [ 0.0859375 , 0.0859375 , 0.09375 ],\n",
132+
" ..., \n",
133+
" [ 0.05859375, 0.04296875, 0.046875 ],\n",
134+
" [ 0.05859375, 0.0546875 , 0.07421875],\n",
135+
" [ 0.06640625, 0.0703125 , 0.08984375]],\n",
136+
"\n",
137+
" [[ 0.109375 , 0.0859375 , 0.09375 ],\n",
138+
" [ 0.109375 , 0.09375 , 0.09765625],\n",
139+
" [ 0.0859375 , 0.0859375 , 0.0859375 ],\n",
140+
" ..., \n",
141+
" [ 0.0703125 , 0.046875 , 0.0546875 ],\n",
142+
" [ 0.09375 , 0.07421875, 0.09765625],\n",
143+
" [ 0.06640625, 0.0625 , 0.0859375 ]],\n",
144+
"\n",
145+
" ..., \n",
146+
" [[ 0.06640625, 0.07421875, 0.03125 ],\n",
147+
" [ 0.08203125, 0.09375 , 0.05859375],\n",
148+
" [ 0.05078125, 0.08203125, 0.0390625 ],\n",
149+
" ..., \n",
150+
" [ 0.06640625, 0.0625 , 0.0859375 ],\n",
151+
" [ 0.078125 , 0.07421875, 0.09765625],\n",
152+
" [ 0.0859375 , 0.08203125, 0.10546875]],\n",
153+
"\n",
154+
" [[ 0.046875 , 0.05078125, 0.01953125],\n",
155+
" [ 0.05078125, 0.07421875, 0.03515625],\n",
156+
" [ 0.0390625 , 0.06640625, 0.03515625],\n",
157+
" ..., \n",
158+
" [ 0.05859375, 0.0625 , 0.0703125 ],\n",
159+
" [ 0.06640625, 0.0703125 , 0.078125 ],\n",
160+
" [ 0.0703125 , 0.07421875, 0.08203125]],\n",
161+
"\n",
162+
" [[ 0.046875 , 0.05859375, 0.0234375 ],\n",
163+
" [ 0.04296875, 0.06640625, 0.02734375],\n",
164+
" [ 0.0234375 , 0.0625 , 0.02734375],\n",
165+
" ..., \n",
166+
" [ 0.0703125 , 0.078125 , 0.06640625],\n",
167+
" [ 0.06640625, 0.07421875, 0.0625 ],\n",
168+
" [ 0.05078125, 0.05859375, 0.046875 ]]])"
169+
]
170+
},
171+
"execution_count": 5,
172+
"metadata": {},
173+
"output_type": "execute_result"
174+
}
175+
],
176+
"source": [
177+
"img"
178+
]
179+
}
180+
],
181+
"metadata": {
182+
"kernelspec": {
183+
"display_name": "Python 2",
184+
"language": "python",
185+
"name": "python2"
186+
},
187+
"language_info": {
188+
"codemirror_mode": {
189+
"name": "ipython",
190+
"version": 2
191+
},
192+
"file_extension": ".py",
193+
"mimetype": "text/x-python",
194+
"name": "python",
195+
"nbconvert_exporter": "python",
196+
"pygments_lexer": "ipython2",
197+
"version": "2.7.10"
198+
}
199+
},
200+
"nbformat": 4,
201+
"nbformat_minor": 0
202+
}

0 commit comments

Comments
 (0)