前言
Pytorch 中使用DataParallel很简单只需要nn.DataParallel(model)
但是如果在GPU上使用而且模型较大可能会遇到一个warning RNN module weights are not part of single contiguous chunk of memory. This means they need to be compacted at every call, possibly greatly increasing memory usage. To compact weights again call flatten_parameters()
原因
就如同warning 中所说model参数放在gpu上的时候不保证放置的memory位置一定是连续的,所以会增加memory的使用,解决方法添加 flatten_parameters()
使用方法如下
class Model(nn.Module):
def __init__(self, input_size, output_size):
super(Model, self).__init__()
self.rnn = nn.RNN(input_size, output_size)
def forward(self, input):
self.rnn.flatten_parameters()
...
ps: 正则化(regularization) 一定要和 normalization/standardization 一起使用, 因为正则化对小权重有偏好,如果不使用normalization/standardization 就会让小weight和最后的大输出之间有一个gap.