class_docstr: This module encodes two modalities separately, and performs bi-directional
  attention using a connection layer. It is based on the modified BertEncoder in
  the paper: [ViLBERT: Pretraining Task-Agnostic Visiolinguistic Representations
  for Vision-and-Language Tasks](https://api.semanticscholar.org/CorpusID:199453025)
  
  # Parameters
  
  num_hidden_layers1: `int` (default = `12`)
      Number of hidden layers in the transformer block for the first modality.
  num_hidden_layers2: `int` (default = `12`)
      Number of hidden layers in the transformer block for the second modality.
  hidden_size1: `int` (default = `1024`)
  hidden_size2: `int` (default = `1024`)
  combined_hidden_size: `int` (default = `1024`)
      Hidden size for the connection layer.
  intermediate_size1: `int` (default = `1024`)
  intermediate_size2: `int` (default = `1024`)
  num_attention_heads1: `int` (default = `8`)
  num_attention_heads2: `int` (default = `8`)
  combined_num_attention_heads: `int` (default = `8`)
      Number of attention heads in the connection layer.
  attention_dropout1: `float` (default = `0.1`)
  hidden_dropout1: `float` (default = `0.1`)
  attention_dropout2: `float` (default = `0.1`)
  hidden_dropout2: `float` (default = `0.1`)
  biattention_id1: `List`, optional (default = `[1]`)
  biattention_id2: `List`, optional (default = `[1]`)
  fixed_layer1: `int` (default = `0`)
  fixed_layer2: `int` (default = `0`)
  fast_mode: `bool` (default = `False`)
  with_coattention: `bool` (default = `True`)
  in_batch_pairs: `bool` (default = `False`)
class_docstr_tok: ["This","module","encodes","two","modalities","separately",",","and","performs","bi-directional","attention","using","a","connection","layer",".","It","is","based","on","the","modified","BertEncoder","in","the","paper",":","[","ViLBERT",":","Pretraining","Task-Agnostic","Visiolinguistic","Representations","for","Vision-and-Language","Tasks","]","(","https",":","\/\/api.semanticscholar.org\/CorpusID:199453025",")","#","Parameters","num_hidden_layers1",":","`","int","`","(","default","=","`","12","`",")","Number","of","hidden","layers","in","the","transformer","block","for","the","first","modality",".","num_hidden_layers2",":","`","int","`","(","default","=","`","12","`",")","Number","of","hidden","layers","in","the","transformer","block","for","the","second","modality",".","hidden_size1",":","`","int","`","(","default","=","`","1024","`",")","hidden_size2",":","`","int","`","(","default","=","`","1024","`",")","combined_hidden_size",":","`","int","`","(","default","=","`","1024","`",")","Hidden","size","for","the","connection","layer",".","intermediate_size1",":","`","int","`","(","default","=","`","1024","`",")","intermediate_size2",":","`","int","`","(","default","=","`","1024","`",")","num_attention_heads1",":","`","int","`","(","default","=","`","8","`",")","num_attention_heads2",":","`","int","`","(","default","=","`","8","`",")","combined_num_attention_heads",":","`","int","`","(","default","=","`","8","`",")","Number","of","attention","heads","in","the","connection","layer",".","attention_dropout1",":","`","float","`","(","default","=","`","0.1","`",")","hidden_dropout1",":","`","float","`","(","default","=","`","0.1","`",")","attention_dropout2",":","`","float","`","(","default","=","`","0.1","`",")","hidden_dropout2",":","`","float","`","(","default","=","`","0.1","`",")","biattention_id1",":","`","List","`",",","optional","(","default","=","`","[","1","]","`",")","biattention_id2",":","`","List","`",",","optional","(","default","=","`","[","1","]","`",")","fixed_layer1",":","`","int","`","(","default","=","`","0","`",")","fixed_layer2",":","`","int","`","(","default","=","`","0","`",")","fast_mode",":","`","bool","`","(","default","=","`","False","`",")","with_coattention",":","`","bool","`","(","default","=","`","True","`",")","in_batch_pairs",":","`","bool","`","(","default","=","`","False","`",")"]
code: def __init__(
      self,
      num_hidden_layers1: int = 12,
      num_hidden_layers2: int = 12,
      hidden_size1: int = 1024,
      hidden_size2: int = 1024,
      combined_hidden_size: int = 1024,
      intermediate_size1: int = 1024,
      intermediate_size2: int = 1024,
      num_attention_heads1: int = 8,
      num_attention_heads2: int = 8,
      combined_num_attention_heads: int = 8,
      attention_dropout1: float = 0.1,
      hidden_dropout1: float = 0.1,
      attention_dropout2: float = 0.1,
      hidden_dropout2: float = 0.1,
      activation: str = "relu",
      biattention_id1: Optional[List[int]] = None,
      biattention_id2: Optional[List[int]] = None,
      fixed_layer1: int = 0,
      fixed_layer2: int = 0,
      fast_mode: bool = False,
      with_coattention: bool = True,
      in_batch_pairs: bool = False,
  ):
      super().__init__()
  
      self.FAST_MODE = fast_mode
      self.with_coattention = with_coattention
      self.biattention_id1 = biattention_id1 or [1]
      self.biattention_id2 = biattention_id2 or [1]
      self.in_batch_pairs = in_batch_pairs
      self.fixed_layer1 = fixed_layer1
      self.fixed_layer2 = fixed_layer2
      self.combined_size = combined_hidden_size
      self.hidden_size1 = hidden_size1
      self.hidden_size2 = hidden_size2
  
      layer1 = TransformerLayer(
          hidden_size=hidden_size1,
          intermediate_size=intermediate_size1,
          num_attention_heads=num_attention_heads1,
          attention_dropout=attention_dropout1,
          hidden_dropout=hidden_dropout1,
          activation=activation,
      )
      layer2 = TransformerLayer(
          hidden_size=hidden_size2,
          intermediate_size=intermediate_size2,
          num_attention_heads=num_attention_heads2,
          attention_dropout=attention_dropout2,
          hidden_dropout=hidden_dropout2,
          activation=activation,
      )
      connect_layer = BiModalConnectionLayer(
          hidden_size1=hidden_size1,
          hidden_size2=hidden_size2,
          combined_hidden_size=combined_hidden_size,
          intermediate_size1=intermediate_size1,
          intermediate_size2=intermediate_size2,
          num_attention_heads=combined_num_attention_heads,
          dropout1=hidden_dropout1,
          dropout2=hidden_dropout2,
          activation=activation,
      )
  
      self.layers1 = replicate_layers(layer1, num_hidden_layers1)
      self.layers2 = replicate_layers(layer2, num_hidden_layers2)
      self.c_layer = replicate_layers(
          connect_layer, len(self.biattention_id2)
      )
  
file_path: allennlp/allennlp/modules/transformer/bimodal_encoder.py
imports: from typing import Optional, List, TYPE_CHECKING
  import torch
  from allennlp.common import FromParams
  from allennlp.modules.util import replicate_layers
  from allennlp.modules.transformer.transformer_layer import TransformerLayer
  from allennlp.modules.transformer.bimodal_connection_layer import BiModalConnectionLayer
  from allennlp.modules.transformer.transformer_module import TransformerModule
label_desc: Use image node_id 1 to create a new BiModalEncoder object from inherited base classes: TransformerModule, FromParams with example: obj = BiModalEncoder(num_hidden_layers1, num_hidden_layers2, hidden_size1, hidden_size2, combined_hidden_size, intermediate_size1, intermediate_size2, num_attention_heads1, num_attention_heads2, combined_num_attention_heads, attention_dropout1, hidden_dropout1, attention_dropout2, hidden_dropout2, activation, biattention_id1, biattention_id2, fixed_layer1, fixed_layer2, fast_mode, with_coattention, in_batch_pairs)