docstr: The conditional mean embedding is estimated as the solution of a regularised regression problem.
  
  This private method function uses cross validation to select the regularisation parameter that
  minimises squared error on the out-of-fold instances. The error is a distance in the RKHS and is
  therefore an MMD-like quantity itself.
docstr_tok: ["The","conditional","mean","embedding","is","estimated","as","the","solution","of","a","regularised","regression","problem",".","This","private","method","function","uses","cross","validation","to","select","the","regularisation","parameter","that","minimises","squared","error","on","the","out-of-fold","instances",".","The","error","is","a","distance","in","the","RKHS","and","is","therefore","an","MMD-like","quantity","itself","."]
code: def _pick_lam(
      self,
      lams: tf.Tensor,
      K: tf.Tensor,
      L: tf.Tensor,
      n_folds: int = 5,
  ) -> tf.Tensor:
      """
      The conditional mean embedding is estimated as the solution of a regularised regression problem.
  
      This private method function uses cross validation to select the regularisation parameter that
      minimises squared error on the out-of-fold instances. The error is a distance in the RKHS and is
      therefore an MMD-like quantity itself.
      """
      n = len(L)
      fold_size = n // n_folds
      K, L = tf.cast(K, tf.float64), tf.cast(K, tf.float64)
      perm = tf.random.shuffle(range(n))
      K, L = tf.gather(tf.gather(K, perm), perm, axis=1), tf.gather(
          tf.gather(L, perm), perm, axis=1
      )
      losses = tf.zeros_like(lams, dtype=tf.float64)
      for fold in range(n_folds):
          inds_oof = np.arange(n)[
              (fold * fold_size) : ((fold + 1) * fold_size)
          ]
          inds_if = np.setdiff1d(np.arange(n), inds_oof)
          K_if = tf.gather(tf.gather(K, inds_if), inds_if, axis=1)
          L_if = tf.gather(tf.gather(L, inds_if), inds_if, axis=1)
          n_if = len(K_if)
          L_inv_lams = tf.stack(
              [
                  tf.linalg.inv(
                      L_if + n_if * lam * tf.eye(n_if, dtype=tf.float64)
                  )
                  for lam in lams
              ]
          )  # n_lam x n_if x n_if
          KW = tf.einsum("ij,ljk->lik", K_if, L_inv_lams)
          lW = tf.einsum(
              "ij,ljk->lik",
              tf.gather(tf.gather(L, inds_oof), inds_if, axis=1),
              L_inv_lams,
          )
          lWKW = tf.einsum("lij,ljk->lik", lW, KW)
          lWKWl = tf.einsum(
              "lkj,jk->lk",
              lWKW,
              tf.gather(tf.gather(L, inds_if), inds_oof, axis=1),
          )  # n_lam x n_oof
          lWk = tf.einsum(
              "lij,ji->li",
              lW,
              tf.gather(tf.gather(K, inds_if), inds_oof, axis=1),
          )  # n_lam x n_oof
          kxx = tf.ones_like(lWk) * tf.reduce_max(K)
          losses += tf.reduce_sum(lWKWl + kxx - 2 * lWk, axis=-1)
      return tf.cast(lams[tf.argmin(losses)], tf.float32)
  
file_path: alibi-detect/alibi_detect/cd/tensorflow/context_aware.py
imports: import logging
  import numpy
  import tensorflow
  import tensorflow_probability
  from typing import Callable, Dict, Optional, Tuple, Union, List
  from alibi_detect.cd.base import BaseContextMMDDrift
  from alibi_detect.utils.tensorflow.kernels import GaussianRBF
  from alibi_detect.utils.warnings import deprecated_alias
  from alibi_detect.utils.frameworks import Framework
  from alibi_detect.cd._domain_clf import _SVCDomainClf
  from tqdm import tqdm
label_desc: Use image node_id 4 for calling the ContextMMDDriftTF obj's underlying member method code with example usage: obj._pick_lam(lams, K, L, n_folds) and returns: tf