Python theano.Param() Examples
The following are 30
code examples of theano.Param().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
theano
, or try the search function
.
Example #1
Source File: dnn.py From DL4H with MIT License | 6 votes |
def get_SGD_trainer(self): """ Returns a plain SGD minibatch trainer with learning rate as param. """ batch_x = T.fmatrix('batch_x') batch_y = T.ivector('batch_y') learning_rate = T.fscalar('lr') # learning rate gparams = T.grad(self.mean_cost, self.params) # all the gradients updates = OrderedDict() for param, gparam in zip(self.params, gparams): updates[param] = param - gparam * learning_rate train_fn = theano.function(inputs=[theano.Param(batch_x), theano.Param(batch_y), theano.Param(learning_rate)], outputs=self.mean_cost, updates=updates, givens={self.x: batch_x, self.y: batch_y}) return train_fn
Example #2
Source File: dnn.py From DL4H with MIT License | 6 votes |
def get_adagrad_trainer(self): """ Returns an Adagrad (Duchi et al. 2010) trainer using a learning rate. """ batch_x = T.fmatrix('batch_x') batch_y = T.ivector('batch_y') learning_rate = T.fscalar('lr') # learning rate gparams = T.grad(self.mean_cost, self.params) # all the gradients updates = OrderedDict() for accugrad, param, gparam in zip(self._accugrads, self.params, gparams): # c.f. Algorithm 1 in the Adadelta paper (Zeiler 2012) agrad = accugrad + gparam * gparam dx = - (learning_rate / T.sqrt(agrad + self._eps)) * gparam updates[param] = param + dx updates[accugrad] = agrad train_fn = theano.function(inputs=[theano.Param(batch_x), theano.Param(batch_y), theano.Param(learning_rate)], outputs=self.mean_cost, updates=updates, givens={self.x: batch_x, self.y: batch_y}) return train_fn
Example #3
Source File: dnn.py From DL4H with MIT License | 6 votes |
def get_adadelta_trainer(self): """ Returns an Adadelta (Zeiler 2012) trainer using self._rho and self._eps params. """ batch_x = T.fmatrix('batch_x') batch_y = T.ivector('batch_y') gparams = T.grad(self.mean_cost, self.params) updates = OrderedDict() for accugrad, accudelta, param, gparam in zip(self._accugrads, self._accudeltas, self.params, gparams): # c.f. Algorithm 1 in the Adadelta paper (Zeiler 2012) agrad = self._rho * accugrad + (1 - self._rho) * gparam * gparam dx = - T.sqrt((accudelta + self._eps) / (agrad + self._eps)) * gparam updates[accudelta] = (self._rho * accudelta + (1 - self._rho) * dx * dx) updates[param] = param + dx updates[accugrad] = agrad train_fn = theano.function(inputs=[theano.Param(batch_x), theano.Param(batch_y)], outputs=self.mean_cost, updates=updates, givens={self.x: batch_x, self.y: batch_y}) return train_fn
Example #4
Source File: drn.py From pdnn with Apache License 2.0 | 5 votes |
def build_finetune_functions_kaldi(self, train_shared_xy, valid_shared_xy): (train_set_x, train_set_y) = train_shared_xy (valid_set_x, valid_set_y) = valid_shared_xy index = T.lscalar('index') # index to a [mini]batch learning_rate = T.fscalar('learning_rate') momentum = T.fscalar('momentum') # compute the gradients with respect to the model parameters gparams = T.grad(self.finetune_cost, self.params) # compute list of fine-tuning updates updates = collections.OrderedDict() for dparam, gparam in zip(self.delta_params, gparams): updates[dparam] = momentum * dparam - gparam*learning_rate for dparam, param in zip(self.delta_params, self.params): updates[param] = param + updates[dparam] if self.max_col_norm is not None: for i in xrange(self.hidden_layers_number): W = self.layers[i].W if W in updates: updated_W = updates[W] col_norms = T.sqrt(T.sum(T.sqr(updated_W), axis=0)) desired_norms = T.clip(col_norms, 0, self.max_col_norm) updates[W] = updated_W * (desired_norms / (1e-7 + col_norms)) train_fn = theano.function(inputs=[theano.Param(learning_rate, default = 0.0001), theano.Param(momentum, default = 0.5)], outputs=self.errors, updates=updates, givens={self.x: train_set_x, self.y: train_set_y}) valid_fn = theano.function(inputs=[], outputs=self.errors, givens={self.x: valid_set_x, self.y: valid_set_y}) return train_fn, valid_fn
Example #5
Source File: nets.py From seizure-detection with MIT License | 5 votes |
def predict_probabilities(self, given_set): """ Returns functions to get class probabilities. """ batch_x = T.fmatrix('batch_x') predict = theano.function(inputs=[theano.Param(batch_x),], outputs=self.predicted_probabilities, givens={self.x: batch_x}) def predictf(): """ returned function that scans the entire set given as input """ return [predict(batch_x) for batch_x, batch_y in given_set] return predictf
Example #6
Source File: nets.py From seizure-detection with MIT License | 5 votes |
def layer_activation(self, n, given_set): """ Returns functions to get class probabilities. """ batch_x = T.fmatrix('batch_x') output = theano.function(inputs=[theano.Param(batch_x),], outputs=self.layers[n].output, givens={self.x: batch_x}) def predictf(): """ returned function that scans the entire set given as input """ return [output(batch_x) for batch_x, batch_y in given_set] return predictf
Example #7
Source File: nets.py From seizure-detection with MIT License | 5 votes |
def get_adadelta_trainer(self): """ Returns an Adadelta (Zeiler 2012) trainer using self._rho and self._eps params. """ batch_x = T.fmatrix('batch_x') batch_y = T.ivector('batch_y') # compute the gradients with respect to the model parameters gparams = T.grad(self.mean_cost, self.params) # compute list of weights updates updates = OrderedDict() for accugrad, accudelta, param, gparam, trainable in zip(self._accugrads, self._accudeltas, self.params, gparams, self.trainables): # c.f. Algorithm 1 in the Adadelta paper (Zeiler 2012) agrad = self._rho * accugrad + (1 - self._rho) * gparam * gparam dx = - T.sqrt((accudelta + self._eps) / (agrad + self._eps)) * gparam * trainable updates[accudelta] = (self._rho * accudelta + (1 - self._rho) * dx * dx) if self.max_norm: W = param + dx col_norms = W.norm(2, axis=0) desired_norms = T.clip(col_norms, 0, self.max_norm) updates[param] = W * (desired_norms / (1e-6 + col_norms)) else: updates[param] = param + dx updates[accugrad] = agrad train_fn = theano.function(inputs=[theano.Param(batch_x), theano.Param(batch_y)], outputs=self.mean_cost, updates=updates, givens={self.x: batch_x, self.y: batch_y}) return train_fn
Example #8
Source File: nets.py From seizure-detection with MIT License | 5 votes |
def get_adagrad_trainer(self): """ Returns an Adagrad (Duchi et al. 2010) trainer using a learning rate. """ batch_x = T.fmatrix('batch_x') batch_y = T.ivector('batch_y') learning_rate = T.fscalar('lr') # learning rate to use # compute the gradients with respect to the model parameters gparams = T.grad(self.mean_cost, self.params) # compute list of weights updates updates = OrderedDict() for accugrad, param, gparam in zip(self._accugrads, self.params, gparams): # c.f. Algorithm 1 in the Adadelta paper (Zeiler 2012) agrad = accugrad + gparam * gparam dx = - (learning_rate / T.sqrt(agrad + self._eps)) * gparam if self.max_norm: W = param + dx col_norms = W.norm(2, axis=0) desired_norms = T.clip(col_norms, 0, self.max_norm) updates[param] = W * (desired_norms / (1e-6 + col_norms)) else: updates[param] = param + dx updates[accugrad] = agrad train_fn = theano.function(inputs=[theano.Param(batch_x), theano.Param(batch_y), theano.Param(learning_rate)], outputs=self.mean_cost, updates=updates, givens={self.x: batch_x, self.y: batch_y}) return train_fn
Example #9
Source File: nets.py From seizure-detection with MIT License | 5 votes |
def get_SGD_trainer(self): """ Returns a plain SGD minibatch trainer with learning rate as param. """ batch_x = T.fmatrix('batch_x') batch_y = T.ivector('batch_y') learning_rate = T.fscalar('lr') # learning rate to use # compute the gradients with respect to the model parameters # using mean_cost so that the learning rate is not too dependent # on the batch size gparams = T.grad(self.mean_cost, self.params) # compute list of weights updates updates = OrderedDict() for param, gparam in zip(self.params, gparams): if self.max_norm: W = param - gparam * learning_rate col_norms = W.norm(2, axis=0) desired_norms = T.clip(col_norms, 0, self.max_norm) updates[param] = W * (desired_norms / (1e-6 + col_norms)) else: updates[param] = param - gparam * learning_rate train_fn = theano.function(inputs=[theano.Param(batch_x), theano.Param(batch_y), theano.Param(learning_rate)], outputs=self.mean_cost, updates=updates, givens={self.x: batch_x, self.y: batch_y}) return train_fn
Example #10
Source File: tSNE_plots.py From kaggle-seizure-prediction with MIT License | 5 votes |
def plot_features(subject, data_path, model_path, test_labels, dataset='test'): with open(model_path + '/' + subject + '.pickle', 'rb') as f: state_dict = cPickle.load(f) cnn = ConvNet(state_dict['params']) cnn.set_weights(state_dict['weights']) scalers = state_dict['scalers'] if dataset == 'test': d = load_test_data(data_path, subject) x = d['x'] y = test_labels['preictal'] elif dataset == 'train': d = load_train_data(data_path, subject) x, y = d['x'], d['y'] else: raise ValueError('dataset') x, _ = scale_across_time(x, x_test=None, scalers=scalers) if state_dict['params']['scale_time'] \ else scale_across_features(x, x_test=None, scalers=scalers) cnn.batch_size.set_value(x.shape[0]) get_features = theano.function([cnn.x, Param(cnn.training_mode, default=0)], cnn.feature_extractor.output, allow_input_downcast=True) logits_test = get_features(x) model = TSNE(n_components=2, random_state=0) z = model.fit_transform(np.float64(logits_test)) plt.scatter(z[:, 0], z[:, 1], s=60, c=y) plt.show()
Example #11
Source File: sda.py From pdnn with Apache License 2.0 | 5 votes |
def pretraining_functions(self, train_set_x, batch_size): # 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 = [] for dA in self.dA_layers: # get the cost and the updates list cost, updates = dA.get_cost_updates(corruption_level, learning_rate) # 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
Example #12
Source File: dnn_sat.py From pdnn with Apache License 2.0 | 5 votes |
def build_finetune_functions(self, train_shared_xy, valid_shared_xy, batch_size): (train_set_x, train_set_y) = train_shared_xy (valid_set_x, valid_set_y) = valid_shared_xy index = T.lscalar('index') # index to a [mini]batch learning_rate = T.fscalar('learning_rate') momentum = T.fscalar('momentum') # compute the gradients with respect to the model parameters gparams = T.grad(self.finetune_cost, self.params) # compute list of fine-tuning updates updates = collections.OrderedDict() for dparam, gparam in zip(self.delta_params, gparams): updates[dparam] = momentum * dparam - gparam*learning_rate for dparam, param in zip(self.delta_params, self.params): updates[param] = param + updates[dparam] train_fn = theano.function(inputs=[index, theano.Param(learning_rate, default = 0.0001), theano.Param(momentum, default = 0.5)], outputs=self.errors, 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]}) valid_fn = theano.function(inputs=[index], outputs=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]}) return train_fn, valid_fn
Example #13
Source File: srbm.py From pdnn with Apache License 2.0 | 5 votes |
def pretraining_functions(self, train_set_x, batch_size, k , weight_cost): index = T.lscalar('index') momentum = T.scalar('momentum') learning_rate = T.scalar('lr') # number of mini-batches n_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size # start and end index of this mini-batch batch_begin = index * batch_size batch_end = batch_begin + batch_size pretrain_fns = [] for rbm in self.rbm_layers: r_cost, fe_cost, updates = rbm.get_cost_updates(batch_size, learning_rate, momentum, weight_cost, persistent=None, k = k) # compile the theano function fn = theano.function(inputs=[index, theano.Param(learning_rate, default=0.0001), theano.Param(momentum, default=0.5)], outputs= [r_cost, fe_cost], updates=updates, givens={self.x: train_set_x[batch_begin:batch_end]}) # append function to the list of functions pretrain_fns.append(fn) return pretrain_fns
Example #14
Source File: cnn_fast.py From pdnn with Apache License 2.0 | 5 votes |
def build_finetune_functions(self, train_shared_xy, valid_shared_xy, batch_size): (train_set_x, train_set_y) = train_shared_xy (valid_set_x, valid_set_y) = valid_shared_xy index = T.lscalar('index') # index to a [mini]batch learning_rate = T.fscalar('learning_rate') momentum = T.fscalar('momentum') # compute the gradients with respect to the model parameters gparams = T.grad(self.finetune_cost, self.params) # compute list of fine-tuning updates updates = collections.OrderedDict() for dparam, gparam in zip(self.delta_params, gparams): updates[dparam] = momentum * dparam - gparam*learning_rate for dparam, param in zip(self.delta_params, self.params): updates[param] = param + updates[dparam] train_fn = theano.function(inputs=[index, theano.Param(learning_rate, default = 0.0001), theano.Param(momentum, default = 0.5)], outputs=self.errors, 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]}) valid_fn = theano.function(inputs=[index], outputs=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]}) return train_fn, valid_fn
Example #15
Source File: dnn_2tower.py From pdnn with Apache License 2.0 | 5 votes |
def build_finetune_functions(self, train_shared_xy, valid_shared_xy, batch_size): (train_set_x, train_set_y) = train_shared_xy (valid_set_x, valid_set_y) = valid_shared_xy index = T.lscalar('index') # index to a [mini]batch learning_rate = T.fscalar('learning_rate') momentum = T.fscalar('momentum') # compute the gradients with respect to the model parameters gparams = T.grad(self.finetune_cost, self.params) # compute list of fine-tuning updates updates = collections.OrderedDict() for dparam, gparam in zip(self.delta_params, gparams): updates[dparam] = momentum * dparam - gparam*learning_rate for dparam, param in zip(self.delta_params, self.params): updates[param] = param + updates[dparam] train_fn = theano.function(inputs=[index, theano.Param(learning_rate, default = 0.0001), theano.Param(momentum, default = 0.5)], outputs=self.errors, 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]}) valid_fn = theano.function(inputs=[index], outputs=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]}) return train_fn, valid_fn
Example #16
Source File: cnn.py From pdnn with Apache License 2.0 | 5 votes |
def build_finetune_functions(self, train_shared_xy, valid_shared_xy, batch_size): (train_set_x, train_set_y) = train_shared_xy (valid_set_x, valid_set_y) = valid_shared_xy index = T.lscalar('index') # index to a [mini]batch learning_rate = T.fscalar('learning_rate') momentum = T.fscalar('momentum') # compute the gradients with respect to the model parameters gparams = T.grad(self.finetune_cost, self.params) # compute list of fine-tuning updates updates = collections.OrderedDict() for dparam, gparam in zip(self.delta_params, gparams): updates[dparam] = momentum * dparam - gparam*learning_rate for dparam, param in zip(self.delta_params, self.params): updates[param] = param + updates[dparam] train_fn = theano.function(inputs=[index, theano.Param(learning_rate, default = 0.0001), theano.Param(momentum, default = 0.5)], outputs=self.errors, 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]}) valid_fn = theano.function(inputs=[index], outputs=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]}) return train_fn, valid_fn
Example #17
Source File: cnn_sat.py From pdnn with Apache License 2.0 | 5 votes |
def build_finetune_functions(self, train_shared_xy, valid_shared_xy, batch_size): (train_set_x, train_set_y) = train_shared_xy (valid_set_x, valid_set_y) = valid_shared_xy index = T.lscalar('index') # index to a [mini]batch learning_rate = T.fscalar('learning_rate') momentum = T.fscalar('momentum') # compute the gradients with respect to the model parameters gparams = T.grad(self.finetune_cost, self.params) # compute list of fine-tuning updates updates = collections.OrderedDict() for dparam, gparam in zip(self.delta_params, gparams): updates[dparam] = momentum * dparam - gparam*learning_rate for dparam, param in zip(self.delta_params, self.params): updates[param] = param + updates[dparam] train_fn = theano.function(inputs=[index, theano.Param(learning_rate, default = 0.0001), theano.Param(momentum, default = 0.5)], outputs=self.errors, 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]}) valid_fn = theano.function(inputs=[index], outputs=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]}) return train_fn, valid_fn
Example #18
Source File: dnn.py From pdnn with Apache License 2.0 | 5 votes |
def build_finetune_functions_kaldi(self, train_shared_xy, valid_shared_xy): (train_set_x, train_set_y) = train_shared_xy (valid_set_x, valid_set_y) = valid_shared_xy index = T.lscalar('index') # index to a [mini]batch learning_rate = T.fscalar('learning_rate') momentum = T.fscalar('momentum') # compute the gradients with respect to the model parameters gparams = T.grad(self.finetune_cost, self.params) # compute list of fine-tuning updates updates = collections.OrderedDict() for dparam, gparam in zip(self.delta_params, gparams): updates[dparam] = momentum * dparam - gparam*learning_rate for dparam, param in zip(self.delta_params, self.params): updates[param] = param + updates[dparam] if self.max_col_norm is not None: for i in xrange(self.hidden_layers_number): W = self.layers[i].W if W in updates: updated_W = updates[W] col_norms = T.sqrt(T.sum(T.sqr(updated_W), axis=0)) desired_norms = T.clip(col_norms, 0, self.max_col_norm) updates[W] = updated_W * (desired_norms / (1e-7 + col_norms)) train_fn = theano.function(inputs=[theano.Param(learning_rate, default = 0.0001), theano.Param(momentum, default = 0.5)], outputs=self.errors, updates=updates, givens={self.x: train_set_x, self.y: train_set_y}) valid_fn = theano.function(inputs=[], outputs=self.errors, givens={self.x: valid_set_x, self.y: valid_set_y}) return train_fn, valid_fn
Example #19
Source File: Model.py From SurvivalNet with Apache License 2.0 | 5 votes |
def pretraining_functions(self, pretrain_x, batch_size): index = T.lscalar('index') # index to a minibatch corruption_level = T.scalar('corruption') # % of corruption learning_rate = T.scalar('lr') # learning rate if 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_x = pretrain_x[batch_begin: batch_end] pretrain_fns = [] is_train = numpy.cast['int32'](0) # value does not matter for dA_layer in self.dA_layers: # get the cost and the updates list cost, updates = dA_layer.get_cost_updates(corruption_level, learning_rate) # compile the theano function fn = theano.function( on_unused_input='ignore', inputs=[ index, theano.Param(corruption_level, default=0.2), theano.Param(learning_rate, default=0.1) ], outputs=cost, updates=updates, givens={ self.x: pretrain_x, self.is_train: is_train } ) pretrain_fns.append(fn) return pretrain_fns
Example #20
Source File: Model.py From Deep-Neural-Networks-HealthCare with MIT License | 5 votes |
def pretraining_functions(self, pretrain_x, batch_size): index = T.lscalar('index') # index to a minibatch corruption_level = T.scalar('corruption') # % of corruption learning_rate = T.scalar('lr') # learning rate if 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_x = pretrain_x[batch_begin: batch_end] pretrain_fns = [] is_train = numpy.cast['int32'](0) # value does not matter for dA_layer in self.dA_layers: # get the cost and the updates list cost, updates = dA_layer.get_cost_updates(corruption_level, learning_rate) # compile the theano function fn = theano.function( on_unused_input='ignore', inputs=[ index, theano.Param(corruption_level, default=0.2), theano.Param(learning_rate, default=0.1) ], outputs=cost, updates=updates, givens={ self.x: pretrain_x, self.is_train: is_train } ) pretrain_fns.append(fn) return pretrain_fns
Example #21
Source File: dnn.py From DL4H with MIT License | 5 votes |
def score_classif(self, given_set): """ Returns functions to get current classification errors. """ batch_x = T.fmatrix('batch_x') batch_y = T.ivector('batch_y') score = theano.function(inputs=[theano.Param(batch_x), theano.Param(batch_y)], outputs=self.errors, givens={self.x: batch_x, self.y: batch_y}) def scoref(): """ returned function that scans the entire set given as input """ return [score(batch_x, batch_y) for batch_x, batch_y in given_set] return scoref
Example #22
Source File: dnn.py From DL4H with MIT License | 5 votes |
def get_rmsprop_trainer(self, with_step_adapt=True, nesterov=False): # TODO Nesterov momentum """ Returns an RmsProp (possibly Nesterov) (Sutskever 2013) trainer using self._rho, self._eps and self._momentum params. """ # TODO CHECK batch_x = T.fmatrix('batch_x') batch_y = T.ivector('batch_y') learning_rate = T.fscalar('lr') # learning rate gparams = T.grad(self.mean_cost, self.params) updates = OrderedDict() for accugrad, avggrad, accudelta, sa, param, gparam in zip( self._accugrads, self._avggrads, self._accudeltas, self._stepadapts, self.params, gparams): acc_grad = self._rho * accugrad + (1 - self._rho) * gparam * gparam avg_grad = self._rho * avggrad + (1 - self._rho) * gparam # this decay/discount (self._rho) should differ from the one of the line above ###scaled_grad = gparam / T.sqrt(acc_grad + self._eps) # original RMSprop gradient scaling scaled_grad = gparam / T.sqrt(acc_grad - avg_grad**2 + self._eps) # Alex Graves' RMSprop variant (divide by a "running stddev" of the updates) if with_step_adapt: incr = sa * (1. + self._stepadapt_alpha) #decr = sa * (1. - self._stepadapt_alpha) decr = sa * (1. - 2*self._stepadapt_alpha) ###steps = sa * T.switch(accudelta * -gparam >= 0, incr, decr) steps = T.clip(T.switch(accudelta * -gparam >= 0, incr, decr), self._eps, 1./self._eps) # bad overloading of self._eps! scaled_grad = steps * scaled_grad updates[sa] = steps dx = self._momentum * accudelta - learning_rate * scaled_grad updates[param] = param + dx updates[accugrad] = acc_grad updates[avggrad] = avg_grad updates[accudelta] = dx train_fn = theano.function(inputs=[theano.Param(batch_x), theano.Param(batch_y), theano.Param(learning_rate)], outputs=self.mean_cost, updates=updates, givens={self.x: batch_x, self.y: batch_y}) return train_fn
Example #23
Source File: nolearn_net.py From kaggle-right-whale with MIT License | 5 votes |
def transform(self, X, target_layer_name, y=None): target_layer = self.layers_[target_layer_name] layers = self.layers_ input_layers = [ layer for layer in layers.values() if isinstance(layer, nn.layers.InputLayer) ] X_inputs = [ theano.Param(input_layer.input_var, name=input_layer.name) for input_layer in input_layers ] target_layer_output = nn.layers.get_output( target_layer, None, deterministic=True ) transform_iter = theano.function( inputs=X_inputs, outputs=target_layer_output, allow_input_downcast=True, ) outputs = [] for Xb, yb in self.batch_iterator_test(X): outputs.append(self.apply_batch_func(transform_iter, Xb)) return np.vstack(outputs)
Example #24
Source File: SdA.py From deeplearn_hsi with BSD 2-Clause "Simplified" License | 4 votes |
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 = [] for dA in self.dA_layers: # get the cost and the updates list cost, updates = dA.get_cost_updates(corruption_level, learning_rate) # 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
Example #25
Source File: drn.py From pdnn with Apache License 2.0 | 4 votes |
def build_finetune_functions(self, train_shared_xy, valid_shared_xy, batch_size): (train_set_x, train_set_y) = train_shared_xy (valid_set_x, valid_set_y) = valid_shared_xy index = T.lscalar('index') # index to a [mini]batch learning_rate = T.fscalar('learning_rate') momentum = T.fscalar('momentum') # compute the gradients with respect to the model parameters gparams = T.grad(self.finetune_cost, self.params) # compute list of fine-tuning updates updates = collections.OrderedDict() for dparam, gparam in zip(self.delta_params, gparams): updates[dparam] = momentum * dparam - gparam*learning_rate for dparam, param in zip(self.delta_params, self.params): updates[param] = param + updates[dparam] if self.max_col_norm is not None: for i in xrange(self.hidden_layers_number): W = self.layers[i].W if W in updates: updated_W = updates[W] col_norms = T.sqrt(T.sum(T.sqr(updated_W), axis=0)) desired_norms = T.clip(col_norms, 0, self.max_col_norm) updates[W] = updated_W * (desired_norms / (1e-7 + col_norms)) train_fn = theano.function(inputs=[index, theano.Param(learning_rate, default = 0.0001), theano.Param(momentum, default = 0.5)], outputs=self.errors, 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]}) valid_fn = theano.function(inputs=[index], outputs=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]}) return train_fn, valid_fn
Example #26
Source File: dnn.py From pdnn with Apache License 2.0 | 4 votes |
def build_finetune_functions(self, train_shared_xy, valid_shared_xy, batch_size): (train_set_x, train_set_y) = train_shared_xy (valid_set_x, valid_set_y) = valid_shared_xy index = T.lscalar('index') # index to a [mini]batch learning_rate = T.fscalar('learning_rate') momentum = T.fscalar('momentum') # compute the gradients with respect to the model parameters gparams = T.grad(self.finetune_cost, self.params) # compute list of fine-tuning updates updates = collections.OrderedDict() for dparam, gparam in zip(self.delta_params, gparams): updates[dparam] = momentum * dparam - gparam*learning_rate for dparam, param in zip(self.delta_params, self.params): updates[param] = param + updates[dparam] if self.max_col_norm is not None: for i in xrange(self.hidden_layers_number): W = self.layers[i].W if W in updates: updated_W = updates[W] col_norms = T.sqrt(T.sum(T.sqr(updated_W), axis=0)) desired_norms = T.clip(col_norms, 0, self.max_col_norm) updates[W] = updated_W * (desired_norms / (1e-7 + col_norms)) train_fn = theano.function(inputs=[index, theano.Param(learning_rate, default = 0.0001), theano.Param(momentum, default = 0.5)], outputs=self.errors, 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]}) valid_fn = theano.function(inputs=[index], outputs=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]}) return train_fn, valid_fn
Example #27
Source File: ae_stacked.py From dnnmapper with GNU General Public License v2.0 | 4 votes |
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 # 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 = [] for dA in self.dA_layers: # get the cost and the updates list cost, updates = dA.get_cost_updates(corruption_level, learning_rate) # 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
Example #28
Source File: conv_net.py From kaggle-seizure-prediction with MIT License | 4 votes |
def __init__(self, param_dict): self.param_dict = param_dict self.training_batch_size = param_dict['training_batch_size'] nkerns = param_dict['nkerns'] recept_width = param_dict['recept_width'] pool_width = param_dict['pool_width'] stride = param_dict['stride'] dropout_prob = param_dict['dropout_prob'] weight_decay = param_dict['l2_reg'] activation = param_dict['activation'] weights_variance = param_dict['weights_variance'] n_channels = param_dict['n_channels'] n_timesteps = param_dict['n_timesteps'] n_fbins = param_dict['n_fbins'] global_pooling = param_dict['global_pooling'] rng = np.random.RandomState(23455) self.training_mode = T.iscalar('training_mode') self.x = T.tensor4('x') self.y = T.bvector('y') self.batch_size = theano.shared(self.training_batch_size) self.input = self.x.reshape((self.batch_size, 1, n_channels * n_fbins, n_timesteps)) self.feature_extractor = FeatureExtractor(rng, self.input, nkerns, recept_width, pool_width, stride, self.training_mode, dropout_prob[0], activation, weights_variance, n_channels, n_timesteps, n_fbins, global_pooling) self.classifier = SoftmaxLayer(rng=rng, input=self.feature_extractor.output, n_in=nkerns[-1], training_mode=self.training_mode, dropout_prob=dropout_prob[-1]) self.weights = self.feature_extractor.weights + self.classifier.weights # ---------------------- BACKPROP self.cost = self.classifier.cross_entropy_cost(self.y) self.cost = self.classifier.cross_entropy_cost(self.y) L2_sqr = sum((weight ** 2).sum() for weight in self.weights[::2]) self.grads = T.grad(self.cost + weight_decay * L2_sqr, self.weights) self.updates = self.adadelta_updates(self.grads, self.weights) # self.updates = self.nesterov_momentum(self.grads, self.weights) # --------------------- FUNCTIONS self.train_model = theano.function([self.x, self.y, Param(self.training_mode, default=1)], outputs=self.cost, updates=self.updates) self.validate_model = theano.function([self.x, self.y, Param(self.training_mode, default=0)], self.cost) self.test_model = theano.function([self.x, Param(self.training_mode, default=0)], self.classifier.p_y_given_x[:, 1])
Example #29
Source File: nn.py From kaggle_diabetic with MIT License | 4 votes |
def _create_iter_funcs(self, layers, objective, update, output_type): y_batch = output_type('y_batch') output_layer = list(layers.values())[-1] objective_params = self._get_params_for('objective') obj = objective(output_layer, **objective_params) if not hasattr(obj, 'layers'): # XXX breaking the Lasagne interface a little: obj.layers = layers loss_train = obj.get_loss(None, y_batch) loss_eval = obj.get_loss(None, y_batch, deterministic=True) predict_proba = get_output(output_layer, None, deterministic=True) try: transform = get_output([v for k, v in layers.items() if 'rmspool' in k or 'maxpool' in k][-1], None, deterministic=True) except IndexError: transform = get_output(layers.values()[-2], None, deterministic=True) if not self.regression: predict = predict_proba.argmax(axis=1) accuracy = T.mean(T.eq(predict, y_batch)) else: accuracy = loss_eval all_params = self.get_all_params(trainable=True) update_params = self._get_params_for('update') updates = update(loss_train, all_params, **update_params) input_layers = [layer for layer in layers.values() if isinstance(layer, InputLayer)] X_inputs = [theano.Param(input_layer.input_var, name=input_layer.name) for input_layer in input_layers] inputs = X_inputs + [theano.Param(y_batch, name="y")] train_iter = theano.function( inputs=inputs, outputs=[loss_train], updates=updates, ) eval_iter = theano.function( inputs=inputs, outputs=[loss_eval, accuracy], ) predict_iter = theano.function( inputs=X_inputs, outputs=predict_proba, ) transform_iter = theano.function( inputs=X_inputs, outputs=transform, ) return train_iter, eval_iter, predict_iter, transform_iter
Example #30
Source File: DBN_for_DTIs.py From DeepDTIs_DBN with GNU General Public License v3.0 | 4 votes |
def pretraining_functions(self, train_set_x, batch_size, k): '''Generates a list of functions, for performing one step of gradient descent at a given layer. The function will require as input the minibatch index, and to train an RBM 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 var. that contains all datapoints used for training the RBM :type batch_size: int :param batch_size: size of a [mini]batch :param k: number of Gibbs steps to do in CD-k / PCD-k ''' # index to a [mini]batch index = T.lscalar('index') # index to a minibatch 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 = [] for rbm in self.rbm_layers: # get the cost and the updates list # using CD-k here (persisent=None) for training each RBM. # TODO: change cost function to reconstruction error cost, updates = rbm.get_cost_updates(learning_rate, persistent=None, k=k) # compile the theano function fn = theano.function( inputs=[index, 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