recurrent neural networks def forward(selftexttext_lengths)embedded self dropout(self embedding(text)#pack sequence packed_embedded nn utils rnn pack_padded_ sequence(embeddedtext_lengthspacked_output(hiddencellself lstm(packed_ embedded#unpack sequence outputoutput_lengths nn utils rnn pad_packed_ sequence(packed_outputhidden self dropout(torch cat((hidden[- ,:,:]hidden[- ,:,:])dim )return self fc(hiddennotice that we have made quite few additions here we now have an lstm layer instead of the vanilla rnn when the bidirectional flag is set to trueit enables us to capture the forward as well backward context the dimensions of the linear layer would be now twice the original layeras we have both forward and backward network functioning in tandem we initially added include_lengths=true while defining our original fieldthereforeour forward function will now take one extra parameter this information is necessary while packing and unpacking the data after receiving from the embedding output and before passing it to the linear layer the hidden layer now concatenates the output from the forward as well as the backward network before passing it to the next layer listing - defines the model properties and copies the pretrained weights