'Why won't PyTorch RNN accept unbatched input?
I'm trying to train a PyTorch RNN to predict the next value in a 1D sequence. According to the PyTorch documentation page, I think I should be able to feed unbatched input to the RNN with shape [L,H_in] where L is the length of the sequence and H_in is the input length. That is, a 2D vector.
https://pytorch.org/docs/stable/generated/torch.nn.RNN.html
import torch
x1 = torch.tensor([[1.0], [2.0], [3.0], [4.0], [5.0], [6.0], [7.0], [8.0]])
x1_input = x1[0:-1, :]
x1_target = x1[1:, :]
rnn = torch.nn.RNN(1, 1, 1)
optimizer_prediction = torch.optim.Adam(rnn.parameters())
prediction_loss = torch.nn.L1Loss()
rnn.train()
epochs = 100
for i in range(0, epochs):
output_x1 = rnn(x1_input)
print(output_x1)
error_x1 = prediction_loss(output_x1, x1_target)
error_x1.backward()
optimizer_prediction.step()
optimizer_prediction.zero_grad()
However, PyTorch is complaining that it expects a 3D input vector (i.e. including a dimension for the batch):
RuntimeError: input must have 3 dimensions, got 2
What is the correct method for feeding unbatched input to an RNN?
Solution 1:[1]
I would recommend turning your input into a 3d array by adding a batch size of one with:
torch.unsqueeze(x1_input, dim=0).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Andronicus |