code: def expand_tensor(tensor, block_size):
      """Expands a 2D tensor by replicating the tensor values.
  
      This is equivalent to the kronecker product of the tensor and a matrix of
      ones of size block_size.
  
      Example:
  
      tensor = [[1,2]
                [3,4]]
      block_size = [2,2]
  
      result = [[1 1 2 2]
                [1 1 2 2]
                [3 3 4 4]
                [3 3 4 4]]
  
      Args:
        tensor: A 2D tensor that needs to be expanded.
        block_size: List of integers specifying the expansion factor.
  
      Returns:
        The expanded tensor
  
      Raises:
        ValueError: if tensor is not rank-2 or block_size is does not have 2
        elements.
      """
      if tensor.get_shape().ndims != 2:
          raise ValueError("Input tensor must be rank 2")
  
      if len(block_size) != 2:
          raise ValueError("block_size must have 2 elements")
  
      block_height, block_width = block_size
  
      def _tile_rows(tensor, multiple):
          """Create a new tensor by tiling the tensor along rows."""
          return tf.tile(tensor, [multiple, 1])
  
      def _generate_indices(num_rows, block_dim):
          indices = np.zeros(
              shape=[num_rows * block_dim, 1], dtype=np.int32
          )
          for k in range(block_dim):
              for r in range(num_rows):
                  indices[k * num_rows + r] = r * block_dim + k
          return indices
  
      def _replicate_rows(tensor, multiple):
          tensor_shape = tensor.shape.as_list()
          expanded_shape = [tensor_shape[0] * multiple, tensor_shape[1]]
          indices = tf.constant(
              _generate_indices(tensor_shape[0], multiple)
          )
          return tf.scatter_nd(
              indices, _tile_rows(tensor, multiple), expanded_shape
          )
  
      expanded_tensor = tensor
  
      # Expand rows by factor block_height.
      if block_height > 1:
          expanded_tensor = _replicate_rows(tensor, block_height)
  
      # Transpose and expand by factor block_width. Transpose the result.
      if block_width > 1:
          expanded_tensor = tf.transpose(
              _replicate_rows(
                  tf.transpose(expanded_tensor), block_width
              )
          )
  
      return expanded_tensor
  
file_path: model-optimization/tensorflow_model_optimization/python/core/sparsity/keras/pruning_utils.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import numpy
  import tensorflow
label_desc: Use image node_id 2 for calling a global function with example usage: expand_tensor(tensor, block_size) and returns: expanded_tensor, tf, indices, tf