code: def top_k_categorical_accuracy(y_true, y_pred, k=5):
      reshape_matches = False
      y_pred = ops.convert_to_tensor(y_pred)
      y_true = ops.convert_to_tensor(y_true, dtype=y_true.dtype)
      y_true = ops.argmax(y_true, axis=-1)
      y_true_rank = len(y_true.shape)
      y_pred_rank = len(y_pred.shape)
      y_true_org_shape = ops.shape(y_true)
  
      # Flatten y_pred to (batch_size, num_samples) and y_true to (num_samples,)
      if (y_true_rank is not None) and (y_pred_rank is not None):
          if y_pred_rank > 2:
              y_pred = ops.reshape(y_pred, [-1, y_pred.shape[-1]])
          if y_true_rank > 1:
              reshape_matches = True
              y_true = ops.reshape(y_true, [-1])
  
      matches = ops.cast(
          ops.in_top_k(ops.cast(y_true, "int32"), y_pred, k=k),
          dtype=backend.floatx(),
      )
  
      # returned matches is expected to have same shape as y_true input
      if reshape_matches:
          matches = ops.reshape(matches, new_shape=y_true_org_shape)
  
      return matches
  
imports: from keras import backend
  from keras import ops
  from keras.api_export import keras_export
  from keras.losses.loss import squeeze_to_same_rank
  from keras.metrics import reduction_metrics
label_desc: Use image node_id 5 for calling a global function with example usage: top_k_categorical_accuracy(y_true, y_pred, k) and returns: matches