Mercurial > ift6266
annotate baseline/conv_mlp/convolutional_mlp.py @ 596:f6a3b28b002c
nips2010_submission.pdf
author | Yoshua Bengio <bengioy@iro.umontreal.ca> |
---|---|
date | Thu, 14 Oct 2010 15:52:02 -0400 |
parents | d41fe003fade |
children |
rev | line source |
---|---|
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
1 """ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
2 This tutorial introduces the LeNet5 neural network architecture using Theano. LeNet5 is a |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
3 convolutional neural network, good for classifying images. This tutorial shows how to build the |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
4 architecture, and comes with all the hyper-parameters you need to reproduce the paper's MNIST |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
5 results. |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
6 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
7 The best results are obtained after X iterations of the main program loop, which takes *** |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
8 minutes on my workstation (an Intel Core i7, circa July 2009), and *** minutes on my GPU (an |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
9 NVIDIA GTX 285 graphics processor). |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
10 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
11 This implementation simplifies the model in the following ways: |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
12 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
13 - LeNetConvPool doesn't implement location-specific gain and bias parameters |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
14 - LeNetConvPool doesn't implement pooling by average, it implements pooling by max. |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
15 - Digit classification is implemented with a logistic regression rather than an RBF network |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
16 - LeNet5 was not fully-connected convolutions at second layer |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
17 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
18 References: |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
19 - Y. LeCun, L. Bottou, Y. Bengio and P. Haffner: Gradient-Based Learning Applied to Document |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
20 Recognition, Proceedings of the IEEE, 86(11):2278-2324, November 1998. |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
21 http://yann.lecun.com/exdb/publis/pdf/lecun-98.pdf |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
22 """ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
23 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
24 import numpy, theano, cPickle, gzip, time |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
25 import theano.tensor as T |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
26 import theano.sandbox.softsign |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
27 import sys |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
28 import pylearn.datasets.MNIST |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
29 from pylearn.io import filetensor as ft |
253
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
30 from theano.sandbox import conv, downsample |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
31 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
32 from ift6266 import datasets |
253
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
33 import theano,pylearn.version,ift6266 |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
34 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
35 class LeNetConvPoolLayer(object): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
36 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
37 def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2,2)): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
38 """ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
39 Allocate a LeNetConvPoolLayer with shared variable internal parameters. |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
40 :type rng: numpy.random.RandomState |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
41 :param rng: a random number generator used to initialize weights |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
42 :type input: theano.tensor.dtensor4 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
43 :param input: symbolic image tensor, of shape image_shape |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
44 :type filter_shape: tuple or list of length 4 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
45 :param filter_shape: (number of filters, num input feature maps, |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
46 filter height,filter width) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
47 :type image_shape: tuple or list of length 4 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
48 :param image_shape: (batch size, num input feature maps, |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
49 image height, image width) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
50 :type poolsize: tuple or list of length 2 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
51 :param poolsize: the downsampling (pooling) factor (#rows,#cols) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
52 """ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
53 assert image_shape[1]==filter_shape[1] |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
54 self.input = input |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
55 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
56 # initialize weight values: the fan-in of each hidden neuron is |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
57 # restricted by the size of the receptive fields. |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
58 fan_in = numpy.prod(filter_shape[1:]) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
59 W_values = numpy.asarray( rng.uniform( \ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
60 low = -numpy.sqrt(3./fan_in), \ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
61 high = numpy.sqrt(3./fan_in), \ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
62 size = filter_shape), dtype = theano.config.floatX) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
63 self.W = theano.shared(value = W_values) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
64 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
65 # the bias is a 1D tensor -- one bias per output feature map |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
66 b_values = numpy.zeros((filter_shape[0],), dtype= theano.config.floatX) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
67 self.b = theano.shared(value= b_values) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
68 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
69 # convolve input feature maps with filters |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
70 conv_out = conv.conv2d(input, self.W, |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
71 filter_shape=filter_shape, image_shape=image_shape) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
72 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
73 # downsample each feature map individually, using maxpooling |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
74 pooled_out = downsample.max_pool2D(conv_out, poolsize, ignore_border=True) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
75 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
76 # add the bias term. Since the bias is a vector (1D array), we first |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
77 # reshape it to a tensor of shape (1,n_filters,1,1). Each bias will thus |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
78 # be broadcasted across mini-batches and feature map width & height |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
79 self.output = T.tanh(pooled_out + self.b.dimshuffle('x', 0, 'x', 'x')) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
80 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
81 # store parameters of this layer |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
82 self.params = [self.W, self.b] |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
83 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
84 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
85 class SigmoidalLayer(object): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
86 def __init__(self, rng, input, n_in, n_out): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
87 """ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
88 Typical hidden layer of a MLP: units are fully-connected and have |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
89 sigmoidal activation function. Weight matrix W is of shape (n_in,n_out) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
90 and the bias vector b is of shape (n_out,). |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
91 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
92 Hidden unit activation is given by: sigmoid(dot(input,W) + b) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
93 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
94 :type rng: numpy.random.RandomState |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
95 :param rng: a random number generator used to initialize weights |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
96 :type input: theano.tensor.dmatrix |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
97 :param input: a symbolic tensor of shape (n_examples, n_in) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
98 :type n_in: int |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
99 :param n_in: dimensionality of input |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
100 :type n_out: int |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
101 :param n_out: number of hidden units |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
102 """ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
103 self.input = input |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
104 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
105 W_values = numpy.asarray( rng.uniform( \ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
106 low = -numpy.sqrt(6./(n_in+n_out)), \ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
107 high = numpy.sqrt(6./(n_in+n_out)), \ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
108 size = (n_in, n_out)), dtype = theano.config.floatX) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
109 self.W = theano.shared(value = W_values) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
110 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
111 b_values = numpy.zeros((n_out,), dtype= theano.config.floatX) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
112 self.b = theano.shared(value= b_values) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
113 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
114 self.output = T.tanh(T.dot(input, self.W) + self.b) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
115 self.params = [self.W, self.b] |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
116 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
117 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
118 class LogisticRegression(object): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
119 """Multi-class Logistic Regression Class |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
120 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
121 The logistic regression is fully described by a weight matrix :math:`W` |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
122 and bias vector :math:`b`. Classification is done by projecting data |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
123 points onto a set of hyperplanes, the distance to which is used to |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
124 determine a class membership probability. |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
125 """ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
126 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
127 def __init__(self, input, n_in, n_out): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
128 """ Initialize the parameters of the logistic regression |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
129 :param input: symbolic variable that describes the input of the |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
130 architecture (one minibatch) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
131 :type n_in: int |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
132 :param n_in: number of input units, the dimension of the space in |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
133 which the datapoints lie |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
134 :type n_out: int |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
135 :param n_out: number of output units, the dimension of the space in |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
136 which the labels lie |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
137 """ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
138 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
139 # initialize with 0 the weights W as a matrix of shape (n_in, n_out) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
140 self.W = theano.shared( value=numpy.zeros((n_in,n_out), |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
141 dtype = theano.config.floatX) ) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
142 # initialize the baises b as a vector of n_out 0s |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
143 self.b = theano.shared( value=numpy.zeros((n_out,), |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
144 dtype = theano.config.floatX) ) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
145 # compute vector of class-membership probabilities in symbolic form |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
146 self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W)+self.b) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
147 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
148 # compute prediction as class whose probability is maximal in |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
149 # symbolic form |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
150 self.y_pred=T.argmax(self.p_y_given_x, axis=1) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
151 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
152 # list of parameters for this layer |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
153 self.params = [self.W, self.b] |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
154 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
155 def negative_log_likelihood(self, y): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
156 """Return the mean of the negative log-likelihood of the prediction |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
157 of this model under a given target distribution. |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
158 :param y: corresponds to a vector that gives for each example the |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
159 correct label |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
160 Note: we use the mean instead of the sum so that |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
161 the learning rate is less dependent on the batch size |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
162 """ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
163 return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y]) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
164 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
165 def errors(self, y): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
166 """Return a float representing the number of errors in the minibatch |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
167 over the total number of examples of the minibatch ; zero one |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
168 loss over the size of the minibatch |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
169 """ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
170 # check if y has same dimension of y_pred |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
171 if y.ndim != self.y_pred.ndim: |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
172 raise TypeError('y should have the same shape as self.y_pred', |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
173 ('y', target.type, 'y_pred', self.y_pred.type)) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
174 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
175 # check if y is of the correct datatype |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
176 if y.dtype.startswith('int'): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
177 # the T.neq operator returns a vector of 0s and 1s, where 1 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
178 # represents a mistake in prediction |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
179 return T.mean(T.neq(self.y_pred, y)) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
180 else: |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
181 raise NotImplementedError() |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
182 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
183 |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
184 def evaluate_lenet5(learning_rate=0.1, n_iter=200, batch_size=20, n_kern0=20, n_kern1=50, n_layer=3, filter_shape0=5, filter_shape1=5, sigmoide_size=500, dataset='mnist.pkl.gz'): |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
185 rng = numpy.random.RandomState(23455) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
186 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
187 print 'Before load dataset' |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
188 dataset=datasets.nist_digits |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
189 train_batches= dataset.train(batch_size) |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
190 valid_batches=dataset.valid(batch_size) |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
191 test_batches=dataset.test(batch_size) |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
192 #print valid_batches.shape |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
193 #print test_batches.shape |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
194 print 'After load dataset' |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
195 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
196 ishape = (32,32) # this is the size of NIST images |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
197 n_kern2=80 |
253
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
198 n_kern3=100 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
199 if n_layer==4: |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
200 filter_shape1=3 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
201 filter_shape2=3 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
202 if n_layer==5: |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
203 filter_shape0=4 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
204 filter_shape1=2 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
205 filter_shape2=2 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
206 filter_shape3=2 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
207 |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
208 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
209 # allocate symbolic variables for the data |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
210 x = T.matrix('x') # rasterized images |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
211 y = T.lvector() # the labels are presented as 1D vector of [long int] labels |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
212 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
213 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
214 ###################### |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
215 # BUILD ACTUAL MODEL # |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
216 ###################### |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
217 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
218 # Reshape matrix of rasterized images of shape (batch_size,28*28) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
219 # to a 4D tensor, compatible with our LeNetConvPoolLayer |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
220 layer0_input = x.reshape((batch_size,1,32,32)) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
221 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
222 # Construct the first convolutional pooling layer: |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
223 # filtering reduces the image size to (32-5+1,32-5+1)=(28,28) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
224 # maxpooling reduces this further to (28/2,28/2) = (14,14) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
225 # 4D output tensor is thus of shape (20,20,14,14) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
226 layer0 = LeNetConvPoolLayer(rng, input=layer0_input, |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
227 image_shape=(batch_size,1,32,32), |
253
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
228 filter_shape=(n_kern0,1,filter_shape0,filter_shape0), poolsize=(2,2)) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
229 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
230 if(n_layer>2): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
231 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
232 # Construct the second convolutional pooling layer |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
233 # filtering reduces the image size to (14-5+1,14-5+1)=(10,10) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
234 # maxpooling reduces this further to (10/2,10/2) = (5,5) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
235 # 4D output tensor is thus of shape (20,50,5,5) |
253
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
236 fshape0=(32-filter_shape0+1)/2 |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
237 layer1 = LeNetConvPoolLayer(rng, input=layer0.output, |
253
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
238 image_shape=(batch_size,n_kern0,fshape0,fshape0), |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
239 filter_shape=(n_kern1,n_kern0,filter_shape1,filter_shape1), poolsize=(2,2)) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
240 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
241 else: |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
242 |
253
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
243 fshape0=(32-filter_shape0+1)/2 |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
244 layer1_input = layer0.output.flatten(2) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
245 # construct a fully-connected sigmoidal layer |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
246 layer1 = SigmoidalLayer(rng, input=layer1_input,n_in=n_kern0*fshape0*fshape0, n_out=sigmoide_size) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
247 |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
248 layer2 = LogisticRegression(input=layer1.output, n_in=sigmoide_size, n_out=10) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
249 cost = layer2.negative_log_likelihood(y) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
250 test_model = theano.function([x,y], layer2.errors(y)) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
251 params = layer2.params+ layer1.params + layer0.params |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
252 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
253 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
254 if(n_layer>3): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
255 |
253
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
256 fshape0=(32-filter_shape0+1)/2 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
257 fshape1=(fshape0-filter_shape1+1)/2 |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
258 layer2 = LeNetConvPoolLayer(rng, input=layer1.output, |
253
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
259 image_shape=(batch_size,n_kern1,fshape1,fshape1), |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
260 filter_shape=(n_kern2,n_kern1,filter_shape2,filter_shape2), poolsize=(2,2)) |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
261 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
262 if(n_layer>4): |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
263 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
264 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
265 fshape0=(32-filter_shape0+1)/2 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
266 fshape1=(fshape0-filter_shape1+1)/2 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
267 fshape2=(fshape1-filter_shape2+1)/2 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
268 fshape3=(fshape2-filter_shape3+1)/2 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
269 layer3 = LeNetConvPoolLayer(rng, input=layer2.output, |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
270 image_shape=(batch_size,n_kern2,fshape2,fshape2), |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
271 filter_shape=(n_kern3,n_kern2,filter_shape3,filter_shape3), poolsize=(2,2)) |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
272 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
273 layer4_input = layer3.output.flatten(2) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
274 |
253
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
275 layer4 = SigmoidalLayer(rng, input=layer4_input, |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
276 n_in=n_kern3*fshape3*fshape3, n_out=sigmoide_size) |
253
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
277 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
278 |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
279 layer5 = LogisticRegression(input=layer4.output, n_in=sigmoide_size, n_out=10) |
253
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
280 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
281 cost = layer5.negative_log_likelihood(y) |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
282 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
283 test_model = theano.function([x,y], layer5.errors(y)) |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
284 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
285 params = layer5.params+ layer4.params+ layer3.params+ layer2.params+ layer1.params + layer0.params |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
286 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
287 elif(n_layer>3): |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
288 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
289 fshape0=(32-filter_shape0+1)/2 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
290 fshape1=(fshape0-filter_shape1+1)/2 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
291 fshape2=(fshape1-filter_shape2+1)/2 |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
292 layer3_input = layer2.output.flatten(2) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
293 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
294 layer3 = SigmoidalLayer(rng, input=layer3_input, |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
295 n_in=n_kern2*fshape2*fshape2, n_out=sigmoide_size) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
296 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
297 |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
298 layer4 = LogisticRegression(input=layer3.output, n_in=sigmoide_size, n_out=10) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
299 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
300 cost = layer4.negative_log_likelihood(y) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
301 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
302 test_model = theano.function([x,y], layer4.errors(y)) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
303 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
304 params = layer4.params+ layer3.params+ layer2.params+ layer1.params + layer0.params |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
305 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
306 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
307 elif(n_layer>2): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
308 |
253
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
309 fshape0=(32-filter_shape0+1)/2 |
a491d3600a77
Derniere version du reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
196
diff
changeset
|
310 fshape1=(fshape0-filter_shape1+1)/2 |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
311 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
312 # the SigmoidalLayer being fully-connected, it operates on 2D matrices of |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
313 # shape (batch_size,num_pixels) (i.e matrix of rasterized images). |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
314 # This will generate a matrix of shape (20,32*4*4) = (20,512) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
315 layer2_input = layer1.output.flatten(2) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
316 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
317 # construct a fully-connected sigmoidal layer |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
318 layer2 = SigmoidalLayer(rng, input=layer2_input, |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
319 n_in=n_kern1*fshape1*fshape1, n_out=sigmoide_size) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
320 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
321 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
322 # classify the values of the fully-connected sigmoidal layer |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
323 layer3 = LogisticRegression(input=layer2.output, n_in=sigmoide_size, n_out=10) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
324 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
325 # the cost we minimize during training is the NLL of the model |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
326 cost = layer3.negative_log_likelihood(y) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
327 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
328 # create a function to compute the mistakes that are made by the model |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
329 test_model = theano.function([x,y], layer3.errors(y)) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
330 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
331 # create a list of all model parameters to be fit by gradient descent |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
332 params = layer3.params+ layer2.params+ layer1.params + layer0.params |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
333 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
334 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
335 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
336 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
337 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
338 # create a list of gradients for all model parameters |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
339 grads = T.grad(cost, params) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
340 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
341 # train_model is a function that updates the model parameters by SGD |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
342 # Since this model has many parameters, it would be tedious to manually |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
343 # create an update rule for each model parameter. We thus create the updates |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
344 # dictionary by automatically looping over all (params[i],grads[i]) pairs. |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
345 updates = {} |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
346 for param_i, grad_i in zip(params, grads): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
347 updates[param_i] = param_i - learning_rate * grad_i |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
348 train_model = theano.function([x, y], cost, updates=updates) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
349 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
350 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
351 ############### |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
352 # TRAIN MODEL # |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
353 ############### |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
354 |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
355 #n_minibatches = len(train_batches) |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
356 n_minibatches=0 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
357 n_valid=0 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
358 n_test=0 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
359 for x, y in dataset.train(batch_size): |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
360 if x.shape[0] == batch_size: |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
361 n_minibatches+=1 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
362 n_minibatches*=batch_size |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
363 print n_minibatches |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
364 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
365 for x, y in dataset.valid(batch_size): |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
366 if x.shape[0] == batch_size: |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
367 n_valid+=1 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
368 n_valid*=batch_size |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
369 print n_valid |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
370 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
371 for x, y in dataset.test(batch_size): |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
372 if x.shape[0] == batch_size: |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
373 n_test+=1 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
374 n_test*=batch_size |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
375 print n_test |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
376 |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
377 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
378 # early-stopping parameters |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
379 patience = 10000 # look as this many examples regardless |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
380 patience_increase = 2 # wait this much longer when a new best is |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
381 # found |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
382 improvement_threshold = 0.995 # a relative improvement of this much is |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
383 # considered significant |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
384 validation_frequency = n_minibatches # go through this many |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
385 # minibatche before checking the network |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
386 # on the validation set; in this case we |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
387 # check every epoch |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
388 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
389 best_params = None |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
390 best_validation_loss = float('inf') |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
391 best_iter = 0 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
392 test_score = 0. |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
393 start_time = time.clock() |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
394 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
395 |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
396 # have a maximum of `n_iter` iterations through the entire dataset |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
397 iter=0 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
398 for epoch in xrange(n_iter): |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
399 for x, y in train_batches: |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
400 if x.shape[0] != batch_size: |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
401 continue |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
402 iter+=1 |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
403 |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
404 # get epoch and minibatch index |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
405 #epoch = iter / n_minibatches |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
406 minibatch_index = iter % n_minibatches |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
407 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
408 if iter %100 == 0: |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
409 print 'training @ iter = ', iter |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
410 cost_ij = train_model(x,y) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
411 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
412 |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
413 # compute zero-one loss on validation set |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
414 this_validation_loss = 0. |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
415 for x,y in valid_batches: |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
416 if x.shape[0] != batch_size: |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
417 continue |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
418 # sum up the errors for each minibatch |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
419 this_validation_loss += test_model(x,y) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
420 |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
421 # get the average by dividing with the number of minibatches |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
422 this_validation_loss /= n_valid |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
423 print('epoch %i, minibatch %i/%i, validation error %f %%' % \ |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
424 (epoch, minibatch_index+1, n_minibatches, \ |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
425 this_validation_loss*100.)) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
426 |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
427 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
428 # if we got the best validation score until now |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
429 if this_validation_loss < best_validation_loss: |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
430 |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
431 #improve patience if loss improvement is good enough |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
432 if this_validation_loss < best_validation_loss * \ |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
433 improvement_threshold : |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
434 patience = max(patience, iter * patience_increase) |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
435 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
436 # save best validation score and iteration number |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
437 best_validation_loss = this_validation_loss |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
438 best_iter = iter |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
439 |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
440 # test it on the test set |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
441 test_score = 0. |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
442 for x,y in test_batches: |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
443 if x.shape[0] != batch_size: |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
444 continue |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
445 test_score += test_model(x,y) |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
446 test_score /= n_test |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
447 print((' epoch %i, minibatch %i/%i, test error of best ' |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
448 'model %f %%') % |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
449 (epoch, minibatch_index+1, n_minibatches, |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
450 test_score*100.)) |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
451 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
452 if patience <= iter : |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
453 break |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
454 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
455 end_time = time.clock() |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
456 print('Optimization complete.') |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
457 print('Best validation score of %f %% obtained at iteration %i,'\ |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
458 'with test performance %f %%' % |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
459 (best_validation_loss * 100., best_iter, test_score*100.)) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
460 print('The code ran for %f minutes' % ((end_time-start_time)/60.)) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
461 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
462 return (best_validation_loss * 100., test_score*100., (end_time-start_time)/60., best_iter) |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
463 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
464 if __name__ == '__main__': |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
465 evaluate_lenet5() |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
466 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
467 def experiment(state, channel): |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
468 print 'start experiment' |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
469 (best_validation_loss, test_score, minutes_trained, iter) = evaluate_lenet5(state.learning_rate, state.n_iter, state.batch_size, state.n_kern0, state.n_kern1, state.n_layer, state.filter_shape0, state.filter_shape1,state.sigmoide_size) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
470 print 'end experiment' |
270
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
471 |
d41fe003fade
Reseau a convolution avec le bon dataset
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
253
diff
changeset
|
472 pylearn.version.record_versions(state,[theano,ift6266,pylearn]) |
146
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
473 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
474 state.best_validation_loss = best_validation_loss |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
475 state.test_score = test_score |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
476 state.minutes_trained = minutes_trained |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
477 state.iter = iter |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
478 |
33038ab4e799
Reseau a convolution
Jeremy Eustache <jeremy.eustache@voila.fr>
parents:
diff
changeset
|
479 return channel.COMPLETE |