我一直在阅读有关扫描的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.
我在这里读到的“表现”是速度的同义词。因此,一旦编译完成,我对于何时/如果扫描将导致更短的运行时间感到困惑。
答案 0 :(得分:1)
如果你的表达本身需要一个for循环,那么你有时会有两个选择:
选项1仅在您事先知道for循环的长度时才有效。可能会发生for循环的长度取决于在脚本编写时不可用的符号变量。在这种情况下,您需要使用扫描。虽然你经常可以用任何一种方式表达问题(参见张量流中没有扫描)。
至于时间表现,有很多结果表明它确实取决于问题哪一个更快。