Greeting,
I was working on converting my model to bidirectional (both the ones using LSTM and GRU), I thought the way to do that is simply make the bidirectional parameter True but unfortunately it did not work and it raise this error
RuntimeError: Expected hidden size (2, 10, 100), got (1, 10, 100)
Then I tried to follow the model below
class BiRNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
super(BiRNN, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, bidirectional=True)
self.fc = nn.Linear(hidden_size*2, num_classes) # 2 for bidirection
def forward(self, x):
# Set initial states
h0 = torch.zeros(self.num_layers*2, x.size(0), self.hidden_size).to(device) # 2 for bidirection
c0 = torch.zeros(self.num_layers*2, x.size(0), self.hidden_size).to(device)
# Forward propagate LSTM
out, _ = self.lstm(x, (h0, c0)) # out: tensor of shape (batch_size, seq_length, hidden_size*2)
# Decode the hidden state of the last time step
out = self.fc(out[:, -1, :])
return out
model = BiRNN(input_size, hidden_size, num_layers, num_classes).to(device)
However it did not work as well,
it raises this error
RuntimeError: Expected hidden[0] size (2, 39, 50), got (2, 10, 50)Preformatted text
if I set the batch)first to False then this is the error
ValueError: Target and input must have the same number of elements. target nelement (20) != input nelement (78)
Thanks in advance