code: def init_embedding(
      to_params, from_params, init_config, model_config, name
  ):
      """Initialize input embedding."""
      if name not in to_params:
          logging.info("No %s in target model", name)
      elif init_config.get("restore_input_embedding", True):
          input_kernel = to_params[name]["kernel"]
          restored_kernel = from_params["kernel"]
          restored_bias = from_params["bias"]
  
          if input_kernel.shape != restored_kernel.shape:
              kernel_init_method = (
                  model_config.temporal_encoding_config.kernel_init_method
              )
              if input_kernel.shape == restored_kernel.shape[1:]:
                  # Deflates a ViViT 3D embedder to work with 2D spectrogram inputs.
                  restored_kernel = np.mean(restored_kernel, axis=0)
              elif input_kernel.shape[1:] != restored_kernel.shape:
                  # Kernel dimensions are [t, c_in, c_out]
                  restored_kernel = np.reshape(
                      restored_kernel, input_kernel.shape
                  )
              elif input_kernel.shape[0] == 1:
                  # Kernel dimensions are [t, h, w, c_in, c_out]
                  restored_kernel = np.expand_dims(
                      restored_kernel, axis=0
                  )
              elif kernel_init_method == "average_frame_initializer":
                  # This corresponds to "filter inflation" in
                  # J Carreira and A Zisserman. Quo vadis, action recognition?
                  # A new model and the kinetics dataset. CVPR 2017"
                  logging.info(
                      "Initializing input kernel with filter inflation."
                  )
                  t = input_kernel.shape[0]
                  restored_kernel = np.expand_dims(
                      restored_kernel, axis=0
                  )
                  restored_kernel = (
                      np.tile(restored_kernel, [t, 1, 1, 1, 1]) / t
                  )
              elif (
                  kernel_init_method == "average_arp_frame_initializer"
              ):
                  # This corresponds to a combination of filter inflation and
                  # the approximate rank pooling described in
                  # H Bilen et al. Action Recognition with Dynamic Image Networks.
                  # PAMI 2017.
                  logging.info(
                      "Initialzing input kernel with ARP inflation"
                  )
                  t = input_kernel.shape[0]
                  restored_kernel = np.expand_dims(
                      restored_kernel, axis=0
                  )
                  restored_kernel = np.tile(
                      restored_kernel, [t, 1, 1, 1, 1]
                  )
  
                  def average_arp(length):
                      # Implements Equation 3 of Bilen et al. PAMI 2017
                      array = np.arange(1, length + 1)
  
                      harmonic = np.zeros((length + 1))
                      harmonic[1:] = np.cumsum(1.0 / array)
  
                      array = 2 * (length - array + 1) - (
                          length + 1
                      ) * (harmonic[-1] - harmonic[:-1])
                      return array
  
                  normalizer = average_arp(t) / t
                  normalizer = np.reshape(normalizer, [t, 1, 1, 1, 1])
                  restored_kernel = restored_kernel * normalizer
              elif kernel_init_method == "central_frame_initializer":
                  logging.info(
                      "Initializing input kernel to select centre frame."
                  )
                  central_time_index = input_kernel.shape[0] // 2
                  temp = np.zeros(input_kernel.shape)
                  temp[central_time_index] = restored_kernel.copy()
                  restored_kernel = temp
              else:
                  raise AssertionError(
                      "Unknown input kernel initialization {}".format(
                          kernel_init_method
                      )
                  )
  
          to_params[name]["kernel"] = restored_kernel
          to_params[name]["bias"] = restored_bias
      else:
          logging.info("Not restoring input embedding parameters")
  
file_path: scenic/scenic/projects/mbt/model_utils.py
imports: from typing import Any
  from absl import logging
  import flax
  import jax.numpy
  import ml_collections
  import numpy
  from scenic.common_lib import debug_utils
  from scenic.projects.vivit import model_utils
  import scipy
label_desc: Use image node_id 4 for calling a global function with example usage: init_embedding(to_params, from_params, init_config, model_config, name) and returns: array