code: def add_timing_signal_from_position(x, position, timescales):
      """
      Args:
        x: a Tensor with shape [batch, len, channels]
        position: [batch, len, nd]
        min_timescale: a float
        max_timescale: a float
      Returns:
        a Tensor the same shape as x.
      """
      channels = shape_list(x)[2]
      num_dims = shape_list(position)[2]
  
      num_timescales = channels // (num_dims * 2)
  
      for dim, timescale in zip(range(num_dims), timescales):
          min_timescale, max_timescale = timescale
          log_timescale_increment = math.log(
              float(max_timescale) / float(min_timescale)
          ) / (tf.cast(num_timescales, dtype=tf.float32) - 1)
          inv_timescales = min_timescale * tf.exp(
              tf.cast(tf.range(num_timescales), dtype=tf.float32)
              * log_timescale_increment
          )
          position_x = tf.expand_dims(
              tf.cast(position[:, :, dim], dtype=tf.float32), 2
          )  # batch, len, 1 # where 1 will be the chanels dim
          scaled_time = position_x * tf.expand_dims(
              tf.expand_dims(inv_timescales, 0), 0
          )  # batch , len, num_timescales
          signal = tf.concat(
              [tf.sin(scaled_time), tf.cos(scaled_time)], axis=2
          )  # batch channels//num_dims
          prepad = dim * 2 * num_timescales
          postpad = channels - (dim + 1) * 2 * num_timescales
          signal = tf.pad(
              tensor=signal,
              paddings=[[0, 0], [0, 0], [prepad, postpad]],
          )
          x = x + signal
      return x
  
imports: import math
  import tensorflow
  from finetune.util.shapes import shape_list
label_desc: Use image node_id 1 for calling a global function with example usage: add_timing_signal_from_position(x, position, timescales) and returns: x