Reputation: 1780
I'm struggling with understanding what sort of dimensions my pytorch model needs as input. My model setup is:
import torch
from torch import nn, tensor
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(RNN, self).__init__()
self.rnn_b = nn.RNN(input_size=input_size,
hidden_size=hidden_size,
num_layers=1,
bias=False)
self.hidden_size = hidden_size
self.linearout = nn.Linear(hidden_size, output_size)
def forward(self, input, hidden):
out, hidden = self.rnn_b(input, hidden)
output = self.linearout(out)
return output, hidden
def initHidden(self):
return torch.zeros(1, self.hidden_size)
model = RNN(1, 2, 1)
If I call model(tensor(25.), tensor([[0., 0.]]))
then I get the error:
IndexError: dimension specified as 1 but tensor has no dimensions
If I call model(tensor([25.]), tensor([[0., 0.]]))
then I get the error
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
Can someone please explain what's going on here? What is the correct way to format the data for the input to my model?
Upvotes: 2
Views: 553
Reputation: 1054
You need to encapsulate more. This is because pytorch automatically allows batches:
model(tensor([[[25.]]]), tensor([[[0., 0.]]]))
Output:
(tensor([[[-0.7704]]], grad_fn=<AddBackward0>),
tensor([[[1., 1.]]], grad_fn=<StackBackward>))
You can use multiple input, as a batch:
model(tensor([[[25.]], [[25.]]]), tensor([[[0., 0.]]]))
Output:
(tensor([[[0.0341]],
[[0.0341]]], grad_fn=<AddBackward0>),
tensor([[[ 0.9999, -1.0000]]], grad_fn=<StackBackward>))
Upvotes: 1