code: def gather_with_default(params, indices, default, name=None, axis=0):
      """Gather slices with `indices=-1` mapped to `default`.
  
      This operation is similar to `tf.gather()`, except that any value of `-1`
      in `indices` will be mapped to `default`.  Example:
  
      >>> gather_with_default(['a', 'b', 'c', 'd'], [2, 0, -1, 2, -1], '_')
      <tf.Tensor: shape=(5,), dtype=string,
          numpy=array([b'c', b'a', b'_', b'c', b'_'], dtype=object)>
  
      Args:
        params: The `Tensor` from which to gather values.  Must be at least rank
          `axis + 1`.
        indices: The index `Tensor`.  Must have dtype `int32` or `int64`, and values
          must be in the range `[-1, params.shape[axis])`.
        default: The value to use when `indices` is `-1`.  `default.shape` must
          be equal to `params.shape[axis + 1:]`.
        name: A name for the operation (optional).
        axis: The axis in `params` to gather `indices` from.  Must be a scalar
          `int32` or `int64`.  Supports negative indices.
  
      Returns:
        A `Tensor` with the same type as `param`, and with shape
        `params.shape[:axis] + indices.shape + params.shape[axis + 1:]`.
      """
      # This implementation basically just concatenates the default value and
      # the params together, and then uses gather(default_plus_params, indices + 1)
      # to get the appropriate values.  Most of the complexity below has to do
      # with properly handling cases where axis != 0, in which case we need to tile
      # the default before concatenating it.
      with ops.name_scope(
          name, "GatherWithDefault", [params, indices, default, axis]
      ):
          # Convert inputs to tensors.
          indices = ops.convert_to_tensor(
              indices, name="indices", preferred_dtype=dtypes.int32
          )
          params = ops.convert_to_tensor(params, name="params")
          default = ops.convert_to_tensor(
              default, name="default", dtype=params.dtype
          )
  
          if axis == 0:
              tiled_default = array_ops_stack.stack([default])
  
          else:
              # Get ranks & shapes of inputs.
              params_rank = array_ops.rank(params)
              params_shape = array_ops.shape(params)
              default_shape = array_ops.shape(default)
              outer_params_shape = params_shape[:axis]
  
              # This will equal `axis` if axis>=0.
              outer_params_rank = array_ops.shape(outer_params_shape)[0]
  
              # Add dimensions (with size=1) to default, so its rank matches params.
              new_shape = array_ops.concat(
                  [
                      array_ops.ones(
                          [outer_params_rank + 1], dtypes.int32
                      ),
                      default_shape,
                  ],
                  axis=0,
              )
              reshaped_default = array_ops.reshape(default, new_shape)
  
              # Tile the default for any dimension dim<axis, so its size matches params.
              multiples = array_ops.concat(
                  [
                      outer_params_shape,
                      array_ops.ones(
                          params_rank - outer_params_rank, dtypes.int32
                      ),
                  ],
                  axis=0,
              )
              tiled_default = array_ops.tile(
                  reshaped_default, multiples
              )
  
          # Prepend the default value to params (on the chosen axis).  Thus, the
          # default value is at index 0, and all other values have their index
          # incremented by one.
          default_plus_params = array_ops.concat(
              [tiled_default, params], axis=axis
          )
          return array_ops.gather(
              default_plus_params, indices + 1, axis=axis
          )
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import functools
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import tensor
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import array_ops_stack
  from tensorflow.python.ops import check_ops
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops.ragged import ragged_functional_ops
  from tensorflow.python.ops.ragged import ragged_gather_ops
  from tensorflow.python.ops.ragged import ragged_math_ops
  from tensorflow.python.ops.ragged import ragged_tensor
  from tensorflow.python.ops.ragged import ragged_where_op
  from tensorflow.python.ops.ragged import segment_id_ops
label_desc: Use image node_id 1 for calling a global function with example usage: gather_with_default(params, indices, default, name, axis) and returns: array_ops