class_docstr: Module for embedding tokens in a low-dimensional space.
docstr: Constructs an Embed module.
  
  Args:
    vocab_size: Number of unique tokens to embed. If not provided, an
      existing vocabulary matrix from which vocab_size can be inferred must
      be provided as existing_vocab.
    embed_dim: Number of dimensions to assign to each embedding.
      If not specified, we use ``6 * sqrt(sqrt(vocab_size))``. If an existing
      vocabulary matrix initializes the module, this should not be provided as
      it will be inferred.
    existing_vocab: A ``[vocab_size, embed_dim]`` vocabulary matrix. Will be
      converted to a tf.float32 tensor. If provided, neither or vocab_size or
      embed_dim should be provided as they are inferred.
    densify_gradients: If True, we convert the embedding gradient from an
      ``tf.IndexedSlices`` to a regular tensor before sending it back to the
      parameter server. This avoids excess computation on the parameter
      server. Use this option for moderately sized embeddings, e.g.,
      a vocabulary size on the order of up to thousands. For embeddings larger
      than these, e.g. a vocabulary size on the order of tens or hundreds of
      thousands, set this to False.
    initializer: Initializer for the embeddings. By default,
      embeddings are initialized via a truncated normal distribution.
    trainable: if True, the embeddings will be updated during training. If
      False, they are fixed to their initial values.
    dtype: The dtype to use for the embedding. Defaults to float32.
    name: Name for this module.
  
  Raises:
    ValueError: if neither one of ``vocab_size`` or ``existing_vocab`` is
      provided, or if ``existing_vocab`` is provided along with
      ``vocab_size``, ``embedding_dim``, ``initializer`` (as these should be
      inferred).
code: def __init__(
      self,
      vocab_size: Optional[int] = None,
      embed_dim: Optional[int] = None,
      existing_vocab: Optional[types.TensorLike] = None,
      densify_gradients: bool = False,
      initializer: Optional[initializers.Initializer] = None,
      trainable: bool = True,
      dtype: tf.DType = tf.float32,
      name: Optional[str] = None,
  ):
      """Constructs an Embed module.
  
      Args:
        vocab_size: Number of unique tokens to embed. If not provided, an
          existing vocabulary matrix from which vocab_size can be inferred must
          be provided as existing_vocab.
        embed_dim: Number of dimensions to assign to each embedding.
          If not specified, we use ``6 * sqrt(sqrt(vocab_size))``. If an existing
          vocabulary matrix initializes the module, this should not be provided as
          it will be inferred.
        existing_vocab: A ``[vocab_size, embed_dim]`` vocabulary matrix. Will be
          converted to a tf.float32 tensor. If provided, neither or vocab_size or
          embed_dim should be provided as they are inferred.
        densify_gradients: If True, we convert the embedding gradient from an
          ``tf.IndexedSlices`` to a regular tensor before sending it back to the
          parameter server. This avoids excess computation on the parameter
          server. Use this option for moderately sized embeddings, e.g.,
          a vocabulary size on the order of up to thousands. For embeddings larger
          than these, e.g. a vocabulary size on the order of tens or hundreds of
          thousands, set this to False.
        initializer: Initializer for the embeddings. By default,
          embeddings are initialized via a truncated normal distribution.
        trainable: if True, the embeddings will be updated during training. If
          False, they are fixed to their initial values.
        dtype: The dtype to use for the embedding. Defaults to float32.
        name: Name for this module.
  
      Raises:
        ValueError: if neither one of ``vocab_size`` or ``existing_vocab`` is
          provided, or if ``existing_vocab`` is provided along with
          ``vocab_size``, ``embedding_dim``, ``initializer`` (as these should be
          inferred).
      """
      super().__init__(name=name)
  
      if vocab_size is None and existing_vocab is None:
          raise ValueError(
              "Must provide one of vocab_size or existing_vocab."
          )
  
      if existing_vocab is not None and (
          vocab_size or embed_dim or initializer
      ):
          raise ValueError(
              "If `existing_vocab` is provided, none of `vocab_size`, "
              "`embedding_dim`, `initializer` are needed."
          )
  
      if existing_vocab is None:
          if embed_dim is None:
              embed_dim = embedding_dim(vocab_size)
          if initializer is None:
              initializer = initializers.TruncatedNormal()
          vocab = initializer([vocab_size, embed_dim], dtype)
      else:
          existing_vocab = tf.convert_to_tensor(
              existing_vocab, dtype=dtype
          )
          vocab_size, embed_dim = existing_vocab.shape
          vocab = existing_vocab
  
      self.vocab_size = vocab_size
      self.embed_dim = embed_dim
      self.densify_gradients = densify_gradients
      self.embeddings = tf.Variable(
          vocab, trainable=trainable, name="embeddings"
      )
  
file_path: sonnet/sonnet/src/embed.py
imports: import math
  from typing import Optional
  from sonnet.src import base
  from sonnet.src import initializers
  from sonnet.src import types
  import tensorflow
label_desc: Use image node_id 1 to create a new Embed object from inherited base classes: base with example: obj = Embed(vocab_size, embed_dim, existing_vocab, densify_gradients, initializer, trainable, dtype, name)