class_docstr: Compute ELMo representations using a pre-trained bidirectional language model.
  
  See "Deep contextualized word representations", Peters et al. for details.
  
  This module takes character id input and computes `num_output_representations` different layers
  of ELMo representations.  Typically `num_output_representations` is 1 or 2.  For example, in
  the case of the SRL model in the above paper, `num_output_representations=1` where ELMo was included at
  the input token representation layer.  In the case of the SQuAD model, `num_output_representations=2`
  as ELMo was also included at the GRU output layer.
  
  In the implementation below, we learn separate scalar weights for each output layer,
  but only run the biLM once on each input sequence for efficiency.
  
  # Parameters
  
  options_file : `str`, required.
      ELMo JSON options file
  weight_file : `str`, required.
      ELMo hdf5 weight file
  num_output_representations : `int`, required.
      The number of ELMo representation to output with
      different linear weighted combination of the 3 layers (i.e.,
      character-convnet output, 1st lstm output, 2nd lstm output).
  requires_grad : `bool`, optional
      If True, compute gradient of ELMo parameters for fine tuning.
  do_layer_norm : `bool`, optional, (default = `False`).
      Should we apply layer normalization (passed to `ScalarMix`)?
  dropout : `float`, optional, (default = `0.5`).
      The dropout to be applied to the ELMo representations.
  vocab_to_cache : `List[str]`, optional, (default = `None`).
      A list of words to pre-compute and cache character convolutions
      for. If you use this option, Elmo expects that you pass word
      indices of shape (batch_size, timesteps) to forward, instead
      of character indices. If you use this option and pass a word which
      wasn't pre-cached, this will break.
  keep_sentence_boundaries : `bool`, optional, (default = `False`)
      If True, the representation of the sentence boundary tokens are
      not removed.
  scalar_mix_parameters : `List[float]`, optional, (default = `None`)
      If not `None`, use these scalar mix parameters to weight the representations
      produced by different layers. These mixing weights are not updated during
      training. The mixing weights here should be the unnormalized (i.e., pre-softmax)
      weights. So, if you wanted to use only the 1st layer of a 2-layer ELMo,
      you can set this to [-9e10, 1, -9e10 ].
  module : `torch.nn.Module`, optional, (default = `None`).
      If provided, then use this module instead of the pre-trained ELMo biLM.
      If using this option, then pass `None` for both `options_file`
      and `weight_file`.  The module must provide a public attribute
      `num_layers` with the number of internal layers and its `forward`
      method must return a `dict` with `activations` and `mask` keys
      (see `_ElmoBilm` for an example).  Note that `requires_grad` is also
      ignored with this option.
