class_docstr: Construct the patch and position embeddings.
class_docstr_tok: ["Construct","the","patch","and","position","embeddings","."]
code: def forward(self, pixel_values):
      batch_size = pixel_values.shape[0]
  
      # create patch embeddings
      embeddings, num_frames, patch_width = self.patch_embeddings(
          pixel_values
      )
  
      cls_tokens = self.cls_token.expand(embeddings.size(0), -1, -1)
      embeddings = torch.cat((cls_tokens, embeddings), dim=1)
  
      # resizing the positional embeddings in case they don't match the input at inference
      if embeddings.size(1) != self.position_embeddings.size(1):
          position_embeddings = self.position_embeddings
          cls_pos_embed = (
              position_embeddings[0, 0, :].unsqueeze(0).unsqueeze(1)
          )
          other_pos_embed = (
              position_embeddings[0, 1:, :].unsqueeze(0).transpose(1, 2)
          )
          patch_num = int(other_pos_embed.size(2) ** 0.5)
          patch_height = embeddings.size(1) // patch_width
          other_pos_embed = other_pos_embed.reshape(
              1, embeddings.size(2), patch_num, patch_num
          )
          new_pos_embed = nn.functional.interpolate(
              other_pos_embed,
              size=(patch_height, patch_width),
              mode="nearest",
          )
          new_pos_embed = new_pos_embed.flatten(2)
          new_pos_embed = new_pos_embed.transpose(1, 2)
          new_pos_embed = torch.cat((cls_pos_embed, new_pos_embed), 1)
          embeddings = embeddings + new_pos_embed
      else:
          embeddings = embeddings + self.position_embeddings
      embeddings = self.pos_drop(embeddings)
  
      # Time Embeddings
      if self.attention_type != "space_only":
          cls_tokens = embeddings[:batch_size, 0, :].unsqueeze(1)
          embeddings = embeddings[:, 1:]
          _, patch_height, patch_width = embeddings.shape
          embeddings = (
              embeddings.reshape(
                  batch_size, num_frames, patch_height, patch_width
              )
              .permute(0, 2, 1, 3)
              .reshape(
                  batch_size * patch_height, num_frames, patch_width
              )
          )
          # Resizing time embeddings in case they don't match
          if num_frames != self.time_embeddings.size(1):
              time_embeddings = self.time_embeddings.transpose(1, 2)
              new_time_embeddings = nn.functional.interpolate(
                  time_embeddings, size=(num_frames), mode="nearest"
              )
              new_time_embeddings = new_time_embeddings.transpose(1, 2)
              embeddings = embeddings + new_time_embeddings
          else:
              embeddings = embeddings + self.time_embeddings
          embeddings = self.time_drop(embeddings)
          embeddings = embeddings.view(
              batch_size, patch_height, num_frames, patch_width
          ).reshape(batch_size, patch_height * num_frames, patch_width)
          embeddings = torch.cat((cls_tokens, embeddings), dim=1)
  
      return embeddings
  
imports: import collections
  from typing import Optional, Tuple, Union
  import torch
  import torch.nn.functional
  import torch.utils.checkpoint
  from torch import nn
  from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
  from ...activations import ACT2FN
  from ...modeling_outputs import BaseModelOutput, ImageClassifierOutput
  from ...modeling_utils import PreTrainedModel
  from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings
  from .configuration_timesformer import TimesformerConfig
label_desc: Use image node_id 2 for calling the TimesformerEmbeddings obj's underlying member method code with example usage: obj.forward(pixel_values) and returns: embeddings