Hey @scarecrow21,

The target size should be different from the input size. Because a sequence of 10 inputs give out 1 output. So, target size = (torch.Size([5, 1, 1])) should be different from input size = (torch.Size([5, 10, 1])).

I think the “input” the error you are reporting is referring to is the input of the F.mse_loss function, which in this case is the output of your RNN, not the input you are giving to the network.
There is probably a mismatch between the size of the output of your RNN and of the target you are giving to the loss function.
Following the docs, the output of a RNN will consist of two tensors [output, h_n], the first with shape (seq_len, batch, num_directions * hidden_size), the second with shape (num_layers * num_directions, batch, hidden_size).
So, it is not true that a sequence of 10 inputs gives out 1 output: your output will contain the output features (h_t) from the last layer of the RNN, for each t. It would be something like [seq_len=10, batch_size=5, hidden_size=1], using the parameters of your configuration.
Now, take a look at the forward function of your ModelRNN:
def forward(self, x, hidden):
        hidden = torch.zeros(num_layer, batch_size, hidden_size)

        x = x.view(batch_size, sequence_length, input_size)
        out, hidden = self.rnn(x, hidden)
        
        return hidden, out

Since you “swap” the hidden and out tensors of position before returning, according to your configuration the tensors hidden, output = model(x_batch, hidden) in the for cycle of the training, would have as shapes [num_layers*num_directions = 3, batch_size=5, hidden_size=1] and [batch_size=5, seq_len=10, num_directions*hidden_size=1] (since you instantiated your RNN with batch_first=True).
When you compute the loss, you are doing it therefore between two tensors one of shape [5, 10, 1], and one of shape [5, 1, 1].
In poor words, you are computing the loss between the whole sequence of output for each t and the last element of the sequence of the target.
If you are interested only in the last element of the sequence for each batch, I would suggest you to modify the forward function in order to return only the last element of the sequence, or to manually modify the tensor before you compute the loss.
Hope everything is clear!
Cheers