class_docstr_tok: ["Compute","ELMo","representations","using","a","pre-trained","bidirectional","language","model",".","See","``","Deep","contextualized","word","representations","''",",","Peters","et","al",".","for","details",".","This","module","takes","character","id","input","and","computes","`","num_output_representations","`","different","layers","of","ELMo","representations",".","Typically","`","num_output_representations","`","is","1","or","2",".","For","example",",","in","the","case","of","the","SRL","model","in","the","above","paper",",","`","num_output_representations=1","`","where","ELMo","was","included","at","the","input","token","representation","layer",".","In","the","case","of","the","SQuAD","model",",","`","num_output_representations=2","`","as","ELMo","was","also","included","at","the","GRU","output","layer",".","In","the","implementation","below",",","we","learn","separate","scalar","weights","for","each","output","layer",",","but","only","run","the","biLM","once","on","each","input","sequence","for","efficiency",".","#","Parameters","options_file",":","`","str","`",",","required",".","ELMo","JSON","options","file","weight_file",":","`","str","`",",","required",".","ELMo","hdf5","weight","file","num_output_representations",":","`","int","`",",","required",".","The","number","of","ELMo","representation","to","output","with","different","linear","weighted","combination","of","the","3","layers","(","i.e.",",","character-convnet","output",",","1st","lstm","output",",","2nd","lstm","output",")",".","requires_grad",":","`","bool","`",",","optional","If","True",",","compute","gradient","of","ELMo","parameters","for","fine","tuning",".","do_layer_norm",":","`","bool","`",",","optional",",","(","default","=","`","False","`",")",".","Should","we","apply","layer","normalization","(","passed","to","`","ScalarMix","`",")","?","dropout",":","`","float","`",",","optional",",","(","default","=","`","0.5","`",")",".","The","dropout","to","be","applied","to","the","ELMo","representations",".","vocab_to_cache",":","`","List","[","str","]","`",",","optional",",","(","default","=","`","None","`",")",".","A","list","of","words","to","pre-compute","and","cache","character","convolutions","for",".","If","you","use","this","option",",","Elmo","expects","that","you","pass","word","indices","of","shape","(","batch_size",",","timesteps",")","to","forward",",","instead","of","character","indices",".","If","you","use","this","option","and","pass","a","word","which","was","n't","pre-cached",",","this","will","break",".","keep_sentence_boundaries",":","`","bool","`",",","optional",",","(","default","=","`","False","`",")","If","True",",","the","representation","of","the","sentence","boundary","tokens","are","not","removed",".","scalar_mix_parameters",":","`","List","[","float","]","`",",","optional",",","(","default","=","`","None","`",")","If","not","`","None","`",",","use","these","scalar","mix","parameters","to","weight","the","representations","produced","by","different","layers",".","These","mixing","weights","are","not","updated","during","training",".","The","mixing","weights","here","should","be","the","unnormalized","(","i.e.",",","pre-softmax",")","weights",".","So",",","if","you","wanted","to","use","only","the","1st","layer","of","a","2-layer","ELMo",",","you","can","set","this","to","[","-9e10",",","1",",","-9e10","]",".","module",":","`","torch.nn.Module","`",",","optional",",","(","default","=","`","None","`",")",".","If","provided",",","then","use","this","module","instead","of","the","pre-trained","ELMo","biLM",".","If","using","this","option",",","then","pass","`","None","`","for","both","`","options_file","`","and","`","weight_file","`",".","The","module","must","provide","a","public","attribute","`","num_layers","`","with","the","number","of","internal","layers","and","its","`","forward","`","method","must","return","a","`","dict","`","with","`","activations","`","and","`","mask","`","keys","(","see","`","_ElmoBilm","`","for","an","example",")",".","Note","that","`","requires_grad","`","is","also","ignored","with","this","option","."]
code: def __init__(
      self,
      options_file: str,
      weight_file: str,
      num_output_representations: int,
      requires_grad: bool = False,
      do_layer_norm: bool = False,
      dropout: float = 0.5,
      vocab_to_cache: List[str] = None,
      keep_sentence_boundaries: bool = False,
      scalar_mix_parameters: List[float] = None,
      module: torch.nn.Module = None,
  ) -> None:
      super().__init__()
  
      logger.info("Initializing ELMo")
      if module is not None:
          if options_file is not None or weight_file is not None:
              raise ConfigurationError(
                  "Don't provide options_file or weight_file with module"
              )
          self._elmo_lstm = module
      else:
          self._elmo_lstm = _ElmoBiLm(  # type: ignore
              options_file,
              weight_file,
              requires_grad=requires_grad,
              vocab_to_cache=vocab_to_cache,
          )
      self._has_cached_vocab = vocab_to_cache is not None
      self._keep_sentence_boundaries = keep_sentence_boundaries
      self._dropout = Dropout(p=dropout)
      self._scalar_mixes: Any = []
      for k in range(num_output_representations):
          scalar_mix = ScalarMix(
              self._elmo_lstm.num_layers,  # type: ignore
              do_layer_norm=do_layer_norm,
              initial_scalar_parameters=scalar_mix_parameters,
              trainable=scalar_mix_parameters is None,
          )
          self.add_module("scalar_mix_{}".format(k), scalar_mix)
          self._scalar_mixes.append(scalar_mix)
  
imports: import json
  import logging
  import warnings
  from typing import Any, Dict, List, Union
  import numpy
  import torch
  from torch.nn.modules import Dropout
  from allennlp.common import FromParams
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.file_utils import cached_path
  from allennlp.common.util import lazy_groups_of
  from allennlp.data.instance import Instance
  from allennlp.data.tokenizers.token_class import Token
  from allennlp.data.vocabulary import Vocabulary
  from allennlp.data.batch import Batch
  from allennlp.data.fields import TextField
  from allennlp.data.token_indexers.elmo_indexer import ELMoCharacterMapper, ELMoTokenCharactersIndexer
  from allennlp.modules.elmo_lstm import ElmoLstm
  from allennlp.modules.highway import Highway
  from allennlp.modules.scalar_mix import ScalarMix
  from allennlp.nn.util import add_sentence_boundary_token_ids, get_device_of, remove_sentence_boundaries
label_desc: Use image node_id 1 to create a new Elmo object from inherited base classes: torch.nn, FromParams with example: obj = Elmo(options_file, weight_file, num_output_representations, requires_grad, do_layer_norm, dropout, vocab_to_cache, keep_sentence_boundaries, scalar_mix_parameters, module)