Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
fa7e8344
提交
fa7e8344
authored
3月 23, 2009
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
changes to basic_tutorial/module.txt: removed Member, External; add second method to example
上级
ea02793c
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
73 行增加
和
61 行删除
+73
-61
module.txt
doc/basic_tutorial/module.txt
+73
-61
没有找到文件。
doc/basic_tutorial/module.txt
浏览文件 @
fa7e8344
...
...
@@ -17,22 +17,26 @@ Let's use Module to re-implement :ref:`the example using state
<functionstateexample>`.
>>> m = Module()
>>> m.state =
Member(T.dscalar()
)
>>> m.inc =
External(T.dscalar()
)
>>> m.state =
T.dscalar(
)
>>> m.inc =
T.dscalar('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.state
array(0.0)
>>> acc.call(2)
array(2.0)
>>> acc.state
>>> acc.state, acc.inc
array(0.0), None
>>> acc.add(2)
array(2.0)
>>> acc.state, acc.inc
array(2.0), None
>>> acc.state = 39.99
>>> acc.
call
(0.01)
>>> acc.
add
(0.01)
array(40.0)
>>> acc.state
array(40.0)
>>> acc.sub(20)
>>> acc.state
array(20.0)
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.
>>> m.state =
Member(T.dscalar()
)
>>> m.state =
T.dscalar(
)
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
...
...
@@ -56,91 +60,90 @@ defined in our Module.
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
will only serve as an input to the method we will define. This is why
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.
The ``inc`` variable doesn't need to be declared as a member of ``m`` because
we only use it as a Method input.
.. note::
Both Member and External are optional. If you do the direct assignment:
>>> m.state = T.dscalar()
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.
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
None throughout the example.
>>> m.new_state = m.state + m.inc
This line describes how to compute the new state.
.. note::
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.
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
put such results of symbolic computation wherever is most convenient.
>>> 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:
* **inputs**: a list of input Variables
* **outputs**: a list of output Variables
* **updates**: a dictionary mapping a Variable declared as a Member to a
Variable representing the computation of the next state of the member.
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'.
* **updates**: a dictionary mapping member Variables to Variables. When we
call the function that this Method compiles to, it will replace (update) the
values associated with the member Variables.
>>> acc = m.make(state = 0)
This line is what does the magic. Everything in m is symbolic. Once
the make method is called on it, an object is made which can do real
computation and whose fields contain real values.
This line is what does the magic (well, compilation).
The ``m`` object contains symbolic things such as Variables and Methods.
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
assign initial values to each Member. If a Member is omitted, the
initial value is None.
>>> acc.state
array(0.0)
>>> acc.state
, acc.inc
array(0.0)
, None
Since state was declared as a Member, we can access it easily using
'.state'.
.. note::
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.
call
(2)
>>> acc.
add
(2)
array(2.0)
>>> acc.state
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
one update which mapped ``state`` to ``new_state`` and you can see
that it works as intended, adding the argument to the internal state
every time.
that it works as intended, adding the argument to the internal state.
>>> 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)
array(40.0)
>>> acc.state
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
...
...
@@ -156,12 +159,15 @@ subclass of Module:
def __init__(self):
super(Accumulator, self).__init__() # don't forget this
self.inc =
External(T.dscalar()
)
self.state =
Member(T.dscalar()
)
self.inc =
T.dscalar(
)
self.state =
T.dscalar(
)
self.new_state = self.inc + self.state
self.
call
= Method(inputs = self.inc,
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)
...
...
@@ -190,12 +196,15 @@ give a method called ``_instance_print_state`` to our Module.
def __init__(self):
super(Accumulator, self).__init__() # don't forget this
self.inc =
External(T.dscalar()
)
self.state =
Member(T.dscalar()
)
self.inc =
T.dscalar(
)
self.state =
T.dscalar(
)
self.new_state = self.inc + self.state
self.
call
= Method(inputs = self.inc,
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})
def _instance_print_state(self, acc):
print '%s is: %s' % (self.state, acc.state)
...
...
@@ -239,12 +248,15 @@ initialize a state with a matrix of zeros:
def __init__(self):
super(MatrixAccumulator, self).__init__() # don't forget this
self.inc =
External(T.dscalar()
)
self.state =
Member(T.dmatrix()
)
self.inc =
T.dscalar(
)
self.state =
T.dmatrix(
)
self.new_state = self.inc + self.state
self.
call
= Method(inputs = self.inc,
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})
def _instance_print_state(self, acc):
print '%s is: %s' % (self.state, acc.state)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论