什么时候使用theano的scan函数比较有利

When is it advantageous to use theano's scan function

我一直在阅读有关 scan 的 Theano 文档,发现自己对两个看似矛盾的陈述感到困惑。

http://deeplearning.net/software/theano/tutorial/loop.html#scan上,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.

我对 'performance' 的阅读,在这里,是速度的同义词。因此,我对 when/if 编译完成后扫描会导致更短的运行时间感到困惑。

如果您的表达式本质上需要一个 for 循环,那么有时您有两个选择:

  1. 使用 python for 循环构建表达式
  2. 使用扫描构建表达式

选项 1 仅在您事先知道 for 循环的长度时才有效。您的 for 循环的长度可能取决于在编写脚本时不可用的符号变量。在这种情况下,您需要使用扫描。尽管通常您可以用任何一种方式来表述问题(请参阅 tensorflow 中缺少扫描)。

至于时间性能,有许多结果表明,这实际上取决于哪个更快的问题。