def __init__(self, numpy_rng, theano_rng=None, n_ins=5000, hidden_layers_sizes=[500, 500], n_outs=2, corruption_levels=[0.1, 0.1]): """ This class is made to support a variable number of layers. :type numpy_rng: numpy.random.RandomState :param numpy_rng: numpy random number generator used to draw initial weights :type theano_rng: theano.tensor.shared_randomstreams.RandomStreams :param theano_rng: Theano random generator; if None is given one is generated based on a seed drawn from `rng` :type n_ins: int :param n_ins: dimension of the input to the sdA :type n_layers_sizes: list of ints :param n_layers_sizes: intermediate layers size, must contain at least one value :type n_outs: int :param n_outs: dimension of the output of the network :type corruption_levels: list of float :param corruption_levels: amount of corruption to use for each layer """ self.sigmoid_layers = [] self.dA_layers = [] self.params = [] self.n_layers = len(hidden_layers_sizes) assert self.n_layers > 0 if not theano_rng: theano_rng = RandomStreams(numpy_rng.randint(2 ** 30)) # allocate symbolic variables for the data self.x = T.matrix('x') # the data is presented as rasterized images self.y = T.matrix('y') # the actual values for each examples # The SdA is an MLP, for which all weights of intermediate layers # are shared with a different denoising autoencoders # We will first construct the SdA as a deep multilayer perceptron, # and when constructing each sigmoidal layer we also construct a # denoising autoencoder that shares weights with that layer # During pretraining we will train these autoencoders (which will # lead to chainging the weights of the MLP as well) # During finetunining we will finish training the SdA by doing # stochastich gradient descent on the MLP for i in xrange(self.n_layers): # construct the sigmoidal layer # the size of the input is either the number of hidden units of # the layer below or the input size if we are on the first layer if i == 0: input_size = n_ins else: input_size = hidden_layers_sizes[i - 1] # the input to this layer is either the activation of the hidden # layer below or the input of the SdA if you are on the first # layer if i == 0: layer_input = self.x else: layer_input = self.sigmoid_layers[-1].output sigmoid_layer = HiddenLayer(rng=numpy_rng, input=layer_input, n_in=input_size, n_out=hidden_layers_sizes[i], activation=T.nnet.sigmoid) # add the layer to our list of layers self.sigmoid_layers.append(sigmoid_layer) # its arguably a philosophical question... # but we are going to only declare that the parameters of the # sigmoid_layers are parameters of the StackedDAA # the visible biases in the dA are parameters of those # dA, but not the SdA self.params.extend(sigmoid_layer.params) # Construct a denoising autoencoder that shared weights with this # layer dA_layer = dA(numpy_rng=numpy_rng, theano_rng=theano_rng, input=layer_input, n_visible=input_size, n_hidden=hidden_layers_sizes[i], W=sigmoid_layer.W, bhid=sigmoid_layer.b) self.dA_layers.append(dA_layer) # We now need to add a regression layer on top of the MLP self.regLayer = Regression( input=self.sigmoid_layers[-1].output, n_in=hidden_layers_sizes[-1], n_out=n_outs) self.params.extend(self.regLayer.params) # construct a function that implements one step of finetunining # compute the cost for second phase of training, # defined as the squared error self.finetune_cost = self.regLayer.errors(self.y) # compute the gradients with respect to the model parameters # symbolic variable that points to the number of errors made on the # minibatch given by self.x and self.y self.errors = self.regLayer.errors(self.y)
def build_model(self, load_previous_weights=False): """Creates the net's layers from the model settings.""" if load_previous_weights == True: self.load_weights() # Load the data datasets = self.load_samples() # Train, Validation, Test 100000, 20000, 26... fot Mitocondria set # Train, Validation, Test 50000, 10000, 10000 times 28x28 = 784 for MNIST dataset self.train_set_x, self.train_set_y = datasets[0] self.valid_set_x, self.valid_set_y = datasets[1] self.test_set_x, self.test_set_y = datasets[2] # Assumes the width equals the height img_width_size = numpy.sqrt(self.test_set_x.shape[2].eval()).astype(int) print img_width_size assert self.test_set_x.shape[2].eval() == img_width_size * img_width_size, 'input image not square' print "Image shape %s x %s" % (img_width_size, img_width_size) nbr_channels = self.test_set_x.shape[1].eval() self.input_shape = (nbr_channels, img_width_size, img_width_size) # Compute number of minibatches for training, validation and testing # Divide the total number of elements in the set by the batch size self.n_train_batches = self.train_set_x.get_value(borrow=True).shape[0] self.n_valid_batches = self.valid_set_x.get_value(borrow=True).shape[0] self.n_test_batches = self.test_set_x.get_value(borrow=True).shape[0] self.n_train_batches /= self.batch_size self.n_valid_batches /= self.batch_size self.n_test_batches /= self.batch_size print 'Size train_batches %d, n_valid_batches %d, n_test_batches %d' % (self.n_train_batches, self.n_valid_batches, self.n_test_batches) ###################### # BUILD ACTUAL MODEL # ###################### print 'Building the model ...' # The input is an 4D array of size, number of images in the batch size, number of channels # (or number of feature maps), image width and height. #TODO(vpetresc) make nbr of channels variable (1 or 3) layer_input = self.x.reshape((self.batch_size, nbr_feature_maps, self.input_shape[1], self.input_shape[2])) pooled_width = self.input_shape[1] pooled_height = self.input_shape[2] # Add convolutional layers followed by pooling clayers = [] idx = 0 for clayer_params in self.convolutional_layers: print 'Adding conv layer nbr filter %d, Ksize %d' % (clayer_params.num_filters, clayer_params.filter_w) if load_previous_weights == False: layer = LeNetConvPoolLayer(self.rng, input=layer_input, image_shape=(self.batch_size, nbr_feature_maps, pooled_width, pooled_height), filter_shape=(clayer_params.num_filters, nbr_feature_maps, clayer_params.filter_w, clayer_params.filter_w), poolsize=(self.poolsize, self.poolsize)) else: layer = LeNetConvPoolLayer(self.rng, input=layer_input, image_shape=(self.batch_size, nbr_feature_maps, pooled_width, pooled_height), filter_shape=(clayer_params.num_filters, nbr_feature_maps, clayer_params.filter_w, clayer_params.filter_w), poolsize=(self.poolsize, self.poolsize), W=self.cached_weights[itdx+1], b=self.cached_weights[idx]) clayers.append(layer) pooled_width = (pooled_width - clayer_params.filter_w + 1) / self.poolsize pooled_height = (pooled_height - clayer_params.filter_w + 1) / self.poolsize layer_input = layer.output nbr_feature_maps = clayer_params.num_filters idx += 2 # Flatten the output of the previous layers and add # fully connected sigmoidal layers layer_input = layer_input.flatten(2) nbr_input = nbr_feature_maps * pooled_width * pooled_height hlayers = [] for hlayer_params in self.hidden_layers: print 'Adding hidden layer fully connected %d' % (hlayer_params.num_outputs) if load_previous_weights == False: layer = HiddenLayer(self.rng, input=layer_input, n_in=nbr_input, n_out=hlayer_params.num_outputs, activation=T.tanh) else: layer = HiddenLayer(self.rng, input=layer_input, n_in=nbr_input, n_out=hlayer_params.num_outputs, activation=T.tanh, W=self.cached_weights[idx+1], b=self.cached_weights[idx]) idx +=2 nbr_input = hlayer_params.num_outputs layer_input = layer.output hlayers.append(layer) # classify the values of the fully-connected sigmoidal layer if load_previous_weights == False: self.output_layer = Regression(input=layer_input, n_in=nbr_input, n_out=self.last_layer.num_outputs) else: self.output_layer = Regression(input=layer_input, n_in=nbr_input, n_out=self.last_layer.num_outputs, W=self.cached_weights[idx+1], b=self.cached_weights[idx]) # the cost we minimize during training is the NLL of the model self.cost = self.output_layer.squared_loss(self.y) # Create a list of all model parameters to be fit by gradient descent. # The parameters are added in reversed order because ofthe order # in the backpropagation algorithm. self.params = self.output_layer.params for hidden_layer in reversed(hlayers): self.params += hidden_layer.params for conv_layer in reversed(clayers): self.params += conv_layer.params # create a list of gradients for all model parameters self.grads = T.grad(self.cost, self.params)
class SdA(object): """Stacked denoising auto-encoder class (SdA) A stacked denoising autoencoder model is obtained by stacking several dAs. The hidden layer of the dA at layer `i` becomes the input of the dA at layer `i+1`. The first layer dA gets as input the input of the SdA, and the hidden layer of the last dA represents the output. Note that after pretraining, the SdA is dealt with as a normal MLP, the dAs are only used to initialize the weights. """ def __init__(self, numpy_rng, theano_rng=None, n_ins=5000, hidden_layers_sizes=[500, 500], n_outs=2, corruption_levels=[0.1, 0.1]): """ This class is made to support a variable number of layers. :type numpy_rng: numpy.random.RandomState :param numpy_rng: numpy random number generator used to draw initial weights :type theano_rng: theano.tensor.shared_randomstreams.RandomStreams :param theano_rng: Theano random generator; if None is given one is generated based on a seed drawn from `rng` :type n_ins: int :param n_ins: dimension of the input to the sdA :type n_layers_sizes: list of ints :param n_layers_sizes: intermediate layers size, must contain at least one value :type n_outs: int :param n_outs: dimension of the output of the network :type corruption_levels: list of float :param corruption_levels: amount of corruption to use for each layer """ self.sigmoid_layers = [] self.dA_layers = [] self.params = [] self.n_layers = len(hidden_layers_sizes) assert self.n_layers > 0 if not theano_rng: theano_rng = RandomStreams(numpy_rng.randint(2 ** 30)) # allocate symbolic variables for the data self.x = T.matrix('x') # the data is presented as rasterized images self.y = T.matrix('y') # the actual values for each examples # The SdA is an MLP, for which all weights of intermediate layers # are shared with a different denoising autoencoders # We will first construct the SdA as a deep multilayer perceptron, # and when constructing each sigmoidal layer we also construct a # denoising autoencoder that shares weights with that layer # During pretraining we will train these autoencoders (which will # lead to chainging the weights of the MLP as well) # During finetunining we will finish training the SdA by doing # stochastich gradient descent on the MLP for i in xrange(self.n_layers): # construct the sigmoidal layer # the size of the input is either the number of hidden units of # the layer below or the input size if we are on the first layer if i == 0: input_size = n_ins else: input_size = hidden_layers_sizes[i - 1] # the input to this layer is either the activation of the hidden # layer below or the input of the SdA if you are on the first # layer if i == 0: layer_input = self.x else: layer_input = self.sigmoid_layers[-1].output sigmoid_layer = HiddenLayer(rng=numpy_rng, input=layer_input, n_in=input_size, n_out=hidden_layers_sizes[i], activation=T.nnet.sigmoid) # add the layer to our list of layers self.sigmoid_layers.append(sigmoid_layer) # its arguably a philosophical question... # but we are going to only declare that the parameters of the # sigmoid_layers are parameters of the StackedDAA # the visible biases in the dA are parameters of those # dA, but not the SdA self.params.extend(sigmoid_layer.params) # Construct a denoising autoencoder that shared weights with this # layer dA_layer = dA(numpy_rng=numpy_rng, theano_rng=theano_rng, input=layer_input, n_visible=input_size, n_hidden=hidden_layers_sizes[i], W=sigmoid_layer.W, bhid=sigmoid_layer.b) self.dA_layers.append(dA_layer) # We now need to add a regression layer on top of the MLP self.regLayer = Regression( input=self.sigmoid_layers[-1].output, n_in=hidden_layers_sizes[-1], n_out=n_outs) self.params.extend(self.regLayer.params) # construct a function that implements one step of finetunining # compute the cost for second phase of training, # defined as the squared error self.finetune_cost = self.regLayer.errors(self.y) # compute the gradients with respect to the model parameters # symbolic variable that points to the number of errors made on the # minibatch given by self.x and self.y self.errors = self.regLayer.errors(self.y) def pretraining_functions(self, train_set_x, batch_size): ''' Generates a list of functions, each of them implementing one step in trainnig the dA corresponding to the layer with same index. The function will require as input the minibatch index, and to train a dA you just need to iterate, calling the corresponding function on all minibatch indexes. :type train_set_x: theano.tensor.TensorType :param train_set_x: Shared variable that contains all datapoints used for training the dA :type batch_size: int :param batch_size: size of a [mini]batch :type learning_rate: float :param learning_rate: learning rate used during training for any of the dA layers ''' # index to a [mini]batch index = T.lscalar('index') # index to a minibatch corruption_level = T.scalar('corruption') # % of corruption to use learning_rate = T.scalar('lr') # learning rate to use # number of batches n_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size # begining of a batch, given `index` batch_begin = index * batch_size # ending of a batch given `index` batch_end = batch_begin + batch_size pretrain_fns = [] first_layer = True for dA in self.dA_layers: # get the cost and the updates list if first_layer: cost, updates = dA.get_cost_updates(corruption_level, learning_rate, noise='gaussian') first_layer = False else: cost, updates = dA.get_cost_updates(corruption_level, learning_rate, noise='gaussian') # compile the theano function fn = theano.function(inputs=[index, theano.Param(corruption_level, default=0.2), theano.Param(learning_rate, default=0.1)], outputs=cost, updates=updates, givens={self.x: train_set_x[batch_begin: batch_end]}) # append `fn` to the list of functions pretrain_fns.append(fn) return pretrain_fns def build_finetune_functions(self, datasets, batch_size): '''Generates a function `train` that implements one step of finetuning, a function `validate` that computes the error on a batch from the validation set, and a function `test` that computes the error on a batch from the testing set :type datasets: list of pairs of theano.tensor.TensorType :param datasets: It is a list that contain all the datasets; the has to contain three pairs, `train`, `valid`, `test` in this order, where each pair is formed of two Theano variables, one for the datapoints, the other for the labels :type batch_size: int :param batch_size: size of a minibatch :type learning_rate: float :param learning_rate: learning rate used during finetune stage ''' (train_set_x, train_set_y) = datasets[0] (valid_set_x, valid_set_y) = datasets[1] (test_set_x, test_set_y) = datasets[2] # compute number of minibatches for training, validation and testing n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] n_valid_batches /= batch_size n_test_batches = test_set_x.get_value(borrow=True).shape[0] n_test_batches /= batch_size index = T.lscalar('index') # index to a [mini]batch learning_rate = T.scalar('lr') # compute the gradients with respect to the model parameters gparams = T.grad(self.finetune_cost, self.params) # compute list of fine-tuning updates updates = [] for param, gparam in zip(self.params, gparams): updates.append((param, param - gparam * learning_rate)) train_fn = theano.function(inputs=[index, theano.Param(learning_rate, default=0.1)], outputs=self.finetune_cost, updates=updates, givens={ self.x: train_set_x[index * batch_size: (index + 1) * batch_size], self.y: train_set_y[index * batch_size: (index + 1) * batch_size]}, name='train') test_score_i = theano.function([index], self.errors, givens={ self.x: test_set_x[index * batch_size: (index + 1) * batch_size], self.y: test_set_y[index * batch_size: (index + 1) * batch_size]}, name='test') valid_score_i = theano.function([index], self.errors, givens={ self.x: valid_set_x[index * batch_size: (index + 1) * batch_size], self.y: valid_set_y[index * batch_size: (index + 1) * batch_size]}, name='valid') # Create a function that scans the entire validation set def valid_score(): return [valid_score_i(i) for i in xrange(n_valid_batches)] # Create a function that scans the entire test set def test_score(): return [test_score_i(i) for i in xrange(n_test_batches)] return train_fn, valid_score, test_score
class CNNTrainRegression(CNNBase): """The class takes a proto bufer as input, setups a CNN according to the settings, trains the network and saves the weights in a file """ def __init__(self, protofile, cached_weights): """ :param protofile: describes the arhitecture of the network :type protofile: string :param cached_weights: filename of the weights :type cached_weights: string """ self.cnntype = 'TRAIN' #: extension that is added to the logfile name super(CNNTrain, self).__init__(protofile, cached_weights) #: Array of train data of size num samples x dimension of sample self.train_set_x = None #: Array of target data of size num samples x 1 self.train_set_y = None #: Array of valid data of size num samples x dimension of sample self.valid_set_x = None self.valid_set_y = None self.test_set_x = None self.test_set_y = None #: The number of batches in which train_set_x is divided self.n_train_batches = 0 self.n_valid_batches = 0 self.n_test_batches = 0 #: The cost that is minimized by the algorithm, usually log likelihood self.cost = 0 #: Array of symbolic gradients of the cost wrt. weights self.grads = None #: Array of weights (plus biases) for which the gradient is computed self.params = None #: Usually logistic regression self.output_layer = None #: The size of the input array that is being passed to the algorithm #: It has size num samples x num channels x img width x img height self.input_shape = None self.index = T.lscalar() #: index to a [mini]batch self.x = T.matrix('x') #: the data is presented as rasterized images self.y = T.ivector('y') #: the labels are presented as 1D vector of ints def build_model(self, load_previous_weights=False): """Creates the net's layers from the model settings.""" if load_previous_weights == True: self.load_weights() # Load the data datasets = self.load_samples() # Train, Validation, Test 100000, 20000, 26... fot Mitocondria set # Train, Validation, Test 50000, 10000, 10000 times 28x28 = 784 for MNIST dataset self.train_set_x, self.train_set_y = datasets[0] self.valid_set_x, self.valid_set_y = datasets[1] self.test_set_x, self.test_set_y = datasets[2] # Assumes the width equals the height img_width_size = numpy.sqrt(self.test_set_x.shape[2].eval()).astype(int) print img_width_size assert self.test_set_x.shape[2].eval() == img_width_size * img_width_size, 'input image not square' print "Image shape %s x %s" % (img_width_size, img_width_size) nbr_channels = self.test_set_x.shape[1].eval() self.input_shape = (nbr_channels, img_width_size, img_width_size) # Compute number of minibatches for training, validation and testing # Divide the total number of elements in the set by the batch size self.n_train_batches = self.train_set_x.get_value(borrow=True).shape[0] self.n_valid_batches = self.valid_set_x.get_value(borrow=True).shape[0] self.n_test_batches = self.test_set_x.get_value(borrow=True).shape[0] self.n_train_batches /= self.batch_size self.n_valid_batches /= self.batch_size self.n_test_batches /= self.batch_size print 'Size train_batches %d, n_valid_batches %d, n_test_batches %d' % (self.n_train_batches, self.n_valid_batches, self.n_test_batches) ###################### # BUILD ACTUAL MODEL # ###################### print 'Building the model ...' # The input is an 4D array of size, number of images in the batch size, number of channels # (or number of feature maps), image width and height. #TODO(vpetresc) make nbr of channels variable (1 or 3) layer_input = self.x.reshape((self.batch_size, nbr_feature_maps, self.input_shape[1], self.input_shape[2])) pooled_width = self.input_shape[1] pooled_height = self.input_shape[2] # Add convolutional layers followed by pooling clayers = [] idx = 0 for clayer_params in self.convolutional_layers: print 'Adding conv layer nbr filter %d, Ksize %d' % (clayer_params.num_filters, clayer_params.filter_w) if load_previous_weights == False: layer = LeNetConvPoolLayer(self.rng, input=layer_input, image_shape=(self.batch_size, nbr_feature_maps, pooled_width, pooled_height), filter_shape=(clayer_params.num_filters, nbr_feature_maps, clayer_params.filter_w, clayer_params.filter_w), poolsize=(self.poolsize, self.poolsize)) else: layer = LeNetConvPoolLayer(self.rng, input=layer_input, image_shape=(self.batch_size, nbr_feature_maps, pooled_width, pooled_height), filter_shape=(clayer_params.num_filters, nbr_feature_maps, clayer_params.filter_w, clayer_params.filter_w), poolsize=(self.poolsize, self.poolsize), W=self.cached_weights[itdx+1], b=self.cached_weights[idx]) clayers.append(layer) pooled_width = (pooled_width - clayer_params.filter_w + 1) / self.poolsize pooled_height = (pooled_height - clayer_params.filter_w + 1) / self.poolsize layer_input = layer.output nbr_feature_maps = clayer_params.num_filters idx += 2 # Flatten the output of the previous layers and add # fully connected sigmoidal layers layer_input = layer_input.flatten(2) nbr_input = nbr_feature_maps * pooled_width * pooled_height hlayers = [] for hlayer_params in self.hidden_layers: print 'Adding hidden layer fully connected %d' % (hlayer_params.num_outputs) if load_previous_weights == False: layer = HiddenLayer(self.rng, input=layer_input, n_in=nbr_input, n_out=hlayer_params.num_outputs, activation=T.tanh) else: layer = HiddenLayer(self.rng, input=layer_input, n_in=nbr_input, n_out=hlayer_params.num_outputs, activation=T.tanh, W=self.cached_weights[idx+1], b=self.cached_weights[idx]) idx +=2 nbr_input = hlayer_params.num_outputs layer_input = layer.output hlayers.append(layer) # classify the values of the fully-connected sigmoidal layer if load_previous_weights == False: self.output_layer = Regression(input=layer_input, n_in=nbr_input, n_out=self.last_layer.num_outputs) else: self.output_layer = Regression(input=layer_input, n_in=nbr_input, n_out=self.last_layer.num_outputs, W=self.cached_weights[idx+1], b=self.cached_weights[idx]) # the cost we minimize during training is the NLL of the model self.cost = self.output_layer.squared_loss(self.y) # Create a list of all model parameters to be fit by gradient descent. # The parameters are added in reversed order because ofthe order # in the backpropagation algorithm. self.params = self.output_layer.params for hidden_layer in reversed(hlayers): self.params += hidden_layer.params for conv_layer in reversed(clayers): self.params += conv_layer.params # create a list of gradients for all model parameters self.grads = T.grad(self.cost, self.params) def train_model(self): """Abstract method to be implemented by subclasses""" raise NotImplementedError()