pytorch-rnn.flatten_parameters作用

最近翻阅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

参考链接:

  • https://stackoverflow.com/questions/53231571/what-does-flatten-parameters-do
  • https://github.com/pytorch/pytorch/issues/2460

你可能感兴趣的:(pytorch)