最近翻閱pytorch的代碼,常常發現這樣一句:rnn.flatten_parameters,但卻不知道作用,遂作此文
rnn.flatten_parameters作用
Resets parameter data pointer so that they can use faster code paths
翻譯一下,就是重置參數的數據指針。其實就是contiguous(連續性)的問題,在pytorch issue上有這樣的warning:
UserWarning: RNN module weights are not part of single contiguous chunk of memory. This means they need to be compacted at every call, possibly greately increasing memory usage. To compact weights again call flatten_parameters()
我的理解是,爲了提高內存的利用率和效率,調用flatten_parameters讓parameter的數據存放成contiguous chunk(連續的塊)。類似我們調用tensor.contiguous