提交 ef750931 authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Merged

...@@ -190,10 +190,19 @@ Extending your Module with Python methods ...@@ -190,10 +190,19 @@ Extending your Module with Python methods
========================================= =========================================
Let's say we want to add a method to our accumulator to print out the Let's say we want to add a method to our accumulator to print out the
state and we want to call it ``print_state``. All we need to do is to state and we want to call it ``print_state``. There are two mechanisms to do
give a method called ``_instance_print_state`` to our Module. this: let's call them instance methods and InstanceType.
Mechanism 1: _instance_method
-----------------------------
This is the preferred way of adding a few instance methods with a minimum of
boilerplate code.
All we need to do to use this mechanism is to give a method called
``_instance_print_state`` to our Module class.
.. code-block:: python .. code-block:: python
class Accumulator(Module): class Accumulator(Module):
...@@ -219,15 +228,56 @@ give a method called ``_instance_print_state`` to our Module. ...@@ -219,15 +228,56 @@ give a method called ``_instance_print_state`` to our Module.
acc.print_state() # --> prints "state is: 0.0" acc.print_state() # --> prints "state is: 0.0"
Any method called like ``_instance_XXX`` will variable in the object Any method called like ``_instance_XXX`` will cause the object
obtained through a call to ``make`` to gain an ``XXX`` method. Note obtained through a call to ``make`` to have a method called ``XXX``.
that when we define ``_instance_print_state`` there are two "self" Note that when we define ``_instance_print_state`` there are two "self"
arguments: ``self`` which is *symbolic* and ``obj`` which contains arguments: ``self`` which is *symbolic* and ``obj`` which is the compiled
*data*. Therefore, ``self.state`` is the symbolic state variable and object (the one that contains values).
Hint:``self.state`` is the symbolic state variable and
prints out as "state", whereas ``obj.state`` is the state's actual prints out as "state", whereas ``obj.state`` is the state's actual
value in the accumulator and prints out as "0.0". value in the accumulator and prints out as "0.0".
Mechanism 2: InstanceType
-------------------------
If a number of instance methods are going to be defined, and especially if you
will want to inherit from the kind of class that gets instantiated by make,
you might prefer to consider using the InstanceType mechanism.
.. code-block:: python
class AccumulatorInstance(ModuleInstance):
def print_state(self):
#self.component points to the Module from which this was compiled.
print '%s is: %s' % (self.component.state, self.state)
class Accumulator(Module):
# This line tells theano to instantiate an AccumulatorInstance
# when make() is called.
InstanceType = AccumulatorInstance
def __init__(self):
super(Accumulator, self).__init__() # don't forget this
self.inc = T.dscalar()
self.state = T.dscalar()
self.new_state = self.inc + self.state
self.add = Method(inputs = self.inc,
outputs = self.new_state,
updates = {self.state: self.new_state})
self.sub = Method(inputs = self.inc,
outputs = None,
updates = {self.state: self.state - self.inc})
m = Accumulator()
acc = m.make(state = 0)
acc.print_state() # --> prints "state is: 0.0"
Adding custom initialization Adding custom initialization
============================ ============================
...@@ -281,8 +331,28 @@ initialize a state with a matrix of zeros: ...@@ -281,8 +331,28 @@ initialize a state with a matrix of zeros:
Nesting Modules Nesting Modules
=============== ===============
WRITEME Probably the most powerful feature of theano's modules is that one can be
included as an attribute to another so that the storage of each is available
to both.
.. code-block:: python
M = theano.Module()
M.a, M.b, M.c = [theano.dvector() for i in 1,2,3]
P = theano.Module()
P.m = M #include a module by nesting
x = theano.dvector()
P.f = Method([x], None, {M.b: M.b + x})
p = P.make() #this converts both M and P because M was nested within P
p.m.b = [4, 5, 6]
p.f(3)
print p.m.b
# prints array([7.,8.,9.])
As you read through examples of Theano code, you will probably see many
instances of Modules being nested in this way.
......
...@@ -108,7 +108,7 @@ Once you have completed these steps, you should run the tests like this: ...@@ -108,7 +108,7 @@ Once you have completed these steps, you should run the tests like this:
nosetests #execute all the tests nosetests #execute all the tests
All tests should pass. If some test fails on your machine, you are All tests should pass. If some test fails on your machine, you are
encouraged to tell us what went wrong on the :ref:`theano-users` mailing encouraged to tell us what went wrong on the ``theano-users@googlegroups.com`` mailing
list. list.
To update your library to the latest revision, change directory (``cd``) To update your library to the latest revision, change directory (``cd``)
...@@ -147,11 +147,11 @@ automatic code generation, but that way is much, much slower. ...@@ -147,11 +147,11 @@ automatic code generation, but that way is much, much slower.
:api:`theano.compile.mode`. :api:`theano.compile.mode`.
Possible values so far are: Possible values so far are:
- FAST_COMPILE, - 'FAST_COMPILE'
- FAST_RUN and - 'FAST_RUN'
- DEBUG_MODE. - 'DEBUG_MODE'
Omitting this variable defaults the mode to FAST_RUN. Omitting this variable defaults the mode to 'FAST_RUN'.
- `THEANO_UNITTEST_SEED`: - `THEANO_UNITTEST_SEED`:
An integer value specifying which seed should be used when An integer value specifying which seed should be used when
......
.. _lisa_labo:: .. _lisa_labo:
=============================== ===============================
LISA Labo specific instructions LISA Labo specific instructions
......
import scipy.sparse
from theano.sparse import * from theano.sparse import *
import random import random
...@@ -142,8 +143,10 @@ class T_conversion(unittest.TestCase): ...@@ -142,8 +143,10 @@ class T_conversion(unittest.TestCase):
self.failUnless(val.format == 'csr') self.failUnless(val.format == 'csr')
def test2(self): def test2(self):
#call dense_from_sparse
for t in _mtypes: for t in _mtypes:
s = t((2,5)) s = t((2,5))
s = t(scipy.sparse.identity(5))
d = dense_from_sparse(s) d = dense_from_sparse(s)
s[0,0] = 1.0 s[0,0] = 1.0
val = eval_outputs([d]) val = eval_outputs([d])
...@@ -161,11 +164,12 @@ class test_structureddot(unittest.TestCase): ...@@ -161,11 +164,12 @@ class test_structureddot(unittest.TestCase):
# iterate 10 times just to make sure (cannot get this wrong !) # iterate 10 times just to make sure (cannot get this wrong !)
for i in range(10): for i in range(10):
spmat = sp.csc_matrix((4,6)) spmat = sp.lil_matrix((4,6))
for i in range(5): for i in range(5):
x = numpy.floor(numpy.random.rand()*spmat.shape[0]) x = numpy.floor(numpy.random.rand()*spmat.shape[0])
y = numpy.floor(numpy.random.rand()*spmat.shape[1]) y = numpy.floor(numpy.random.rand()*spmat.shape[1])
spmat[x,y] = numpy.random.rand()*10 spmat[x,y] = numpy.random.rand()*10
spmat = sp.csc_matrix(spmat)
kerns = tensor.dvector('kerns') kerns = tensor.dvector('kerns')
images = tensor.dmatrix('images') images = tensor.dmatrix('images')
......
...@@ -255,8 +255,8 @@ class Loss01(object): ...@@ -255,8 +255,8 @@ class Loss01(object):
def loss_01(self, x, targ): def loss_01(self, x, targ):
return N.mean(self.classify(x) != targ) return N.mean(self.classify(x) != targ)
class LogRegInstanceType(module.FancyModuleInstance): class Module_Nclass(module.FancyModule):
def initialize(self, n_in, n_out, lr, seed): def _instance_initialize(mod_self, self, n_in, n_out, lr, seed):
#self.component is the LogisticRegressionTemplate instance that built this guy. #self.component is the LogisticRegressionTemplate instance that built this guy.
""" """
@todo: Remove seed. Used only to keep Stacker happy. @todo: Remove seed. Used only to keep Stacker happy.
...@@ -269,9 +269,6 @@ class LogRegInstanceType(module.FancyModuleInstance): ...@@ -269,9 +269,6 @@ class LogRegInstanceType(module.FancyModuleInstance):
self.input_dimension = n_in self.input_dimension = n_in
self.output_dimension = n_out self.output_dimension = n_out
class Module_Nclass(module.FancyModule):
InstanceType = LogRegInstanceType
def __init__(self, x=None, targ=None, w=None, b=None, lr=None, regularize=False): def __init__(self, x=None, targ=None, w=None, b=None, lr=None, regularize=False):
super(Module_Nclass, self).__init__() #boilerplate super(Module_Nclass, self).__init__() #boilerplate
...@@ -324,49 +321,7 @@ class Module_Nclass(module.FancyModule): ...@@ -324,49 +321,7 @@ class Module_Nclass(module.FancyModule):
#self.update = module.Method([self.input, self.targ], sum_xent, #self.update = module.Method([self.input, self.targ], sum_xent,
#updates = dict((p, p - self.lr * g) for p, g in zip(self.params, gparams))) #updates = dict((p, p - self.lr * g) for p, g in zip(self.params, gparams)))
class ConvolutionalMLPInstance(module.FancyModuleInstance, Loss01):
#initialize is called by Module.make
def initialize(self, input_size, input_representation_size, hidden_representation_size, output_size, lr, seed, noise_level, qfilter_relscale):
R = N.random.RandomState(unittest_tools.fetch_seed(seed))
self.input_size = input_size
self.input_representation_size = input_representation_size
self.hidden_representation_size = hidden_representation_size
self.output_size = output_size
self.lr = lr
# for layer in obj.layers:
# if layer.lr is None:
# layer.lr = lr
assert self.input_representations[-1] is not self.input_representations[0]
assert self.input_representations[-1].w1 is self.input_representations[0].w1
for i in self.input_representations:
# i.initialize(input_size=self.input_size, hidden_size=self.input_representation_size, seed=R.random_integers(2**30), noise_level=noise_level, qfilter_relscale=qfilter_relscale)
i.initialize(input_size=self.input_size,
hidden_size=self.input_representation_size, noise_level=noise_level,
seed=int(R.random_integers(2**30)), lr=lr, qfilter_relscale=qfilter_relscale)
print type(i.w1)
assert isinstance(i.w1, N.ndarray)
for i in self.input_representations[1:]:
print type(i.w1)
assert isinstance(i.w1, N.ndarray)
assert (i.w1 == self.input_representations[0].w1).all()
assert (i.w2 == self.input_representations[0].w2).all()
assert (i.b1 == self.input_representations[0].b1).all()
assert (i.b2 == self.input_representations[0].b2).all()
assert all((a==b).all() for a, b in zip(i.qfilters, self.input_representations[0].qfilters))
self.hidden.initialize(input_size=(len(self.inputs) * self.input_representation_size),
hidden_size=self.hidden_representation_size, noise_level=noise_level,
seed=int(R.random_integers(2**30)), lr=lr, qfilter_relscale=qfilter_relscale)
self.output.initialize(n_in=self.hidden_representation_size, n_out=self.output_size, lr=lr, seed=R.random_integers(2**30))
class ConvolutionalMLP(module.FancyModule): class ConvolutionalMLP(module.FancyModule):
InstanceType = ConvolutionalMLPInstance
def __init__(self, def __init__(self,
window_size, window_size,
n_quadratic_filters, n_quadratic_filters,
...@@ -459,6 +414,45 @@ class ConvolutionalMLP(module.FancyModule): ...@@ -459,6 +414,45 @@ class ConvolutionalMLP(module.FancyModule):
#self.validate = module.Method(self.inputs + [self.targ], [self.output.cost, self.output.argmax, self.output.max_pr]) #self.validate = module.Method(self.inputs + [self.targ], [self.output.cost, self.output.argmax, self.output.max_pr])
#self.softmax_output = module.Method(self.inputs, self.output.softmax_unsupervised) #self.softmax_output = module.Method(self.inputs, self.output.softmax_unsupervised)
def _instance_initialize(mod_self, self, input_size, input_representation_size, hidden_representation_size, output_size, lr, seed, noise_level, qfilter_relscale):
R = N.random.RandomState(unittest_tools.fetch_seed(seed))
self.input_size = input_size
self.input_representation_size = input_representation_size
self.hidden_representation_size = hidden_representation_size
self.output_size = output_size
self.lr = lr
# for layer in obj.layers:
# if layer.lr is None:
# layer.lr = lr
assert self.input_representations[-1] is not self.input_representations[0]
assert self.input_representations[-1].w1 is self.input_representations[0].w1
for i in self.input_representations:
# i.initialize(input_size=self.input_size, hidden_size=self.input_representation_size, seed=R.random_integers(2**30), noise_level=noise_level, qfilter_relscale=qfilter_relscale)
i.initialize(input_size=self.input_size,
hidden_size=self.input_representation_size, noise_level=noise_level,
seed=int(R.random_integers(2**30)), lr=lr, qfilter_relscale=qfilter_relscale)
print type(i.w1)
assert isinstance(i.w1, N.ndarray)
for i in self.input_representations[1:]:
print type(i.w1)
assert isinstance(i.w1, N.ndarray)
assert (i.w1 == self.input_representations[0].w1).all()
assert (i.w2 == self.input_representations[0].w2).all()
assert (i.b1 == self.input_representations[0].b1).all()
assert (i.b2 == self.input_representations[0].b2).all()
assert all((a==b).all() for a, b in zip(i.qfilters, self.input_representations[0].qfilters))
self.hidden.initialize(input_size=(len(self.inputs) * self.input_representation_size),
hidden_size=self.hidden_representation_size, noise_level=noise_level,
seed=int(R.random_integers(2**30)), lr=lr, qfilter_relscale=qfilter_relscale)
self.output.initialize(n_in=self.hidden_representation_size, n_out=self.output_size, lr=lr, seed=R.random_integers(2**30))
def create(window_size=3, def create(window_size=3,
input_dimension=9, input_dimension=9,
output_vocabsize=8, output_vocabsize=8,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论