Pata_Naheen:

hare me complete code what are you runn


import torch
import torch.nn as nn


class RNN(nn.Module):
    def __init__(self):
        super(RNN, self).__init__()

        self.rnn = nn.LSTM(
            input_size=6,
            hidden_size=6,
            num_layers=2,
            batch_first=True,
        )
        self.fc = nn.Linear(6, 2)

    def forward(self, x):
        out, (h_n, h_c) = self.rnn(x, None)
        return out[:, -1, :]    # Return output at last time-step


X = torch.randn(10, 1, 6)
y = torch.randn(10, 1, 2)

rnn = RNN()
optimizer = torch.optim.Adam(rnn.parameters(), lr=0.001)
loss_func = nn.CrossEntropyLoss()


for j in range(2):
    for i, item in enumerate(X):
        item = item.unsqueeze(0)
        output = rnn(item)
        target = y[i]
        # print(target)
        #target = target.squeeze_()
        #print("input shape:",output.shape, "output shape:", target.shape)
        loss = loss_func(output, target.argmax(dim=1))
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

Hope it helps. But still, you code doesn’t make much sense to me. First, you input shape is (1000, 1, 6), in which you are saying that your whole batch size is 1000, sequence length is 1, and feature dimension is 6. If you are using RNN, why do you have sequence length 1? Second, You label’s shape is (batch_size, sequence_len, num_of_classes). This is weird. I think you want one label for each sequence, then why is sequence_len involved? Should it be (batch_size, num_of_classes)? Third, in your RNN network, you defined fc  linear layer in init(), but you forgot to call it in forward()