code: def generate_counterfactuals(
      self, query_instance, total_CFs, desired_class="opposite"
  ):
      # Loading the latest trained CFVAE model
      self.cf_vae.load_state_dict(torch.load(self.save_path))
      self.cf_vae.eval()
  
      query_instance = (
          self.data_interface.get_ohe_min_max_normalized_data(
              query_instance
          )
      )
      query_instance = np.array([query_instance.iloc[0].values])
  
      if query_instance.shape[0] > self.batch_size:
          test_dataset = np.array_split(
              query_instance,
              query_instance.shape[0] // self.batch_size,
              axis=0,
          )
      else:
          test_dataset = [query_instance]
      final_gen_cf = []
      final_cf_pred = []
      final_test_pred = []
      for i in range(len(query_instance)):
          train_x = test_dataset[i]
          train_x = torch.tensor(train_x).float()
          train_y = torch.argmax(self.pred_model(train_x), dim=1)
  
          curr_gen_cf = []
          curr_cf_pred = []
          curr_test_pred = train_y.numpy()
  
          for _ in range(total_CFs):
              (
                  recon_err,
                  kl_err,
                  x_true,
                  x_pred,
                  cf_label,
              ) = self.cf_vae.compute_elbo(
                  train_x, 1.0 - train_y, self.pred_model
              )
              while cf_label == train_y:
                  print(cf_label, train_y)
                  (
                      recon_err,
                      kl_err,
                      x_true,
                      x_pred,
                      cf_label,
                  ) = self.cf_vae.compute_elbo(
                      train_x, 1.0 - train_y, self.pred_model
                  )
  
              x_pred = x_pred.detach().numpy()
              # Converting mixed scores into one hot feature representations
              for v in self.cf_vae.encoded_categorical_feature_indexes:
                  curr_max = x_pred[:, v[0]]
                  curr_max_idx = v[0]
                  for idx in v:
                      if curr_max < x_pred[:, idx]:
                          curr_max = x_pred[:, idx]
                          curr_max_idx = idx
                  for idx in v:
                      if idx == curr_max_idx:
                          x_pred[:, idx] = 1
                      else:
                          x_pred[:, idx] = 0
  
              cf_label = cf_label.detach().numpy()
              cf_label = np.reshape(cf_label, (cf_label.shape[0], 1))
  
              curr_gen_cf.append(x_pred)
              curr_cf_pred.append(cf_label)
  
          final_gen_cf.append(curr_gen_cf)
          final_cf_pred.append(curr_cf_pred)
          final_test_pred.append(curr_test_pred)
  
      # CF Gen out
      result = {}
      result["query-instance"] = query_instance
      result["test-pred"] = final_test_pred[0][0]
      result["CF"] = final_gen_cf[0]
      result["CF-Pred"] = final_cf_pred[0]
  
      # do inverse transform of CFs to original user-fed format
      cfs = np.array([cfval[0] for cfval in result["CF"]])
      final_cfs_df = (
          self.data_interface.get_inverse_ohe_min_max_normalized_data(
              cfs
          )
      )
      cfs_preds = [
          np.round(preds.flatten().tolist(), 3)
          for preds in result["CF-Pred"]
      ]
      cfs_preds = [item for sublist in cfs_preds for item in sublist]
      final_cfs_df[self.data_interface.outcome_name] = np.array(
          cfs_preds
      )
      test_instance_df = (
          self.data_interface.get_inverse_ohe_min_max_normalized_data(
              result["query-instance"]
          )
      )
      test_instance_df[self.data_interface.outcome_name] = np.array(
          np.round(result["test-pred"], 3)
      )
  
      # Adding empty list for sparse cf gen and pred; adding 'NA' for the posthoc sparsity cofficient
      return exp.CounterfactualExamples(
          data_interface=self.data_interface,
          final_cfs_df=final_cfs_df,
          test_instance_df=test_instance_df,
          final_cfs_df_sparse=None,
          posthoc_sparsity_param=None,
          desired_class=desired_class,
      )
  
file_path: DiCE/dice_ml/explainer_interfaces/feasible_base_vae.py
imports: import numpy
  import torch
  import torch.utils.data
  from torch.nn import functional
  from dice_ml import diverse_counterfactuals
  from dice_ml.explainer_interfaces.explainer_base import ExplainerBase
  from dice_ml.utils.helpers import get_base_gen_cf_initialization
label_desc: Use image node_id 4 for calling the FeasibleBaseVAE obj's underlying member method code with example usage: obj.generate_counterfactuals(query_instance, total_CFs, desired_class) and returns: exp