提交 fa7e8344 authored 作者: James Bergstra's avatar James Bergstra

changes to basic_tutorial/module.txt: removed Member, External; add second method to example

上级 ea02793c
...@@ -17,22 +17,26 @@ Let's use Module to re-implement :ref:`the example using state ...@@ -17,22 +17,26 @@ Let's use Module to re-implement :ref:`the example using state
<functionstateexample>`. <functionstateexample>`.
>>> m = Module() >>> m = Module()
>>> m.state = Member(T.dscalar()) >>> m.state = T.dscalar()
>>> m.inc = External(T.dscalar()) >>> m.inc = T.dscalar('inc')
>>> m.new_state = m.state + m.inc >>> m.new_state = m.state + m.inc
>>> m.call = Method(m.inc, m.new_state, {m.state: m.new_state}) >>> m.add = Method(m.inc, m.new_state, {m.state: m.new_state})
>>> m.sub = Method(m.inc, None, {m.state: m.state - m.inc})
>>> acc = m.make(state = 0) >>> acc = m.make(state = 0)
>>> acc.state >>> acc.state, acc.inc
array(0.0) array(0.0), None
>>> acc.call(2) >>> acc.add(2)
array(2.0)
>>> acc.state
array(2.0) array(2.0)
>>> acc.state, acc.inc
array(2.0), None
>>> acc.state = 39.99 >>> acc.state = 39.99
>>> acc.call(0.01) >>> acc.add(0.01)
array(40.0) array(40.0)
>>> acc.state >>> acc.state
array(40.0) array(40.0)
>>> acc.sub(20)
>>> acc.state
array(20.0)
This deserves to be broken up a bit... This deserves to be broken up a bit...
...@@ -42,7 +46,7 @@ This deserves to be broken up a bit... ...@@ -42,7 +46,7 @@ This deserves to be broken up a bit...
Here we instantiate an empty Module. Here we instantiate an empty Module.
>>> m.state = Member(T.dscalar()) >>> m.state = T.dscalar()
Then we declare a Variable for use with our Module. That Variable will Then we declare a Variable for use with our Module. That Variable will
be a :ref:`member` of the Module, which means that it will be be a :ref:`member` of the Module, which means that it will be
...@@ -56,91 +60,90 @@ defined in our Module. ...@@ -56,91 +60,90 @@ defined in our Module.
be given the name 'state' automatically. be given the name 'state' automatically.
>>> m.inc = External(T.dscalar('inc')) >>> m.inc = T.dscalar('inc')
The ``inc`` variable doesn't need to be declared as a Member because it The ``inc`` variable doesn't need to be declared as a member of ``m`` because
will only serve as an input to the method we will define. This is why we only use it as a Method input.
it is defined as an :ref:`external` variable. Do note that it is
inconsequential if you do declare it as a Member - it is unlikely
to cause you any problems.
.. note:: .. note::
Both Member and External are optional. If you do the direct assignment: Since we made it a member of ``m``, the ``acc`` object will have an
attribute called ``inc``. This attribute will keep its default value of
>>> m.state = T.dscalar() None throughout the example.
The default is to declare m.state as a Member. This is a sensible
default in most situations and can avoid clutter, so feel free to
omit explicit calls to Member.
>>> m.new_state = m.state + m.inc >>> m.new_state = m.state + m.inc
This line describes how to compute the new state. This line creates a Variable corresponding to some symbolic computation. It
doesn't matter to Theano whether we put it inside ``m`` or not. Feel free to
.. note:: put such results of symbolic computation wherever is most convenient.
Here new_state is implicitly declared as External since it is
illegal to declare a Variable as a Member if it is the variable of
previous computations.
>>> m.call = Method(m.inc, m.new_state, {m.state: m.new_state}) >>> m.add = Method(m.inc, m.new_state, {m.state: m.new_state})
Here we declare a Method. The three arguments are as follow: Here we declare a Method. The three arguments are as follow:
* **inputs**: a list of input Variables * **inputs**: a list of input Variables
* **outputs**: a list of output Variables * **outputs**: a list of output Variables
* **updates**: a dictionary mapping a Variable declared as a Member to a * **updates**: a dictionary mapping member Variables to Variables. When we
Variable representing the computation of the next state of the member. call the function that this Method compiles to, it will replace (update) the
values associated with the member Variables.
If possible, you may also give the updates as keyword arguments, as
in: ``Method(m.inc, m.new_state, state = m.new_state)``. This implies
that m.state's name is 'state'.
>>> acc = m.make(state = 0) >>> acc = m.make(state = 0)
This line is what does the magic. Everything in m is symbolic. Once This line is what does the magic (well, compilation).
the make method is called on it, an object is made which can do real The ``m`` object contains symbolic things such as Variables and Methods.
computation and whose fields contain real values. Calling ``make`` on ``m`` creates an object that can do real
computation and whose attributes contain values such as numbers and numpy
ndarrays.
The keyword arguments given to make are optional and are used to The keyword arguments given to make are optional and are used to
assign initial values to each Member. If a Member is omitted, the assign initial values to each Member. If a Member is omitted, the
initial value is None. initial value is None.
>>> acc.state >>> acc.state, acc.inc
array(0.0) array(0.0), None
Since state was declared as a Member, we can access it easily using Since state was declared as a Member, we can access it easily using
'.state'. '.state'.
.. note::
>>> acc.call(2)
Members can also be accessed using a dictionary-like notation. The syntax
``acc.value[m.state]`` is equivalent to ``acc[m.state]``, and in this case,
``acc.state``. The dictionary-like syntax works for member Variables that
appear inside sub-modules, lists, dictionaries, and for whatever reason have
no name.
>>> acc.add(2)
array(2.0) array(2.0)
>>> acc.state >>> acc.state
array(2.0) array(2.0)
>>> acc.call(6)
array(8.0)
>>> acc.state
array(8.0)
When we call the ``call`` method, all the updates given to the When we call the ``acc.add`` method, all the updates given to the
corresponding Method's ``updates`` field are performed. We only had corresponding Method's ``updates`` field are performed. We only had
one update which mapped ``state`` to ``new_state`` and you can see one update which mapped ``state`` to ``new_state`` and you can see
that it works as intended, adding the argument to the internal state that it works as intended, adding the argument to the internal state.
every time.
>>> acc.state = 39.99 >>> acc.state = 39.99
The state is also easy to set. When we manually set the value of a member
attribute like this, then subsequent calls to the methods of our module will
use the new value.
>>> acc.call(0.01) >>> acc.call(0.01)
array(40.0) array(40.0)
>>> acc.state >>> acc.state
array(40.0) array(40.0)
>>> acc.sub(20)
>>> acc.state
array(20.0)
Here, note that ``acc.add`` and ``acc.sub`` share access to the same ``state``
value but update it in different ways.
The state is also easy to set.
Using Inheritance Using Inheritance
...@@ -156,12 +159,15 @@ subclass of Module: ...@@ -156,12 +159,15 @@ subclass of Module:
def __init__(self): def __init__(self):
super(Accumulator, self).__init__() # don't forget this super(Accumulator, self).__init__() # don't forget this
self.inc = External(T.dscalar()) self.inc = T.dscalar()
self.state = Member(T.dscalar()) self.state = T.dscalar()
self.new_state = self.inc + self.state self.new_state = self.inc + self.state
self.call = Method(inputs = self.inc, self.add = Method(inputs = self.inc,
outputs = self.new_state, outputs = self.new_state,
updates = {self.state: 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() m = Accumulator()
acc = m.make(state = 0) acc = m.make(state = 0)
...@@ -190,12 +196,15 @@ give a method called ``_instance_print_state`` to our Module. ...@@ -190,12 +196,15 @@ give a method called ``_instance_print_state`` to our Module.
def __init__(self): def __init__(self):
super(Accumulator, self).__init__() # don't forget this super(Accumulator, self).__init__() # don't forget this
self.inc = External(T.dscalar()) self.inc = T.dscalar()
self.state = Member(T.dscalar()) self.state = T.dscalar()
self.new_state = self.inc + self.state self.new_state = self.inc + self.state
self.call = Method(inputs = self.inc, self.add = Method(inputs = self.inc,
outputs = self.new_state, outputs = self.new_state,
updates = {self.state: self.new_state}) updates = {self.state: self.new_state})
self.sub = Method(inputs = self.inc,
outputs = None,
updates = {self.state: self.state - self.inc})
def _instance_print_state(self, acc): def _instance_print_state(self, acc):
print '%s is: %s' % (self.state, acc.state) print '%s is: %s' % (self.state, acc.state)
...@@ -239,12 +248,15 @@ initialize a state with a matrix of zeros: ...@@ -239,12 +248,15 @@ initialize a state with a matrix of zeros:
def __init__(self): def __init__(self):
super(MatrixAccumulator, self).__init__() # don't forget this super(MatrixAccumulator, self).__init__() # don't forget this
self.inc = External(T.dscalar()) self.inc = T.dscalar()
self.state = Member(T.dmatrix()) self.state = T.dmatrix()
self.new_state = self.inc + self.state self.new_state = self.inc + self.state
self.call = Method(inputs = self.inc, self.add = Method(inputs = self.inc,
outputs = self.new_state, outputs = self.new_state,
updates = {self.state: self.new_state}) updates = {self.state: self.new_state})
self.sub = Method(inputs = self.inc,
outputs = None,
updates = {self.state: self.state - self.inc})
def _instance_print_state(self, acc): def _instance_print_state(self, acc):
print '%s is: %s' % (self.state, acc.state) print '%s is: %s' % (self.state, acc.state)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论