docstr: Class for Causally Adaptive Constraint Minimization (CACM) Algorithm.
      @article{Kaur2022ModelingTD,
       title={Modeling the Data-Generating Process is Necessary for Out-of-Distribution Generalization},
       author={Jivat Neet Kaur and Emre Kıcıman and Amit Sharma},
       journal={ArXiv},
       year={2022},
       volume={abs/2206.07837},
       url={https://arxiv.org/abs/2206.07837}
      }
  
  :param model: Networks used for training. `model` type expected is torch.nn.Sequential(featurizer, classifier) where featurizer and classifier are of type torch.nn.Module.
  :param optimizer: Optimization algorithm used for training. Currently supports "Adam" and "SGD".
  :param lr: learning rate for CACM
  :param weight_decay: Value of weight decay for optimizer
  :param betas: Adam configuration parameters (beta1, beta2), exponential decay rate for the first moment and second-moment estimates, respectively.
  :param momentum: Value of momentum for SGD optimzer
  :param kernel_type: Kernel type for MMD penalty. Currently, supports "gaussian" (RBF). If None, distance between mean and second-order statistics (covariances) is used.
  :param ci_test: Conditional independence metric used for regularization penalty. Currently, MMD is supported.
  :param attr_types: list of attribute types (based on relationship with label Y); should be ordered according to attribute order in loaded dataset.
      Currently, 'causal' (Causal), 'conf' (Confounded), 'ind' (Independent) and 'sel' (Selected) are supported.
      For single-shift datasets, use: ['causal'], ['ind']
      For multi-shift datasets, use: ['causal', 'ind']
  :param E_conditioned: Binary flag indicating whether E-conditioned regularization has to be applied
  :param E_eq_A: list indicating indices of attributes that coincide with environment (E) definition; default is empty.
  :param gamma: kernel bandwidth for MMD (due to implementation, the kernel bandwdith will actually be the reciprocal of gamma i.e., gamma=1e-6 implies kernel bandwidth=1e6. See `mmd_compute` in utils.py)
  :param lambda_causal: MMD penalty hyperparameter for Causal shift
  :param lambda_conf: MMD penalty hyperparameter for Confounded shift
  :param lambda_ind: MMD penalty hyperparameter for Independent shift
  :param lambda_sel: MMD penalty hyperparameter for Selected shift
  :returns: an instance of PredictionAlgorithm class
docstr_tok: ["Class","for","Causally","Adaptive","Constraint","Minimization","(","CACM",")","Algorithm",".","@","article","{","Kaur2022ModelingTD",",","title=","{","Modeling","the","Data-Generating","Process","is","Necessary","for","Out-of-Distribution","Generalization","}",",","author=","{","Jivat","Neet","Kaur","and","Emre","K\u0131c\u0131man","and","Amit","Sharma","}",",","journal=","{","ArXiv","}",",","year=","{","2022","}",",","volume=","{","abs\/2206.07837","}",",","url=","{","https",":","\/\/arxiv.org\/abs\/2206.07837","}","}",":","param","model",":","Networks","used","for","training",".","`","model","`","type","expected","is","torch.nn.Sequential","(","featurizer",",","classifier",")","where","featurizer","and","classifier","are","of","type","torch.nn.Module",".",":","param","optimizer",":","Optimization","algorithm","used","for","training",".","Currently","supports","``","Adam","''","and","``","SGD","''",".",":","param","lr",":","learning","rate","for","CACM",":","param","weight_decay",":","Value","of","weight","decay","for","optimizer",":","param","betas",":","Adam","configuration","parameters","(","beta1",",","beta2",")",",","exponential","decay","rate","for","the","first","moment","and","second-moment","estimates",",","respectively",".",":","param","momentum",":","Value","of","momentum","for","SGD","optimzer",":","param","kernel_type",":","Kernel","type","for","MMD","penalty",".","Currently",",","supports","``","gaussian","''","(","RBF",")",".","If","None",",","distance","between","mean","and","second-order","statistics","(","covariances",")","is","used",".",":","param","ci_test",":","Conditional","independence","metric","used","for","regularization","penalty",".","Currently",",","MMD","is","supported",".",":","param","attr_types",":","list","of","attribute","types","(","based","on","relationship","with","label","Y",")",";","should","be","ordered","according","to","attribute","order","in","loaded","dataset",".","Currently",",","'causal","'","(","Causal",")",",","'conf","'","(","Confounded",")",",","'ind","'","(","Independent",")","and","'sel","'","(","Selected",")","are","supported",".","For","single-shift","datasets",",","use",":","[","'causal","'","]",",","[","'ind","'","]","For","multi-shift","datasets",",","use",":","[","'causal","'",",","'ind","'","]",":","param","E_conditioned",":","Binary","flag","indicating","whether","E-conditioned","regularization","has","to","be","applied",":","param","E_eq_A",":","list","indicating","indices","of","attributes","that","coincide","with","environment","(","E",")","definition",";","default","is","empty",".",":","param","gamma",":","kernel","bandwidth","for","MMD","(","due","to","implementation",",","the","kernel","bandwdith","will","actually","be","the","reciprocal","of","gamma","i.e.",",","gamma=1e-6","implies","kernel","bandwidth=1e6",".","See","`","mmd_compute","`","in","utils.py",")",":","param","lambda_causal",":","MMD","penalty","hyperparameter","for","Causal","shift",":","param","lambda_conf",":","MMD","penalty","hyperparameter","for","Confounded","shift",":","param","lambda_ind",":","MMD","penalty","hyperparameter","for","Independent","shift",":","param","lambda_sel",":","MMD","penalty","hyperparameter","for","Selected","shift",":","returns",":","an","instance","of","PredictionAlgorithm","class"]
code: def __init__(
      self,
      model,
      optimizer="Adam",
      lr=1e-3,
      weight_decay=0.0,
      betas=(0.9, 0.999),
      momentum=0.9,
      kernel_type="gaussian",
      ci_test="mmd",
      attr_types=[],
      E_conditioned=True,
      E_eq_A=[],
      gamma=1e-6,
      lambda_causal=1.0,
      lambda_conf=1.0,
      lambda_ind=1.0,
      lambda_sel=1.0,
  ):
      """Class for Causally Adaptive Constraint Minimization (CACM) Algorithm.
          @article{Kaur2022ModelingTD,
           title={Modeling the Data-Generating Process is Necessary for Out-of-Distribution Generalization},
           author={Jivat Neet Kaur and Emre Kıcıman and Amit Sharma},
           journal={ArXiv},
           year={2022},
           volume={abs/2206.07837},
           url={https://arxiv.org/abs/2206.07837}
          }
  
      :param model: Networks used for training. `model` type expected is torch.nn.Sequential(featurizer, classifier) where featurizer and classifier are of type torch.nn.Module.
      :param optimizer: Optimization algorithm used for training. Currently supports "Adam" and "SGD".
      :param lr: learning rate for CACM
      :param weight_decay: Value of weight decay for optimizer
      :param betas: Adam configuration parameters (beta1, beta2), exponential decay rate for the first moment and second-moment estimates, respectively.
      :param momentum: Value of momentum for SGD optimzer
      :param kernel_type: Kernel type for MMD penalty. Currently, supports "gaussian" (RBF). If None, distance between mean and second-order statistics (covariances) is used.
      :param ci_test: Conditional independence metric used for regularization penalty. Currently, MMD is supported.
      :param attr_types: list of attribute types (based on relationship with label Y); should be ordered according to attribute order in loaded dataset.
          Currently, 'causal' (Causal), 'conf' (Confounded), 'ind' (Independent) and 'sel' (Selected) are supported.
          For single-shift datasets, use: ['causal'], ['ind']
          For multi-shift datasets, use: ['causal', 'ind']
      :param E_conditioned: Binary flag indicating whether E-conditioned regularization has to be applied
      :param E_eq_A: list indicating indices of attributes that coincide with environment (E) definition; default is empty.
      :param gamma: kernel bandwidth for MMD (due to implementation, the kernel bandwdith will actually be the reciprocal of gamma i.e., gamma=1e-6 implies kernel bandwidth=1e6. See `mmd_compute` in utils.py)
      :param lambda_causal: MMD penalty hyperparameter for Causal shift
      :param lambda_conf: MMD penalty hyperparameter for Confounded shift
      :param lambda_ind: MMD penalty hyperparameter for Independent shift
      :param lambda_sel: MMD penalty hyperparameter for Selected shift
      :returns: an instance of PredictionAlgorithm class
  
      """
  
      super().__init__(
          model, optimizer, lr, weight_decay, betas, momentum
      )
  
      self.CACMRegularizer = Regularizer(
          E_conditioned, ci_test, kernel_type, gamma
      )
  
      self.attr_types = attr_types
      self.E_eq_A = E_eq_A
      self.lambda_causal = lambda_causal
      self.lambda_conf = lambda_conf
      self.lambda_ind = lambda_ind
      self.lambda_sel = lambda_sel
  
file_path: dowhy/dowhy/causal_prediction/algorithms/cacm.py
imports: import torch
  from torch.nn import functional
  from dowhy.causal_prediction.algorithms.base_algorithm import PredictionAlgorithm
  from dowhy.causal_prediction.algorithms.regularization import Regularizer
label_desc: Use image node_id 1 to create a new CACM object from inherited base classes: PredictionAlgorithm with example: obj = CACM(model, optimizer, lr, weight_decay, betas, momentum, kernel_type, ci_test, attr_types, E_conditioned, E_eq_A, gamma, lambda_causal, lambda_conf, lambda_ind, lambda_sel)