2016-03-05 37 views
0

我一直在阅读Theano文档的扫描,发现自己被两个看似矛盾的陈述所困惑。什么时候使用theano的扫描功能是有利的

http://deeplearning.net/software/theano/tutorial/loop.html#scan,扫描的优势之一被列为:

Slightly faster than using a for loop in Python with a compiled Theano function. 

但是,在http://deeplearning.net/software/theano/library/scan.html#lib-scan,在部分优化使用扫描的,它说:

Scan makes it possible to define simple and compact graphs that can do 
the same work as much larger and more complicated graphs. However, it 
comes with a significant overhead. As such, **when performance is the 
objective, a good rule of thumb is to perform as much of the computation 
as possible outside of Scan**. This may have the effect of increasing 
memory usage but can also reduce the overhead introduces by using Scan. 

我在这里,“表演”的阅读是速度的代名词。所以,一旦编译完成后,我会感到困惑,因为扫描是否会导致运行时间缩短。

回答

1

如果你的表达内在需要一个for循环,那么你有时有两种选择:

  1. 构建使用Python的for循环
  2. 表达式中使用扫描

选项构建表达1只有在您事先知道for循环的长度时才有效。可能发生的问题是,for循环的长度取决于脚本写入时不可用的符号变量。在这种情况下,您需要使用扫描。虽然通常情况下你可以用任何一种方式来制定问题(参见张量流中没有扫描)。

至于时间表现,有很多结果表明它真的取决于哪个问题更快。

+0

对于在脚本写入时其值不可用的符号变量,是否有某种方法可以用运行时计算该变量值的theano函数替换该变量? – user1245262