I’m trying to train a character level RNN with a difference that each token(character) gets classified into one of m classes (multi class classification). My Input is of the shape (batch_size, seq_len, num_classes) because I’m using OHEncoding and output is of shape (batch_size, seq_len,num_classes) with softmax applied to dim 2. Here’s my model
class CharRNN(nn.Module): def __init__(self, seq_len, input_size, hidden_size, output_size): super(CharRNN, self).__init__() self.rnn = nn.RNN(input_size, hidden_size, batch_first=True) self.op = nn.Linear(hidden_size, output_size) self.softmax = nn.LogSoftmax(dim=2) def forward(self, x): x, h = self.rnn(x) return self.softmax(self.op(x))
but when I use
NLLLoss() as the criterion and
batch_size=32, seq_len=101, num_classes=32, I get the following error
ValueError: Expected target size (32, 32), got torch.Size([32, 101, 32])
Please help if my understanding is correct or am I making a logical mistake somewhere. Thanks.