Mercurial > pylearn
comparison sandbox/gradient_learner.py @ 426:d7611a3811f2
Moved incomplete stuff to sandbox
author | Yoshua Bengio <bengioy@iro.umontreal.ca> |
---|---|
date | Tue, 22 Jul 2008 15:20:25 -0400 |
parents | gradient_learner.py@46c5c90019c2 |
children |
comparison
equal
deleted
inserted
replaced
425:e2b46a8f2b7b | 426:d7611a3811f2 |
---|---|
1 | |
2 from learner import * | |
3 from tensor import * | |
4 import gradient | |
5 from compile import Function | |
6 | |
7 class GradientLearner(Learner): | |
8 """ | |
9 Base class for gradient-based optimization of a training criterion | |
10 that can consist in two parts, an additive part over examples, and | |
11 an example-independent part (usually called the regularizer). | |
12 The user provides a Theano formula that maps the fields of a minibatch (each being a tensor with the | |
13 same number of rows = minibatch size) and parameters to output fields (for the use function), one of which | |
14 must be a cost that is the training criterion to be minimized. Subclasses implement | |
15 a training strategy that uses the Theano formula to compute gradients and | |
16 to compute outputs in the update method. | |
17 The inputs, parameters, and outputs are lists of Theano tensors, | |
18 while the example_wise_cost and regularization_term are Theano tensors. | |
19 The user can specify a regularization coefficient that multiplies the regularization term. | |
20 The training algorithm looks for parameters that minimize | |
21 regularization_coefficient * regularization_term(parameters) + | |
22 sum_{inputs in training_set} example_wise_cost(inputs,parameters) | |
23 i.e. the regularization_term should not depend on the inputs, only on the parameters. | |
24 The learned function can map a subset of inputs to a subset of outputs (as long as the inputs subset | |
25 includes all the inputs required in the Theano expression for the selected outputs). | |
26 It is assumed that all the inputs are provided in the training set (as dataset fields | |
27 with the corresponding name), but not necessarily when using the learned function. | |
28 """ | |
29 def __init__(self, inputs, parameters, outputs, example_wise_cost, regularization_term=astensor(0.0), | |
30 regularization_coefficient = astensor(1.0)): | |
31 self.inputs = inputs | |
32 self.outputs = outputs | |
33 self.parameters = parameters | |
34 self.example_wise_cost = example_wise_cost | |
35 self.regularization_term = regularization_term | |
36 self.regularization_coefficient = regularization_coefficient | |
37 self.parameters_example_wise_gradient = gradient.grad(example_wise_cost, parameters) | |
38 self.parameters_regularization_gradient = gradient.grad(self.regularization_coefficient * regularization_term, parameters) | |
39 if example_wise_cost not in outputs: | |
40 outputs.append(example_wise_cost) | |
41 if regularization_term not in outputs: | |
42 outputs.append(regularization_term) | |
43 self.example_wise_gradient_fn = Function(inputs + parameters, | |
44 [self.parameters_example_wise_gradient + self.parameters_regularization_gradient]) | |
45 self.use_functions = {frozenset([input.name for input in inputs]+[output.name for output in outputs]) | |
46 : Function(inputs, outputs)} | |
47 | |
48 def use(self,input_dataset,output_fields=None,copy_inputs=True): | |
49 # obtain the function that maps the desired inputs to desired outputs | |
50 input_fields = input_dataset.fieldNames() | |
51 # map names of input fields to Theano tensors in self.inputs | |
52 input_variables = ??? | |
53 if output_fields is None: output_fields = [output.name for output in outputs] | |
54 # handle special case of inputs that are directly copied into outputs | |
55 # map names of output fields to Theano tensors in self.outputs | |
56 output_variables = ??? | |
57 use_function_key = input_fields+output_fields | |
58 if not self.use_functions.has_key(use_function_key): | |
59 self.use_function[use_function_key]=Function(input_variables,output_variables) | |
60 use_function = self.use_functions[use_function_key] | |
61 # return a dataset that computes the outputs | |
62 return input_dataset.apply_function(use_function,input_fields,output_fields,copy_inputs,compute_now=True) | |
63 | |
64 | |
65 class StochasticGradientDescent(object): | |
66 def update_parameters(self): | |
67 | |
68 class StochasticGradientLearner(GradientLearner,StochasticGradientDescent): | |
69 def __init__(self,inputs, parameters, outputs, example_wise_cost, regularization_term=astensor(0.0), | |
70 regularization_coefficient = astensor(1.0),) | |
71 def update() |