I have this RNN class:
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size, n_layers):
super(RNN, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.n_layers = n_layers
self.encoder = nn.Embedding(input_size, hidden_size)
self.GRU = nn.GRU(hidden_size, hidden_size, n_layers)
self.decoder = nn.Linear(hidden_size, output_size)
def forward(self, input, hidden, batch_size):
input = self.encoder(input.view(batch_size, seq_len))
input = input.permute(1, 0, 2)
output, hidden = self.GRU(input, hidden)
output = self.decoder(output.view(batch_size, hidden_size))
return output, hidden
def init_hidden(self, batch_size):
return Variable(torch.randn(self.n_layers, batch_size, self.hidden_size).cuda())
Note that both forward and init_hidden functions receive batch_size argument.
Then I create my model using DataParallel to run it on multiple GPUs:
net = torch.nn.DataParallel(RNN(n_chars, hidden_size, n_chars, n_layers)).cuda()
Then I start training the model:
for inp, target in training_set:
hidden = net.init_hidden(batch_size)
...
However I get this error:
Traceback (most recent call last):
File "char-rnn.py", line 178, in <module>
hidden = net.init_hidden(batch_size)
File "/root/miniconda2/lib/python2.7/site-packages/torch/nn/modules/module.py", line 237, in __getattr__
return object.__getattr__(self, name)
AttributeError: type object 'object' has no attribute '__getattr__'
What is the correct way to wrap my RNN class into DataParallel, so that it can use init_hidden function?
p.s. here’s full code