class_docstr: Base class for all Bi-directional Transformer based Sentence Encoders. All
  children of this class should implement an _encoder function which takes
  as input: tokens, [optional] segment labels and a pad mask and outputs both
  the sentence representation (output of _pool_encoded_layers) and the
  output states of all the intermediate Transformer layers as a list of
  tensors.
  
  Input tuple consists of the following elements:
  1) tokens: torch tensor of size B x T which contains tokens ids
  2) pad_mask: torch tensor of size B x T generated with the condition
  tokens != self.vocab.get_pad_index()
  3) segment_labels: torch tensor of size B x T which contains the segment
  id of each token
  
  Output tuple consists of the following elements:
  1) encoded_layers: List of torch tensors where each tensor has shape
  B x T x C and there are num_transformer_layers + 1 of these.
  Each tensor represents the output of the intermediate
  transformer layers with the 0th element being the input to the
  first transformer layer (token + segment + position emebdding).
  2) [Optional] pooled_output: Output of the pooling operation associated
  with config.pooling_method to the encoded_layers.
  Size B x C (or B x 4C if pooling = AVG_CONCAT_LAST_4_LAYERS)
class_docstr_tok: ["Base","class","for","all","Bi-directional","Transformer","based","Sentence","Encoders",".","All","children","of","this","class","should","implement","an","_encoder","function","which","takes","as","input",":","tokens",",","[","optional","]","segment","labels","and","a","pad","mask","and","outputs","both","the","sentence","representation","(","output","of","_pool_encoded_layers",")","and","the","output","states","of","all","the","intermediate","Transformer","layers","as","a","list","of","tensors",".","Input","tuple","consists","of","the","following","elements",":","1",")","tokens",":","torch","tensor","of","size","B","x","T","which","contains","tokens","ids","2",")","pad_mask",":","torch","tensor","of","size","B","x","T","generated","with","the","condition","tokens","!","=","self.vocab.get_pad_index","(",")","3",")","segment_labels",":","torch","tensor","of","size","B","x","T","which","contains","the","segment","id","of","each","token","Output","tuple","consists","of","the","following","elements",":","1",")","encoded_layers",":","List","of","torch","tensors","where","each","tensor","has","shape","B","x","T","x","C","and","there","are","num_transformer_layers","+","1","of","these",".","Each","tensor","represents","the","output","of","the","intermediate","transformer","layers","with","the","0th","element","being","the","input","to","the","first","transformer","layer","(","token","+","segment","+","position","emebdding",")",".","2",")","[","Optional","]","pooled_output",":","Output","of","the","pooling","operation","associated","with","config.pooling_method","to","the","encoded_layers",".","Size","B","x","C","(","or","B","x","4C","if","pooling","=","AVG_CONCAT_LAST_4_LAYERS",")"]
code: def _pool_encoded_layers(
      self, encoded_layers: torch.Tensor, pad_mask: torch.Tensor
  ) -> torch.Tensor:
      if self.pooling == PoolingMethod.NO_POOL:
          return None
      elif self.pooling == PoolingMethod.AVG_CONCAT_LAST_4_LAYERS:
          sent_out = torch.cat(encoded_layers[-4:], 2)
      elif self.pooling == PoolingMethod.AVG_SUM_LAST_4_LAYERS:
          sent_out = torch.stack(encoded_layers[-4:]).sum(0)
      elif self.pooling == PoolingMethod.AVG_SECOND_TO_LAST_LAYER:
          sent_out = encoded_layers[-2]
      elif self.pooling == PoolingMethod.AVG_LAST_LAYER:
          sent_out = encoded_layers[-1]
      else:
          raise Exception(
              "{} pooling is not supported".format(self.pooling)
          )
  
      pad_mask = pad_mask.unsqueeze(2).type_as(encoded_layers[0])
      sent_out = sent_out * pad_mask
      pooled_output = torch.sum(sent_out, 1) / torch.sum(pad_mask, 1)
      return pooled_output
  
file_path: pytext/pytext/models/representations/transformer_sentence_encoder_base.py
imports: from enum import Enum
  from typing import Tuple
  import torch
  import torch.nn
  from pytext.config import ConfigBase
  from pytext.models.representations.representation_base import RepresentationBase
  from pytext.models.utils import normalize_embeddings
  from pytext.utils.usage import log_class_usage
label_desc: Use image node_id 4 for calling the TransformerSentenceEncoderBase obj's underlying member method code with example usage: obj._pool_encoded_layers(encoded_layers, pad_mask) and returns: pooled_output, None