'PackedSequence' object has no attribute 'size' error with LSTMCell

amitkayal: <p>Hello All,</p>
<p>I am trying to use LSTMCell to form multiple LSTM layer and not using LSTM. Getting following error and looks like it is due due to nn.utils.rnn.pack_padded_sequence but not able to debug further. Could you please help me on this?</p>
<p><strong>My network definition is:</strong></p>
<blockquote>
<p>class RNN(nn.Module):</p>
<pre><code>def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim, n_layers, 

             bidirectional, dropout, pad_idx):

    

    super().__init__()

    

    self.n_layers = n_layers

    self.embedding_dim = embedding_dim

    self.hidden_dim = hidden_dim

      # Number of time steps

    self.sequence_len = 3

    self.embedding = nn.Embedding(vocab_size, embedding_dim, padding_idx = pad_idx)

    # Initialize LSTM Cell for the first layer

    self.lstm_cell_layer_1 = nn.LSTMCell(self.embedding_dim, self.hidden_dim)

    

    # Initialize LSTM Cell for the second layer

    self.lstm_cell_layer_2 = nn.LSTMCell(self.hidden_dim, self.hidden_dim)

    # Initialize LSTM Cell for the second layer

    self.lstm_cell_layer_3 = nn.LSTMCell(self.hidden_dim, self.hidden_dim)

    ## we would need to initialize the hidden and cell state for each LSTM layer.

                    

    self.fc = nn.Linear(hidden_dim*2, output_dim)

    

    self.dropout = nn.Dropout(dropout)

    

def forward(self, text, text_lengths):

    

    #text = [sent len, batch size]

    

    embedded = self.dropout(self.embedding(text))

    

    #embedded = [sent len, batch size, emb dim]

    ## Initialisation

    

    #pack sequence

    packed_embedded = nn.utils.rnn.pack_padded_sequence(embedded, text_lengths)

    # packed_embedded = embedded

    print(f'text_lengths{text_lengths}')

    print(f'text.size(0){text.size(0)}')

    # out = packed_embedded.view(self.sequence_len, text.size(0), -1)

    # Creation of cell state and hidden state for layer 1

    hidden_state = torch.zeros(self.embedding_dim, self.hidden_dim)

    cell_state = torch.zeros(self.embedding_dim, self.hidden_dim)

    # Creation of cell state and hidden state for layer 2

    hidden_state_2 = torch.zeros(self.embedding_dim, self.hidden_dim)

    cell_state_2 = torch.zeros(self.embedding_dim, self.hidden_dim)

    # Creation of cell state and hidden state for layer 3

    hidden_state_3 = torch.zeros(self.embedding_dim, self.hidden_dim)

    cell_state_3 = torch.zeros(self.embedding_dim, self.hidden_dim)

    # Weights initialization

    torch.nn.init.xavier_normal_(hidden_state)

    torch.nn.init.xavier_normal_(cell_state)

    torch.nn.init.xavier_normal_(hidden_state_2)

    torch.nn.init.xavier_normal_(cell_state_2)

    

    torch.nn.init.xavier_normal_(hidden_state_3)

    torch.nn.init.xavier_normal_(cell_state_3)

    ## End of Initialisation

    # Unfolding LSTM

    for input_t in range(self.sequence_len):

      # print(f'packed_embedded.size(0){len(packed_embedded)}')

      hidden_state_1, cell_state_1 = self.lstm_cell_layer_1(packed_embedded,(hidden_state, cell_state))

      hidden_state_2, cell_state_2 = self.lstm_cell_2(hidden_state_1, (hidden_state_2, cell_state_2))

      hidden_state_3, cell_state_3 = self.lstm_cell_3(hidden_state_2, (hidden_state_3, cell_state_3))

    

    #unpack sequence

    output, output_lengths = nn.utils.rnn.pad_packed_sequence(hidden_state_3)

  

    hidden = self.dropout(torch.cat((hidden_state_3[-2,:,:], hidden_state_3[-1,:,:]), dim = 1))

                        

    return self.fc(hidden)
</code></pre>
</blockquote>
<p><strong>Error</strong>:</p>
<blockquote>
<p>AttributeError                            Traceback (most recent call last)<br>
 in ()<br>
7     start_time = time.time()<br>
8<br>
----&gt; 9     train_loss, train_acc = train(model, train_iterator, optimizer, criterion)<br>
10     valid_loss, valid_acc = evaluate(model, valid_iterator, criterion)<br>
11</p>
<p>5 frames<br>
 in train(model, iterator, optimizer, criterion)<br>
13<br>
14         text_lengths = text_lengths.cpu()<br>
—&gt; 15         predictions = model(text, text_lengths).squeeze(1)<br>
16<br>
17         loss = criterion(predictions, batch.label)</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p> in forward(self, text, text_lengths)<br>
76         for input_t in range(self.sequence_len):<br>
77           # print(f’packed_embedded.size(0){len(packed_embedded)}')<br>
—&gt; 78           hidden_state_1, cell_state_1 = self.lstm_cell_layer_1(packed_embedded,(hidden_state, cell_state))<br>
79           hidden_state_2, cell_state_2 = self.lstm_cell_2(hidden_state_1, (hidden_state_2, cell_state_2))<br>
80           hidden_state_3, cell_state_3 = self.lstm_cell_3(hidden_state_2, (hidden_state_3, cell_state_3))</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/rnn.py in forward(self, input, hx)<br>
963<br>
964     def forward(self, input: Tensor, hx: Optional[Tuple[Tensor, Tensor]] = None) → Tuple[Tensor, Tensor]:<br>
 → 965         self.check_forward_input(input)<br>
966         if hx is None:<br>
967             zeros = torch.zeros(input.size(0), self.hidden_size, dtype=input.dtype, device=input.device)</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/rnn.py in check_forward_input(self, input)<br>
788<br>
789     def check_forward_input(self, input: Tensor) → None:<br>
 → 790         if input.size(1) != self.input_size:<br>
791             raise RuntimeError(<br>
792                 “input has inconsistent input_size: got {}, expected {}”.format(</p>
<p>AttributeError: ‘PackedSequence’ object has no attribute ‘size’</p>
</blockquote>