class_docstr: Wrapper for nn.LSTM
  
  Differences include:
  * weight initialization
  * the bidirectional option makes the first layer bidirectional only
  (and in that case the hidden dim is divided by 2)
class_docstr_tok: ["Wrapper","for","nn.LSTM","Differences","include",":","*","weight","initialization","*","the","bidirectional","option","makes","the","first","layer","bidirectional","only","(","and","in","that","case","the","hidden","dim","is","divided","by","2",")"]
code: def __init__(
      self,
      num_layers,
      bidirectional,
      embed_dim,
      hidden_dim,
      dropout,
      residual_level,
  ):
      super().__init__()
      self.num_layers = num_layers
      self.bidirectional = bidirectional
      if bidirectional:
          assert (
              hidden_dim % 2 == 0
          ), "hidden_dim should be even if bidirectional"
      self.hidden_dim = hidden_dim
      self.residual_level = residual_level
      self.layers = nn.ModuleList([])
      for layer in range(num_layers):
          is_layer_bidirectional = bidirectional and layer == 0
          if is_layer_bidirectional:
              assert hidden_dim % 2 == 0, (
                  "hidden_dim must be even if bidirectional "
                  "(to be divided evenly between directions)"
              )
          self.layers.append(
              BiLSTM.LSTM(
                  embed_dim if layer == 0 else hidden_dim,
                  hidden_dim // 2
                  if is_layer_bidirectional
                  else hidden_dim,
                  num_layers=1,
                  dropout=dropout,
                  bidirectional=is_layer_bidirectional,
              )
          )
  
imports: import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  import torch.onnx.operators
  from fairseq import options, utils
  from fairseq.models import FairseqEncoder, FairseqEncoderDecoderModel, register_model, register_model_architecture
  from fairseq.modules import AdaptiveSoftmax
  from pytorch_translate import attention, rnn_cell, utils, vocab_reduction
  from pytorch_translate.common_layers import ContextEmbedding, DecoderWithOutputProjection, Embedding, Linear, RNNLayer, VariableTracker
  from pytorch_translate.data import data
  from pytorch_translate.multi_model import MultiDecoder, MultiEncoder
  from pytorch_translate.multilingual import MultilingualDecoder, MultilingualEncoder
  from pytorch_translate.ngram import NGramDecoder
  from pytorch_translate.semi_supervised import SemiSupervisedModel
  from pytorch_translate.utils import maybe_cat, maybe_cuda, torch_find
  from torch.nn.utils.rnn import pack_padded_sequence, PackedSequence, pad_packed_sequence
label_desc: Use image node_id 2 to create a new BiLSTM object from inherited base classes: nn with example: obj = BiLSTM(num_layers, bidirectional, embed_dim, hidden_dim, dropout, residual_level)