LSTM 'tuple' object has no attribute 'size' error


#1

I’ve been trying to get an LSTM working, and have been looking off the docs and a couple examples but I keep getting a ‘tuple’ object has no attribute ‘size’ error when I call the model at

output, hidden = model((inp[s,:,:], (initial_hidden, initial_cell)))

I can’t figure out whats causing the error, here’s what I have so far:

model = torch.nn.Sequential(
    nn.LSTM(input_size = input_size, hidden_size = hidden_size, num_layers = num_layers, dropout = dropout),
    nn.Linear(hidden_size, input_size)
)   

initial_hidden = Variable(torch.zeros(num_layers, 1, hidden_size))
initial_cell = Variable(torch.zeros(num_layers, 1, hidden_size))

loss_function = nn.CrossEntropyLoss()

model_optimizer = torch.optim.Adam(model.parameters(), lr = learning_rate)
    
for i in range(int(np.shape(data)[0] / seq_length)):
     model.zero_grad()
     loss = 0
   
     inp = data[int(i * seq_length) : int((i * seq_length) + seq_length - 2), :, :]

     target = data[int((i * seq_length) + 1) : int((i * seq_length) + seq_length - 1), :, :]
        
     for s in range(seq_length):
          output, hidden = model((inp[s,:,:], (initial_hidden, initial_cell)))
        
          loss += loss_function(output, target[s,:,:])
        
        loss.backward()
        model_optimizer.step()

Thank you!


#2

The problem is, that nn.Sequential is a container for simple models.
The nn.LSTM layer however, outputs the hidden and cell state. Also it takes input, (h_0, c_0) for the input.
I’m not sure, if that’s currently somehow manageable using Sequential, so I would suggest to create an own model. Have a look at this tutorial to see some example implementations.