两点说明:
- theano的语法习惯是先定义类型,最后赋值的做法
- theano中scan函数是用来做循环使用的。
? ? ? 最简单的其形式如下: theano.scan(fn? ,squences? ,outputs_info? ,non_sequences)
? ? ? 其中,fn是循环函数,squences? ,outputs_info? ,non_sequences都是fn的参数,简明解释如下:
fn:循环函数
squence:以时序方式给出的参数,形如[s1,s2,...st-1,st]
outputs_info:当t=0时表示输出的初始化值,在时间步t时相当于fn的t-1时刻的output值
non_sequences:相当于是静态变量,在循环过程中不变的值
? ? ? fn通常以lambda表达式方式给出,fn参数列表中参数给出的顺序和scan接收参数顺序有关,因此要将其顺序对应起来。
scan中接收参数的顺序在代码中有说明,这里直接放出scan函数中的说明:
The order of the sequences is the same as the one in the list
`sequences` given to scan. The order of the outputs is the same
as the order of ``outputs_info``. For any sequence or output the
order of the time slices is the same as the one in which they have
been given as taps. For example if one writes the following :
.. code-block:: python
scan(fn, sequences = [ dict(input= Sequence1, taps = [-3,2,-1])
, Sequence2
, dict(input = Sequence3, taps = 3) ]
, outputs_info = [ dict(initial = Output1, taps = [-3,-5])
, dict(initial = Output2, taps = None)
, Output3 ]
, non_sequences = [ Argument1, Argument2])
``fn`` should expect the following arguments in this given order:
#. ``Sequence1[t-3]``
#. ``Sequence1[t+2]``
#. ``Sequence1[t-1]``
#. ``Sequence2[t]``
#. ``Sequence3[t+3]``
#. ``Output1[t-3]``
#. ``Output1[t-5]``
#. ``Output3[t-1]``
#. ``Argument1``
#. ``Argument2``
由此可见theano的scan很适合实现RNN结构:当前的输出和当前的输入(sequences,non_sequences)和上一时间步的输出(outputs_info)有关
给出一个例子说明这件事情(交换了fn中参数顺序导致计算图的不同):
?
?
|