Connect your moderator Slack workspace to receive post notifications:
Sign in with Slack

LSTM problem

Hello,
I am using lstm to run a neural network but i have problems with tensor dimensions and run time, i don't get the pb
LSTM works with 3D tensors so i reshaped my train data.
We initially have a matrix (150'000, 20) that i reshape into torch.Size([1, 150000, 20]).
Ytrain is a tensor (150000) (i test also (1,150000) and (150000,1))
I get a warning during the run : UserWarning: Using a target size (torch.Size([1, 150000])) that is different to the input size (torch.Size([1, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
return F.mse_loss(input, target, reduction=self.reduction)
That's why i guess it's linked to dim
Here is the function to train the model:

#########definition of the lstm

class LSTM(nn.Module):

def __init__(self, num_classes, input_size, hidden_size, num_layers):
    super(LSTM, self).__init__()

    self.num_classes = num_classes
    self.num_layers = num_layers
    self.input_size = input_size
    self.hidden_size = hidden_size
    self.seq_length = 20
    #self.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size,
                        #num_layers=num_layers, 150000, 1, batch_first=True)
    self.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size,
                        num_layers=num_layers, batch_first=True)

    self.fc = nn.Linear(hidden_size, num_classes)

def forward(self, x):
    h_0 = Variable(torch.zeros(
        self.num_layers, x.size(0), self.hidden_size))

    c_0 = Variable(torch.zeros(
        self.num_layers, x.size(0), self.hidden_size))

    # Propagate input through LSTM
    ula, (h_out, _) = self.lstm(x, (h_0, c_0))

    h_out = h_out.view(-1, self.hidden_size)

    out = self.fc(h_out)

    return out
#
TRAINING:

num_epochs = 5 ###add a zero
learning_rate = 0.01

input_size = 20
hidden_size = 2
num_layers = 1
seq_length=1

num_classes = 1

lstm = LSTM(num_classes, input_size, hidden_size, num_layers)

lstm = LSTM(num_classes, input_size, hidden_size, num_layers)

criterion = torch.nn.MSELoss() # mean-squared error for regression
optimizer = torch.optim.Adam(lstm.parameters(), lr=learning_rate)

optimizer = torch.optim.SGD(lstm.parameters(), lr=learning_rate)

Train the model

for epoch in range(num_epochs):
outputs = lstm(trainX)
optimizer.zero_grad()

# obtain the loss function
loss = criterion(outputs, trainY)

loss.backward()

optimizer.step()
if epoch % 100 == 0:
    print("Epoch: %d, loss: %1.5f" % (epoch, loss.item()))
#
Page 1 of 1

Add comment

Post as Anonymous Dont send out notification