I’m trying to use Seq2Seq models for sentiment classification. I can execute an implementation using torchtext with FastText model on IMDB dataset. It works as expected and I can obtain predictions. However, when I try to replace FastText model with a BiRNN model , I’m getting an error about input sizes as below. Can anyone explain how to correct the error about input sizes? If you can provide a suitable implementation of a Seq2Seq model that I can adapt, it will be ok for me also. I provide the implementation of these two models as below. Thanks.
raise ValueError(“Target size ({}) must be the same as input size ({})”.format(target.size(), input.size()))
ValueError: Target size (torch.Size([64])) must be the same as input size (torch.Size([1631]))
class FastText(nn.Module):
def init(self,
vocab_size,
embedding_dim,
output_dim,
pad_idx):
super().init()
self.embedding = nn.Embedding(
vocab_size,
embedding_dim,
padding_idx=pad_idx)
self.fc = nn.Linear(embedding_dim,
output_dim)
def forward(self, text):
embedded = self.embedding(text)
embedded = embedded.permute(1, 0, 2)
pooled = F.avg_pool2d(
embedded,
(embedded.shape[1], 1)).squeeze(1)
return self.fc(pooled)
class BiRNN(nn.Module):
def init(self, vocab, embed_size, num_hiddens, num_layers):
super(BiRNN, self).init()
self.embedding = nn.Embedding(len(vocab), embed_size)
self.encoder = nn.LSTM(input_size=embed_size,
hidden_size=num_hiddens,
num_layers=num_layers,
bidirectional=True)
self.decoder = nn.Linear(4 * num_hiddens, 1)
def forward(self, inputs):
embeddings = self.embedding(inputs.permute(1, 0))
outputs, _ = self.encoder(embeddings)
encoding = torch.cat((outputs[0], outputs[-1]), -1)
outs = self.decoder(encoding) # (batch_size, 2)
return outs