theano中scan简明理解

本文介绍了theano中scan函数的工作原理,强调了其在循环计算中的重要性。首先,阐述了theano遵循先定义类型再赋值的语法习惯。接着,详细解释了scan函数的参数:fn作为循环执行的函数,sequences是按时间步传递的参数,outputs_info用于保存每个时间步的输出状态,non_sequences则是循环中不变的静态变量。通过举例,展示了fn参数顺序对计算图的影响,进一步说明了scan函数在实现递归神经网络(RNN)结构上的适用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两点说明:

  • 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中参数顺序导致计算图的不同):

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值