Simple LSTM Cell like below… I declare my cell state thus…
self.c_t = Variable(torch.zeros(batch_size, cell_size), requires_grad=False).double()
I really don’t like having to do the .double().cuda() on my hidden Variable. But if I dont, the model breaks…
Is there a way to fix this… I tried doing Parameters, but the LSTMCell returns a Variable, so I got a type error.
What the “correct” way to setup hidden variables for LSTMCell?
import torch
from torch import nn
from torch.nn import Parameter
from torch.autograd import Variable
class SimpleLSTM(nn.Module):
def __init__(self, batch_size, input_dims, sequence_length, cell_size, output_features=1):
super(SimpleLSTM, self).__init__()
self.input_dims = input_dims
self.sequence_length = sequence_length
self.cell_size = cell_size
self.lstm = nn.LSTMCell(input_dims, cell_size)
self.to_output = nn.Linear(cell_size, output_features)
self.h_t = Variable(torch.zeros(batch_size, cell_size), requires_grad=False).double()
self.c_t = Variable(torch.zeros(batch_size, cell_size), requires_grad=False).double()
def forward(self, input):
self.h_t.zero_()
self.c_t.zero_()
outputs = []
for input_t in torch.chunk(input, self.sequence_length, dim=2):
self.h_t, self.c_t = self.lstm(input_t.squeeze(2), (self.h_t, self.c_t))
outputs.append(self.to_output(self.h_t))
return torch.cat(outputs, dim=1)