class_docstr: Counterfactual Reinforcement Learning. 
class_docstr_tok: ["Counterfactual","Reinforcement","Learning","."]
docstr: Compute counterfactual instance for a given input, target and condition vector.
  
  Parameters
  ----------
  X
      Instances to be explained.
  Y_t
      Counterfactual targets.
  C
      Conditional vector. If ``None``, it means that no conditioning was used during training (i.e. the
      `conditional_func` returns ``None``).
  
  Returns
  -------
  Dictionary containing the input instances in the original format, input classification labels,
  counterfactual instances in the original format, counterfactual classification labels, target labels,
  conditional vectors.
code: def _compute_counterfactual(
      self,
      X: np.ndarray,
      Y_t: np.ndarray,
      C: Optional[np.ndarray] = None,
  ) -> Dict[str, Optional[np.ndarray]]:  # TODO: TypedDict
      """
      Compute counterfactual instance for a given input, target and condition vector.
  
      Parameters
      ----------
      X
          Instances to be explained.
      Y_t
          Counterfactual targets.
      C
          Conditional vector. If ``None``, it means that no conditioning was used during training (i.e. the
          `conditional_func` returns ``None``).
  
      Returns
      -------
      Dictionary containing the input instances in the original format, input classification labels,
      counterfactual instances in the original format, counterfactual classification labels, target labels,
      conditional vectors.
      """
      # Save original input for later usage.
      X_orig = X
  
      # Compute models prediction.
      Y_m = self.params["predictor"](X_orig)
  
      # Check if the prediction task is classification. Please refer to
      # `alibi.explainers.backends.cfrl_base.CounterfactualRLDataset` for a justification.
      if self._is_classification(pred=Y_m):
          Y_m = get_hard_distribution(Y=Y_m, num_classes=Y_m.shape[1])
          Y_t = get_hard_distribution(Y=Y_t, num_classes=Y_m.shape[1])
      else:
          Y_m = Y_m.reshape(-1, 1)
          Y_t = Y_t.reshape(-1, 1)
  
      # Apply autoencoder preprocessing step.
      X = self.params["encoder_preprocessor"](X_orig)
  
      # Convert to tensors.
      X = self.backend.to_tensor(X, **self.params)
      Y_m = self.backend.to_tensor(Y_m, **self.params)
      Y_t = self.backend.to_tensor(Y_t, **self.params)
      C = self.backend.to_tensor(C, **self.params)
  
      # Encode instance.
      Z = self.backend.encode(X, **self.params)
  
      # Generate counterfactual embedding.
      Z_cf = self.backend.generate_cf(Z, Y_m, Y_t, C, **self.params)
  
      # Decode counterfactual.
      X_cf = self.backend.decode(Z_cf, **self.params)
  
      # Convert to numpy for postprocessing
      X_cf = self.backend.to_numpy(X_cf)
      X = self.backend.to_numpy(X)
      C = self.backend.to_numpy(C)
  
      # Apply postprocessing functions.
      for pp_func in self.params["postprocessing_funcs"]:
          X_cf = pp_func(X_cf, X, C)
  
      # Apply inverse autoencoder pre-processor.
      X_cf = self.params["decoder_inv_preprocessor"](X_cf)
  
      # Classify counterfactual instances.
      Y_m_cf = self.params["predictor"](X_cf)
  
      # Convert tensors to numpy.
      Y_m = self.backend.to_numpy(Y_m)
      Y_t = self.backend.to_numpy(Y_t)
  
      # If the prediction is a classification task.
      if self._is_classification(pred=Y_m):
          Y_m = np.argmax(Y_m, axis=1)
          Y_t = np.argmax(Y_t, axis=1)
          Y_m_cf = np.argmax(Y_m_cf, axis=1)
  
      return {
          "X": X_orig,  # input instances
          "Y_m": Y_m,  # input classification labels
          "X_cf": X_cf,  # counterfactual instances
          "Y_m_cf": Y_m_cf,  # counterfactual classification labels
          "Y_t": Y_t,  # target labels
          "C": C,  # conditional vectors
      }
  
imports: import logging
  import os
  from abc import ABC, abstractmethod
  from copy import deepcopy
  from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Tuple, Union, cast
  import numpy
  from tqdm import tqdm
  from alibi.api.defaults import DEFAULT_DATA_CFRL, DEFAULT_META_CFRL
  from alibi.api.interfaces import Explainer, Explanation, FitMixin
  from alibi.explainers.backends.cfrl_base import generate_empty_condition, get_classification_reward, get_hard_distribution, identity_function
  from alibi.utils.frameworks import Framework, has_pytorch, has_tensorflow
label_desc: Use image node_id 15 for calling the CounterfactualRL obj's underlying member method code with example usage: obj._compute_counterfactual(X, Y_t, C) and returns: dict