Hi
So I’m trying a seq-2-seq encoder decoder model based on this comment.
class Encoder(nn.Module):
def __init__(self, input_size, hidden_dim, num_layers=1):
super(Encoder, self).__init__()
self.input_size = input_size
self.hidden_dim = hidden_dim
self.num_layers = num_layers
# self.lstm = nn.LSTM(self.input_size, self.hidden_dim, num_layers=self.num_layers, batch_first=True)
self.lstm = nn.LSTM(input_size=input_size,
hidden_size=self.hidden_dim,
num_layers=self.num_layers,
batch_first=True) # Note that "batch_first" is set to "True"
self.hidden = None
def init_hidden(self, batch_size):
return (torch.zeros(self.num_layers, batch_size, self.hidden_dim),
torch.zeros(self.num_layers, batch_size, self.hidden_dim))
def forward(self, inputs):
# Push through RNN layer (the ouput is irrelevant)
_, self.hidden = self.lstm(inputs, self.hidden)
return self.hidden
But as my time series might not all be be the same length, I’m pack-padding my batches in the train function.
batch_size = 64
layer_size = 256
input_dim = 34
def train(train_loader):
encoder.train()
decoder.train()
total_loss = 0
correct = 0
encoder_optimizer.zero_grad()
decoder_optimizer.zero_grad()
for data_list in train_loader:
# datalist:
# 0 = encoder input
# 1 = data shape
# 2 = label
# 3 = decoder input
# Prepare data
encoder_input = pack_padded_sequence(data_list[0], data_list[1], batch_first=True, enforce_sorted=False).float()
y = data_list[2]
decoder_input = data_list[3]
current_batch_size = data_list[2].shape[0]
# Reset hidden state of encoder for current batch
encoder.hidden = encoder.init_hidden(34)
# Do forward pass through encoder
hidden_state = encoder(encoder_input)
output = decoder(decoder_input.float(), outputs.float(), hidden_state)
loss = criterion(y, outputs.float())
loss.backward()
total_loss += loss.item()
#Accuracy
output = (output).float()
correct += torch.eq(output, y).float().sum()/(current_batch_size)
# Run Optimizer
encoder_optimizer.step()
decoder_optimizer.step()
return total_loss / len(train_loader), correct / len(train_loader)
I’m using a batch first variation and the code does run with a batch size of 1 or 2. Now when i started testing this with a batch size of 64, i got the following error:
Exception has occurred: IndexError
index out of range in self
File "2_Classifier.py", line 59, in forward
_, self.hidden = self.lstm(inputs, self.hidden)
File "2_Classifier.py", line 118, in train
hidden_state = encoder(encoder_input)
File 2_Classifier.py", line 197, in <module>
loss, train_acc = train(train_loader)
# data shapes
data_list[0].shape
torch.Size([64, 26, 34])
data_list[1].shape
torch.Size([64])
data_list[2].shape
torch.Size([64, 2])
data_list[3].shape
torch.Size([64])
Now i have no idea, where the problem might be. It’s probably some sort of dimension error, but i can’t tell what i should be changing.