class_docstr: A stacked, bidirectional LSTM which uses
  :class:`~allennlp.modules.lstm_cell_with_projection.LstmCellWithProjection`'s
  with highway layers between the inputs to layers.
  The inputs to the forward and backward directions are independent - forward and backward
  states are not concatenated between layers.
  Additionally, this LSTM maintains its `own` state, which is updated every time
  ``forward`` is called. It is dynamically resized for different batch sizes and is
  designed for use with non-continuous inputs (i.e inputs which aren't formatted as a stream,
  such as text used for a language modelling task, which is how stateful RNNs are typically used).
  This is non-standard, but can be thought of as having an "end of sentence" state, which is
  carried across different sentences.
  Parameters
  ----------
  input_size : ``int``, required
      The dimension of the inputs to the LSTM.
  hidden_size : ``int``, required
      The dimension of the outputs of the LSTM.
  cell_size : ``int``, required.
      The dimension of the memory cell of the
      :class:`~allennlp.modules.lstm_cell_with_projection.LstmCellWithProjection`.
  num_layers : ``int``, required
      The number of bidirectional LSTMs to use.
  requires_grad: ``bool``, optional
      If True, compute gradient of ELMo parameters for fine tuning.
  recurrent_dropout_probability: ``float``, optional (default = 0.0)
      The dropout probability to be used in a dropout scheme as stated in
      `A Theoretically Grounded Application of Dropout in Recurrent Neural Networks
      <https://arxiv.org/abs/1512.05287>`_ .
  state_projection_clip_value: ``float``, optional, (default = None)
      The magnitude with which to clip the hidden_state after projecting it.
  memory_cell_clip_value: ``float``, optional, (default = None)
      The magnitude with which to clip the memory cell.
docstr: Load the pre-trained weights from the file.
docstr_tok: ["Load","the","pre-trained","weights","from","the","file","."]
code: def load_weights(self, weight_file):
      """
      Load the pre-trained weights from the file.
      """
      requires_grad = self.requires_grad
  
      with h5py.File(cached_path(weight_file), "r") as fin:
          for i_layer, lstms in enumerate(
              zip(self.forward_layers, self.backward_layers)
          ):
              for j_direction, lstm in enumerate(lstms):
                  # lstm is an instance of LSTMCellWithProjection
                  cell_size = lstm.cell_size
  
                  dataset = fin["RNN_%s" % j_direction]["RNN"][
                      "MultiRNNCell"
                  ]["Cell%s" % i_layer]["LSTMCell"]
  
                  # tensorflow packs together both W and U matrices into one matrix,
                  # but pytorch maintains individual matrices.  In addition, tensorflow
                  # packs the gates as input, memory, forget, output but pytorch
                  # uses input, forget, memory, output.  So we need to modify the weights.
                  tf_weights = numpy.transpose(dataset["W_0"][...])
                  torch_weights = tf_weights.copy()
  
                  # split the W from U matrices
                  input_size = lstm.input_size
                  input_weights = torch_weights[:, :input_size]
                  recurrent_weights = torch_weights[:, input_size:]
                  tf_input_weights = tf_weights[:, :input_size]
                  tf_recurrent_weights = tf_weights[:, input_size:]
  
                  # handle the different gate order convention
                  for torch_w, tf_w in [
                      [input_weights, tf_input_weights],
                      [recurrent_weights, tf_recurrent_weights],
                  ]:
                      torch_w[
                          (1 * cell_size) : (2 * cell_size), :
                      ] = tf_w[(2 * cell_size) : (3 * cell_size), :]
                      torch_w[
                          (2 * cell_size) : (3 * cell_size), :
                      ] = tf_w[(1 * cell_size) : (2 * cell_size), :]
  
                  lstm.input_linearity.weight.data.copy_(
                      torch.FloatTensor(input_weights)
                  )
                  lstm.state_linearity.weight.data.copy_(
                      torch.FloatTensor(recurrent_weights)
                  )
                  lstm.input_linearity.weight.requires_grad = (
                      requires_grad
                  )
                  lstm.state_linearity.weight.requires_grad = (
                      requires_grad
                  )
  
                  # the bias weights
                  tf_bias = dataset["B"][...]
                  # tensorflow adds 1.0 to forget gate bias instead of modifying the
                  # parameters...
                  tf_bias[(2 * cell_size) : (3 * cell_size)] += 1
                  torch_bias = tf_bias.copy()
                  torch_bias[
                      (1 * cell_size) : (2 * cell_size)
                  ] = tf_bias[(2 * cell_size) : (3 * cell_size)]
                  torch_bias[
                      (2 * cell_size) : (3 * cell_size)
                  ] = tf_bias[(1 * cell_size) : (2 * cell_size)]
                  lstm.state_linearity.bias.data.copy_(
                      torch.FloatTensor(torch_bias)
                  )
                  lstm.state_linearity.bias.requires_grad = (
                      requires_grad
                  )
  
                  # the projection weights
                  proj_weights = numpy.transpose(dataset["W_P_0"][...])
                  lstm.state_projection.weight.data.copy_(
                      torch.FloatTensor(proj_weights)
                  )
                  lstm.state_projection.weight.requires_grad = (
                      requires_grad
                  )
  
file_path: magnitude/pymagnitude/third_party/allennlp/modules/elmo_lstm.py
imports: import warnings
  import torch
  from torch.nn.utils.rnn import PackedSequence, pad_packed_sequence
  import numpy
  from allennlp.modules.lstm_cell_with_projection import LstmCellWithProjection
  from allennlp.common.checks import ConfigurationError
  from allennlp.modules.encoder_base import _EncoderBase
  from allennlp.common.file_utils import cached_path
label_desc: Use image node_id 4 for calling the ElmoLstm obj's underlying member method code with example usage: obj.load_weights(weight_file) without return types