class_docstr: Module for dictionary feature embeddings for tokens. Dictionary features are
  also known as gazetteer features. These are per token discrete features that
  the module learns embeddings for.
  Example: For the utterance *Order coffee from Starbucks*, the dictionary
  features could be
  ::
  
      [
          {"tokenIdx": 1, "features": {"drink/beverage": 0.8, "music/song": 0.2}},
          {"tokenIdx": 3, "features": {"store/coffee_shop": 1.0}}
      ]
  
  ::
  Thus, for a given token there can be more than one dictionary features each
  of which has a confidence score. The final embedding for a token is the
  weighted average of the dictionary embeddings followed by a pooling operation
  such that the module produces an embedding vector per token.
  
  Args:
      num_embeddings (int): Total number of dictionary features (vocabulary size).
      embed_dim (int): Size of embedding vector.
      pooling_type (PoolingType): Type of pooling for combining the dictionary
          feature embeddings.
  
  Attributes:
      pooling_type (PoolingType): Type of pooling for combining the dictionary
          feature embeddings.
class_docstr_tok: ["Module","for","dictionary","feature","embeddings","for","tokens",".","Dictionary","features","are","also","known","as","gazetteer","features",".","These","are","per","token","discrete","features","that","the","module","learns","embeddings","for",".","Example",":","For","the","utterance","*","Order","coffee","from","Starbucks","*",",","the","dictionary","features","could","be",":",":","[","{","``","tokenIdx","''",":","1",",","``","features","''",":","{","``","drink\/beverage","''",":","0.8",",","``","music\/song","''",":","0.2","}","}",",","{","``","tokenIdx","''",":","3",",","``","features","''",":","{","``","store\/coffee_shop","''",":","1.0","}","}","]",":",":","Thus",",","for","a","given","token","there","can","be","more","than","one","dictionary","features","each","of","which","has","a","confidence","score",".","The","final","embedding","for","a","token","is","the","weighted","average","of","the","dictionary","embeddings","followed","by","a","pooling","operation","such","that","the","module","produces","an","embedding","vector","per","token",".","Args",":","num_embeddings","(","int",")",":","Total","number","of","dictionary","features","(","vocabulary","size",")",".","embed_dim","(","int",")",":","Size","of","embedding","vector",".","pooling_type","(","PoolingType",")",":","Type","of","pooling","for","combining","the","dictionary","feature","embeddings",".","Attributes",":","pooling_type","(","PoolingType",")",":","Type","of","pooling","for","combining","the","dictionary","feature","embeddings","."]
docstr: Given a batch of sentences such containing dictionary feature ids per
  token, produce token embedding vectors for each sentence in the batch.
  
  Args:
      feats (torch.Tensor): Batch of sentences with dictionary feature ids.
          shape: [bsz, seq_len * max_feat_per_token]
      weights (torch.Tensor): Batch of sentences with dictionary feature
          weights for the dictionary features.
          shape: [bsz, seq_len * max_feat_per_token]
      lengths (torch.Tensor): Batch of sentences with the number of
          dictionary features per token.
          shape: [bsz, seq_len]
  
  Returns:
      torch.Tensor: Embedded batch of sentences. Dimension:
      batch size X maximum sentence length, token embedding size.
      Token embedding size = `embed_dim` passed to the constructor.
docstr_tok: ["Given","a","batch","of","sentences","such","containing","dictionary","feature","ids","per","token",",","produce","token","embedding","vectors","for","each","sentence","in","the","batch",".","Args",":","feats","(","torch.Tensor",")",":","Batch","of","sentences","with","dictionary","feature","ids",".","shape",":","[","bsz",",","seq_len","*","max_feat_per_token","]","weights","(","torch.Tensor",")",":","Batch","of","sentences","with","dictionary","feature","weights","for","the","dictionary","features",".","shape",":","[","bsz",",","seq_len","*","max_feat_per_token","]","lengths","(","torch.Tensor",")",":","Batch","of","sentences","with","the","number","of","dictionary","features","per","token",".","shape",":","[","bsz",",","seq_len","]","Returns",":","torch.Tensor",":","Embedded","batch","of","sentences",".","Dimension",":","batch","size","X","maximum","sentence","length",",","token","embedding","size",".","Token","embedding","size","=","`","embed_dim","`","passed","to","the","constructor","."]
code: def forward(
      self,
      feats: torch.Tensor,
      weights: torch.Tensor,
      lengths: torch.Tensor,
  ) -> torch.Tensor:
      """Given a batch of sentences such containing dictionary feature ids per
      token, produce token embedding vectors for each sentence in the batch.
  
      Args:
          feats (torch.Tensor): Batch of sentences with dictionary feature ids.
              shape: [bsz, seq_len * max_feat_per_token]
          weights (torch.Tensor): Batch of sentences with dictionary feature
              weights for the dictionary features.
              shape: [bsz, seq_len * max_feat_per_token]
          lengths (torch.Tensor): Batch of sentences with the number of
              dictionary features per token.
              shape: [bsz, seq_len]
  
      Returns:
          torch.Tensor: Embedded batch of sentences. Dimension:
          batch size X maximum sentence length, token embedding size.
          Token embedding size = `embed_dim` passed to the constructor.
  
      """
      batch_size = torch.onnx.operators.shape_as_tensor(feats)[0]
      max_toks = torch.onnx.operators.shape_as_tensor(lengths)[1]
  
      if self.unk_index != self.pad_index:
          # convert all unk indices to pad indices
          feats = self.find_and_replace(
              feats, self.unk_index, self.pad_index
          )
  
      dict_emb = self.embedding(feats)
  
      # Calculate weighted average of the embeddings
      weighted_embds = (
          dict_emb * weights.unsqueeze(2)
          if self.use_weights
          else dict_emb
      )
      new_emb_shape = torch.cat(
          (
              batch_size.view(1),
              max_toks.view(1),
              torch.tensor([-1]).long(),
              torch.tensor([weighted_embds.size()[-1]]).long(),
          )
      )
      weighted_embds = torch.onnx.operators.reshape_from_tensor_shape(
          weighted_embds, new_emb_shape
      )
  
      # Temporary workaround till https://github.com/pytorch/pytorch/issues/32840
      # is resolved
      if self.pooling_type == "mean":
          reduced_embeds = torch.sum(
              weighted_embds, dim=2
          ) / lengths.unsqueeze(2).to(weighted_embds.dtype)
      elif self.pooling_type == "max":
          reduced_embeds, _ = torch.max(weighted_embds, dim=2)
      else:
          raise RuntimeError(
              f"Pooling type {self.pooling_type} is unsupported."
          )
  
      return reduced_embeds
  
file_path: pytext/pytext/models/embeddings/dict_embedding.py
imports: from typing import Optional
  import torch
  import torch.nn
  import torch.onnx.operators
  from pytext.config.field_config import DictFeatConfig
  from pytext.config.module_config import PoolingType
  from pytext.data.tensorizers import Tensorizer
  from pytext.data.utils import PAD_INDEX, UNK_INDEX, Vocabulary
  from pytext.fields import FieldMeta
  from pytext.utils.usage import log_class_usage
  from .embedding_base import EmbeddingBase
label_desc: Use image node_id 4 for calling the DictEmbedding obj's underlying member method code with example usage: obj.forward(feats, weights, lengths) and returns: reduced_embeds