annotate gradient_learner.py @ 14:5ede27026e05

Working on gradient_based_learner
author bengioy@bengiomac.local
date Wed, 26 Mar 2008 22:56:13 -0400
parents 633453635d51
children 266c68cb6136
rev   line source
13
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
1
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
2 from learner import *
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
3 from tensor import *
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
4 import gradient
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
5 from compile import Function
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
6 from gradient_based_optimizer import *
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
7
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
8 class GradientLearner(Learner):
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
9 """
14
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
10 Base class for gradient-based optimization of a training criterion
13
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
11 that can consist in two parts, an additive part over examples, and
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
12 an example-independent part (usually called the regularizer).
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
13 The user provides a Theano formula that maps the fields of a training example
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
14 and parameters to output fields (for the use function), one of which must be a cost
14
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
15 that is the training criterion to be minimized. Subclasses implement
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
16 a training strategy that uses the function to compute gradients and
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
17 to compute outputs in the update method.
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
18 The inputs, parameters, and outputs are lists of Theano tensors,
13
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
19 while the example_wise_cost and regularization_term are Theano tensors.
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
20 The user can specify a regularization coefficient that multiplies the regularization term.
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
21 The training algorithm looks for parameters that minimize
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
22 regularization_coefficienet * regularization_term(parameters) +
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
23 sum_{inputs in training_set} example_wise_cost(inputs,parameters)
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
24 i.e. the regularization_term should not depend on the inputs, only on the parameters.
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
25 The learned function can map a subset of inputs to a subset of outputs (as long as the inputs subset
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
26 includes all the inputs required in the Theano expression for the selected outputs).
14
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
27 It is assumed that all the inputs are provided in the training set, but
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
28 not necessarily when using the learned function.
13
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
29 """
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
30 def __init__(self, inputs, parameters, outputs, example_wise_cost, regularization_term,
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
31 gradient_based_optimizer=StochasticGradientDescent(), regularization_coefficient = astensor(1.0)):
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
32 self.inputs = inputs
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
33 self.outputs = outputs
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
34 self.parameters = parameters
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
35 self.example_wise_cost = example_wise_cost
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
36 self.regularization_term = regularization_term
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
37 self.gradient_based_optimizer = gradient_based_optimizer
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
38 self.regularization_coefficient = regularization_coefficient
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
39 self.parameters_example_wise_gradient = gradient.grad(example_wise_cost, parameters)
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
40 self.parameters_regularization_gradient = gradient.grad(self.regularization_coefficient * regularization, parameters)
14
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
41 if example_wise_cost not in outputs:
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
42 outputs.append(example_wise_cost)
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
43 if regularization_term not in outputs:
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
44 outputs.append(regularization_term)
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
45 self.example_wise_gradient_fn = Function(inputs + parameters,
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
46 [self.parameters_example_wise_gradient + self.parameters_regularization_gradient])
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
47 self.use_functions = {frozenset([input.name for input in inputs]) : Function(inputs, outputs)}
13
633453635d51 Starting to work on gradient_based_learner.py
bengioy@bengiomac.local
parents:
diff changeset
48
14
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
49 def update(self,training_set):
5ede27026e05 Working on gradient_based_learner
bengioy@bengiomac.local
parents: 13
diff changeset
50