I debug program each step that the first is encoder layer I don’t got any error as below
input_dim = len(en_vocab)
output_dim = 11
encoder_embedding_dim = 512
decoder_embedding_dim = 512
hidden_dim = 1024
n_layers = 2
encoder_dropout = 0.5
decoder_dropout = 0.5
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
encoder = Encoder(
input_dim,
encoder_embedding_dim,
hidden_dim,
n_layers,
encoder_dropout,
)
hidden, cell = encoder(batch["numericalized_input"])
hidden.shape, cell.shape
Output:
src shape: torch.Size([26, 3])
embedded shape: torch.Size([26, 3, 512])
hidden shape: torch.Size([2, 26, 1024])
(torch.Size([2, 26, 1024]), torch.Size([2, 26, 1024]))
My decoder layer
class Decoder(nn.Module):
def __init__(self, output_dim,hidden_dim, n_layers, dropout):
super().__init__()
self.output_dim = output_dim
self.hidden_dim = hidden_dim
self.n_layers = n_layers
self.rnn = nn.LSTM(output_dim, hidden_dim, n_layers, dropout=dropout,batch_first=True)
self.fc_out = nn.Linear(hidden_dim, output_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, input, hidden,cell):
input = input.unsqueeze(0)
input = input.float()
print('input :',input)
output,(hidden,cell) = self.rnn(input,(hidden,cell))
prediction = self.fc_out(output.squeeze(0))
return prediction,hidden, output
When I run this program
decoder = Decoder(
output_dim,
hidden_dim,
n_layers,
decoder_dropout,
)
prediction, hidden, output = decoder(batch["target"][0], hidden,cell)
prediction.shape, hidden.shape
I’m getting the following error
RuntimeError: For unbatched 2-D input, hx and cx should also be 2-D but got (3-D, 3-D) tensors
Anyone with the solution. Kindly reply. Thanks in advance.