class_docstr: Compute 2D patch embeddings with cls token and positional encoding
class_docstr_tok: ["Compute","2D","patch","embeddings","with","cls","token","and","positional","encoding"]
docstr: 100 % borrowed from:
  https://github.com/huggingface/transformers/blob/main/src/transformers/models/vit/modeling_tf_vit.py
  
  This method allows to interpolate the pre-trained position encodings, to be able to use the model on higher
  resolution images.
  
  Source:
  https://github.com/facebookresearch/dino/blob/de9ee3df6cf39fac952ab558447af1fa1365362a/vision_transformer.py
docstr_tok: ["100","%","borrowed","from",":","https",":","\/\/github.com\/huggingface\/transformers\/blob\/main\/src\/transformers\/models\/vit\/modeling_tf_vit.py","This","method","allows","to","interpolate","the","pre-trained","position","encodings",",","to","be","able","to","use","the","model","on","higher","resolution","images",".","Source",":","https",":","\/\/github.com\/facebookresearch\/dino\/blob\/de9ee3df6cf39fac952ab558447af1fa1365362a\/vision_transformer.py"]
code: def interpolate_pos_encoding(
      self, embeddings: tf.Tensor, height: int, width: int
  ) -> tf.Tensor:
      """100 % borrowed from:
      https://github.com/huggingface/transformers/blob/main/src/transformers/models/vit/modeling_tf_vit.py
  
      This method allows to interpolate the pre-trained position encodings, to be able to use the model on higher
      resolution images.
  
      Source:
      https://github.com/facebookresearch/dino/blob/de9ee3df6cf39fac952ab558447af1fa1365362a/vision_transformer.py
      """
      seq_len, dim = embeddings.shape[1:]
      num_patches = seq_len - 1
  
      num_positions = self.positions.shape[1] - 1
  
      if num_patches == num_positions and height == width:
          return self.positions
      class_pos_embed = self.positions[:, :1]
      patch_pos_embed = self.positions[:, 1:]
      h0 = height // self.patch_size[0]
      w0 = width // self.patch_size[1]
      patch_pos_embed = tf.image.resize(
          images=tf.reshape(
              patch_pos_embed,
              shape=(
                  1,
                  int(math.sqrt(num_positions)),
                  int(math.sqrt(num_positions)),
                  dim,
              ),
          ),
          size=(h0, w0),
          method="bilinear",
      )
  
      shape = patch_pos_embed.shape
      assert (
          h0 == shape[-3]
      ), "height of interpolated patch embedding doesn't match"
      assert (
          w0 == shape[-2]
      ), "width of interpolated patch embedding doesn't match"
  
      patch_pos_embed = tf.reshape(
          tensor=patch_pos_embed, shape=(1, -1, dim)
      )
      return tf.concat(
          values=(class_pos_embed, patch_pos_embed), axis=1
      )
  
file_path: doctr/doctr/models/modules/vision_transformer/tensorflow.py
imports: import math
  from typing import Any, Tuple
  import tensorflow
  from tensorflow.keras import layers
  from doctr.utils.repr import NestedObject
label_desc: Use image node_id 2 for calling the PatchEmbedding obj's underlying member method code with example usage: obj.interpolate_pos_encoding(embeddings, height, width) and returns: tf, self