code: def _select_target(
      preds: tf.Tensor,
      targets: Union[None, tf.Tensor, np.ndarray, list],
  ) -> tf.Tensor:
      """
      Select the predictions corresponding to the targets if targets is not ``None``.
  
      Parameters
      ----------
      preds
          Predictions before selection.
      targets
          Targets to select.
  
      Returns
      -------
      Selected predictions.
  
      """
      if not isinstance(targets, tf.Tensor):
          targets = tf.convert_to_tensor(targets)
  
      if targets is not None:
          if isinstance(preds, tf.Tensor):
              preds = tf.gather_nd(
                  preds, tf.expand_dims(targets, axis=1), batch_dims=1
              )
          else:
              raise NotImplementedError
      else:
          raise ValueError(
              "target cannot be `None` if `model` output dimensions > 1"
          )
  
      return preds
  
imports: import copy
  import logging
  import string
  import warnings
  from enum import Enum
  from typing import Callable, List, Optional, Tuple, Union, cast
  import numpy
  import tensorflow
  from alibi.api.defaults import DEFAULT_DATA_INTGRAD, DEFAULT_META_INTGRAD
  from alibi.api.interfaces import Explainer, Explanation
  from alibi.utils.approximation_methods import approximation_parameters
label_desc: Use image node_id 2 for calling a global function with example usage: _select_target(preds, targets) and returns: preds