2016-03-05 70 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