code: def test_sparsity_loss(dataset, backend):
      SIZE = 100
      feature_names = dataset["metadata"]["feature_names"]
      category_map = dataset["metadata"].get("category_map", {})
  
      # define number of numerical
      num_features = len(feature_names) - len(category_map)
  
      # define random autoencoder reconstruction and ohe
      X_hat_split = []
      X_ohe_split = []
  
      # generate numerical feature reconstruction
      X_num = np.random.randn(SIZE, num_features).astype(np.float32)
      X_hat_split.append(X_num)
      X_ohe_split.append(X_num)
  
      # for each categorical feature generate random reconstruction
      for cat_col in category_map:
          X_cat = np.random.rand(
              SIZE, len(category_map[cat_col])
          ).astype(np.float32)
          X_hat_split.append(X_cat)
  
          X_ohe_cat = np.zeros_like(X_cat)
          X_ohe_cat[np.arange(SIZE), np.argmax(X_cat, axis=1)] = 1
          X_ohe_split.append(X_ohe_cat)
  
      if backend == "tensorflow":
          from alibi.explainers.backends.tensorflow.cfrl_tabular import (
              sparsity_loss,
          )
          from alibi.explainers.backends.tensorflow.cfrl_base import (
              to_tensor,
              to_numpy,
          )
  
          device = None
      else:
          import torch
          from alibi.explainers.backends.pytorch.cfrl_tabular import (
              sparsity_loss,
          )
          from alibi.explainers.backends.pytorch.cfrl_base import (
              to_tensor,
              to_numpy,
          )
  
          device = torch.device("cpu")
  
      for i in range(len(X_hat_split)):
          X_hat_split[i] = to_tensor(X_hat_split[i], device=device)
  
      X_ohe = np.concatenate(X_ohe_split, axis=1)
      X_ohe = to_tensor(X_ohe, device=device)
  
      losses = sparsity_loss(X_hat_split, X_ohe, category_map)
      losses = {key: to_numpy(val) for key, val in losses.items()}
      assert np.isclose(losses["sparsity_num_loss"], 0, atol=1e-5)
      assert np.isclose(losses["sparsity_cat_loss"], 0, atol=1e-5)
  
imports: from typing import Union, List
  import pytest
  from pytest_lazyfixture import lazy_fixture
  import numpy
  from numpy.testing import assert_allclose
  import tensorflow
  import tensorflow.keras
  from alibi.explainers import CounterfactualRLTabular
  from alibi.explainers.backends.cfrl_base import get_hard_distribution
  from alibi.explainers.backends.cfrl_tabular import get_he_preprocessor, split_ohe, get_numerical_conditional_vector, get_categorical_conditional_vector, get_statistics, get_conditional_vector, sample
label_desc: Use image node_id 13 for calling a global function with example usage: test_sparsity_loss(dataset, backend) without return types