class LSTMModel(nn.Module):
def __init__(self, lstm_size=50, linear_size=50, vocab_size=20000):
super(LSTMModel, self).__init__()
self.linear_size = linear_size
self.lstm_size = lstm_size
self.embeds = nn.Embedding(vocab_size, 128)
self.lstm = nn.LSTM(128, lstm_size)
self.dropout = nn.Dropout(.1)
self.fc1 = nn.Linear(lstm_size, linear_size) # * 2 for bidirection
self.fc2 = nn.Linear(linear_size, 6)
def forward(self, x):
h0 = Variable(torch.zeros(1, x.size(1), self.lstm_size)) # 2 for bidirection
c0 = Variable(torch.zeros(1, x.size(1), self.lstm_size))
out = self.embeds(x)
out = out.view(len(x), x.size(1), -1)
out, _ = self.lstm(out, (h0, c0))
out = self.dropout(out[-1])
out = self.fc1(out)
out = torch.nn.functional.relu(out)
out = self.dropout(out)
out = self.fc2(out)
out = torch.nn.functional.sigmoid(out)
return out
If I forward pass with a batch size bigger than 1 I get this error:
RuntimeError Traceback (most recent call last)
<ipython-input-219-ce43ac67861e> in <module>()
1 t = dataset[:10][0]
----> 2 model(t.t())
~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
323 for hook in self._forward_pre_hooks.values():
324 hook(self, input)
--> 325 result = self.forward(*input, **kwargs)
326 for hook in self._forward_hooks.values():
327 hook_result = hook(self, input, result)
<ipython-input-217-478ca91c0a21> in forward(self, x)
20 h0 = Variable(torch.zeros(1, x.size(1), self.lstm_size)) # 2 for bidirection
21 c0 = Variable(torch.zeros(1, x.size(1), self.lstm_size))
---> 22 out = self.embeds(x)
23 out = out.view(len(x), x.size(1), -1)
~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
323 for hook in self._forward_pre_hooks.values():
324 hook(self, input)
--> 325 result = self.forward(*input, **kwargs)
326 for hook in self._forward_hooks.values():
327 hook_result = hook(self, input, result)
~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/sparse.py in forward(self, input)
101 input, self.weight,
102 padding_idx, self.max_norm, self.norm_type,
--> 103 self.scale_grad_by_freq, self.sparse
104 )
105
~/anaconda3/lib/python3.6/site-packages/torch/nn/_functions/thnn/sparse.py in forward(cls, ctx, indices, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
57 output = torch.index_select(weight, 0, indices)
58 else:
---> 59 output = torch.index_select(weight, 0, indices.view(-1))
60 output = output.view(indices.size(0), indices.size(1), weight.size(1))
61
RuntimeError: index out of range at /Users/soumith/minicondabuild3/conda-bld/pytorch_1512381214802/work/torch/lib/TH/generic/THTensorMath.c:277