I want to build a model like
Model( (rnn): LSTM(25088, 5, num_layers=2, batch_first=True) (output_layer): Linear(in_features=5, out_features=2, bias=True) )
and I know when train a RNN-like model, we can use functions like
torch.nn.utils.rnn.pad_sequence() torch.nn.utils.rnn.pack_padded_sequence() torch.nn.utils.rnn.pad_packed_sequence()
My question is about the forward() in class Model()
class Model(nn.Module) def __init__(self): super(Model, self).__init__() self.rnn = nn.LSTM(25088, 5, 2, batch_first=True) self.output_layer = nn.Linear(5, 2) def forward(self, inputs, hidden_0=None) output, (hidden, cell) = self.rnn(inputs, hidden_0) output = self.output_layer(output) # AttributeError: 'PackedSequence' object has no attribute 'dim'
I know I can get packed sequence by pack_padded_sequence(), and also can feed the part of model, self.rnn. But when feed the part of model, self.output_layer, it will error as above. The point is that, there is batch-size info in the output of self.rnn(inputs, hidden_0), but self.output_layer(output), which is a nn.Linear() has no info about batch. It just has parameters ( in_features , out_features , bias=True), which meas there is mistake that batch-size info and no-batch-size info. How can train the model properly ?
Thank you for your help!