class_docstr: Transform a batch of views into a sequence of tokens.
docstr: Add positional encodings to the input patch sequence.
code: def add_positional_encodings(
      self, x: jnp.ndarray, positional_embedding: str = ""
  ) -> jnp.ndarray:
      """Add positional encodings to the input patch sequence."""
      _, h, w, c = x.shape
      positional_embedding = (
          positional_embedding or self.positional_embedding
      )
      if positional_embedding == "learned":
          posemb = self.param(
              "posembed_input",
              nn.initializers.normal(stddev=1 / np.sqrt(c)),
              (1, self.posembs[0], self.posembs[1], c),
              x.dtype,
          )
          # Optionally resize the positional encodings.
          if (h, w) != self.posembs:
              posemb = jax.image.resize(
                  posemb, (1, h, w, c), "bilinear"
              )
          x = x + posemb
      elif positional_embedding == "sinusoidal_2d":
          x = attention_layers.AddFixedSinCosPositionEmbedding()(x)
      x = jnp.reshape(x, (-1, h * w, c))
      return x
  
imports: import copy
  import functools
  from typing import Any, Optional, Tuple
  import flax.linen
  import jax
  import jax.numpy
  import ml_collections
  import numpy
  from scenic.model_lib.base_models import base_model
  from scenic.model_lib.base_models import classification_model
  from scenic.model_lib.base_models import model_utils
  from scenic.model_lib.layers import attention_layers
  from scenic.model_lib.layers import nn_layers
  from scenic.projects.baselines import vit
label_desc: Use image node_id 1 for calling the ToTokenSequence obj's underlying member method code with example usage: obj.add_positional_encodings(x, positional_embedding) and returns: x