docstr: The basic trainer for self-supervised learning with contrastive method.
  Used to automatically train the self-supervised problems.
  Parameters
  ----------
  encoder: `BaseEncoderMaintainer`, `str` or None
      A graph encoder shared by all views.
  decoder: `BaseDecoderMaintainer`, `str` or None
      A decoder which can be understood as a projection head for graph-level representations.
      Only required if `graph_level` = True.
  decoder_node: `BaseDecoderMaintainer`, `str` or None
      A decoder which can be understood as a projection head for node-level representations.
      Only required if `node_level` = True.
  num_features: `int` or None, Optional
      The number of features in dataset.
  num_graph_features: `int` or None, Optional
      The number of graph level features in dataset.
  device: `torch.device` or `str`, Optional
      The device this trainer will use.
      When `device` = "auto", if GPU exists in the device and dependency is installed, 
      the trainer will give priority to GPU, otherwise CPU will be used
  feval: a sequence of `str` or a sequence of `Evaluation`, Optional
      The evaluation methods.
  loss: `str` or `Callable`, Optional
      The loss function or the learning objective of contrastive model.
  views_fn: a list of `Callable` or None, Optional
      List of functions or augmentation methods to generate views from give graphs.
  graph_level: `bool`, Optional
      Whether to include graph-level representations
  node_level: `bool`, Optional
      Whether to include node-level representations
  z_dim: `int`, Optional
      The dimension of graph-level representations
  z_node_dim: `int`, Optional
      The dimension of node-level representations
  tau: `int`, Optional
      The temperature parameter in NT_Xent loss. Only used when `loss` = "NT_Xent"
  model_path: `str` or None, Optional
      The directory to restore the saved model.
      If `model_path` = None, the model will not be saved.
docstr_tok: ["The","basic","trainer","for","self-supervised","learning","with","contrastive","method",".","Used","to","automatically","train","the","self-supervised","problems",".","Parameters","--","--","--","--","--","encoder",":","`","BaseEncoderMaintainer","`",",","`","str","`","or","None","A","graph","encoder","shared","by","all","views",".","decoder",":","`","BaseDecoderMaintainer","`",",","`","str","`","or","None","A","decoder","which","can","be","understood","as","a","projection","head","for","graph-level","representations",".","Only","required","if","`","graph_level","`","=","True",".","decoder_node",":","`","BaseDecoderMaintainer","`",",","`","str","`","or","None","A","decoder","which","can","be","understood","as","a","projection","head","for","node-level","representations",".","Only","required","if","`","node_level","`","=","True",".","num_features",":","`","int","`","or","None",",","Optional","The","number","of","features","in","dataset",".","num_graph_features",":","`","int","`","or","None",",","Optional","The","number","of","graph","level","features","in","dataset",".","device",":","`","torch.device","`","or","`","str","`",",","Optional","The","device","this","trainer","will","use",".","When","`","device","`","=","``","auto","''",",","if","GPU","exists","in","the","device","and","dependency","is","installed",",","the","trainer","will","give","priority","to","GPU",",","otherwise","CPU","will","be","used","feval",":","a","sequence","of","`","str","`","or","a","sequence","of","`","Evaluation","`",",","Optional","The","evaluation","methods",".","loss",":","`","str","`","or","`","Callable","`",",","Optional","The","loss","function","or","the","learning","objective","of","contrastive","model",".","views_fn",":","a","list","of","`","Callable","`","or","None",",","Optional","List","of","functions","or","augmentation","methods","to","generate","views","from","give","graphs",".","graph_level",":","`","bool","`",",","Optional","Whether","to","include","graph-level","representations","node_level",":","`","bool","`",",","Optional","Whether","to","include","node-level","representations","z_dim",":","`","int","`",",","Optional","The","dimension","of","graph-level","representations","z_node_dim",":","`","int","`",",","Optional","The","dimension","of","node-level","representations","tau",":","`","int","`",",","Optional","The","temperature","parameter","in","NT_Xent","loss",".","Only","used","when","`","loss","`","=","``","NT_Xent","''","model_path",":","`","str","`","or","None",",","Optional","The","directory","to","restore","the","saved","model",".","If","`","model_path","`","=","None",",","the","model","will","not","be","saved","."]
code: def __init__(
      self,
      encoder: _typing.Union[BaseEncoderMaintainer, str, None],
      decoder: _typing.Union[BaseDecoderMaintainer, str, None],
      decoder_node: _typing.Union[BaseDecoderMaintainer, None],
      num_features: _typing.Union[int, None] = None,
      num_graph_features: _typing.Union[int, None] = None,
      device: _typing.Union[torch.device, str] = "auto",
      feval: _typing.Union[
          _typing.Sequence[str],
          _typing.Sequence[_typing.Type[Evaluation]],
      ] = (Acc,),
      loss: Union[str, Callable] = "NT_Xent",
      f_loss: Union[str, Callable] = "nll_loss",
      views_fn: _typing.Union[
          _typing.Sequence[_typing.Callable], None
      ] = None,
      aug_ratio: Union[float, Sequence[float]] = 0.2,
      graph_level: bool = True,
      node_level: bool = False,
      z_dim: _typing.Union[int, None] = None,
      z_node_dim: _typing.Union[int, None] = None,
      tau: int = 0.5,
      p_optim: Union[torch.optim.Optimizer, str] = "Adam",
      p_lr: float = 0.0001,
      p_lr_scheduler_type: str = None,
      p_weight_decay: int = 0,
      p_epoch: int = 100,
      p_early_stopping_round: int = 20,
      f_optim: Union[torch.optim.Optimizer, str] = "Adam",
      f_lr: float = 0.001,
      f_lr_scheduler_type: str = None,
      f_weight_decay: int = 0,
      f_epoch: int = 100,
      f_early_stopping_round: int = 20,
      model_path: _typing.Union[str, None] = "./models",
  ):
      """
      The basic trainer for self-supervised learning with contrastive method.
      Used to automatically train the self-supervised problems.
      Parameters
      ----------
      encoder: `BaseEncoderMaintainer`, `str` or None
          A graph encoder shared by all views.
      decoder: `BaseDecoderMaintainer`, `str` or None
          A decoder which can be understood as a projection head for graph-level representations.
          Only required if `graph_level` = True.
      decoder_node: `BaseDecoderMaintainer`, `str` or None
          A decoder which can be understood as a projection head for node-level representations.
          Only required if `node_level` = True.
      num_features: `int` or None, Optional
          The number of features in dataset.
      num_graph_features: `int` or None, Optional
          The number of graph level features in dataset.
      device: `torch.device` or `str`, Optional
          The device this trainer will use.
          When `device` = "auto", if GPU exists in the device and dependency is installed,
          the trainer will give priority to GPU, otherwise CPU will be used
      feval: a sequence of `str` or a sequence of `Evaluation`, Optional
          The evaluation methods.
      loss: `str` or `Callable`, Optional
          The loss function or the learning objective of contrastive model.
      views_fn: a list of `Callable` or None, Optional
          List of functions or augmentation methods to generate views from give graphs.
      graph_level: `bool`, Optional
          Whether to include graph-level representations
      node_level: `bool`, Optional
          Whether to include node-level representations
      z_dim: `int`, Optional
          The dimension of graph-level representations
      z_node_dim: `int`, Optional
          The dimension of node-level representations
      tau: `int`, Optional
          The temperature parameter in NT_Xent loss. Only used when `loss` = "NT_Xent"
      model_path: `str` or None, Optional
          The directory to restore the saved model.
          If `model_path` = None, the model will not be saved.
      """
      assert (node_level or graph_level) is True
      assert (
          isinstance(encoder, BaseEncoderMaintainer)
          or isinstance(encoder, str)
          or encoder is None
      )
      self.loss = self._get_loss(loss)
      self.node_level = node_level
      self.graph_level = graph_level
      self.z_dim = z_dim
      self.z_node_dim = z_node_dim
      self._encoder = None
      # parameters that record the results
      self.valid_result = None
      self.valid_result_prob = None
      self.valid_score = None
      # TODO
      # do not support method with both node-level representation and graph-level representation
      # so the decoder will be either _decoder or _decoder_node, one of them
      self._decoder = None
      self.views_fn_opt = views_fn
      self._views_fn = None
      self._aug_ratio = aug_ratio
      self.last_dim = z_dim if graph_level else z_node_dim
      self.num_features = num_features
      self.num_graph_features = num_graph_features
      self.tau = tau
      self.model_path = model_path
      if isinstance(device, str):
          if device == "auto":
              self.device = torch.device(
                  "cuda" if torch.cuda.is_available() else "cpu"
              )
          else:
              self.device = torch.device(device)
      elif isinstance(device, torch.device):
          self.device = device
      else:
          self.device = torch.device(
              "cuda" if torch.cuda.is_available() else "cpu"
          )
      self.p_opt_received = p_optim
      self.p_optimizer = self._get_optimizer(p_optim)
      self.p_lr_scheduler_type = p_lr_scheduler_type
      self.p_lr = p_lr
      self.p_epoch = p_epoch
      self.p_weight_decay = p_weight_decay
      self.p_early_stopping_round = (
          p_early_stopping_round
          if p_early_stopping_round is not None
          else 100
      )
      self.p_early_stopping = EarlyStopping(
          patience=self.p_early_stopping_round, verbose=False
      )
      self.f_opt_received = f_optim
      self.f_optimizer = self._get_optimizer(f_optim)
      self.f_lr_scheduler_type = f_lr_scheduler_type
      self.f_lr = f_lr
      self.f_epoch = f_epoch
      self.f_weight_decay = f_weight_decay
      self.f_early_stopping_round = (
          f_early_stopping_round
          if f_early_stopping_round is not None
          else 100
      )
      self.f_early_stopping = EarlyStopping(
          patience=self.f_early_stopping_round, verbose=False
      )
      if isinstance(f_loss, str) and hasattr(F, f_loss):
          self.f_loss = getattr(F, f_loss)
      elif callable(f_loss):
          self.f_loss = f_loss
      else:
          raise NotImplementedError(
              f"The loss {f_loss} is not supported yet."
          )
      super().__init__(
          encoder=encoder,
          decoder=decoder if graph_level else decoder_node,
          device=self.device,
          feval=feval,
          loss=self.loss,
      )
  
imports: import os
  import torch
  import torch_geometric
  import logging
  import numpy
  import typing
  from tqdm import trange
  from typing import Union, Tuple, Sequence, Type, Callable
  import torch.nn.functional
  import torch.utils.data
  from torch.optim.lr_scheduler import StepLR, MultiStepLR, ExponentialLR, ReduceLROnPlateau
  from .losses import NTXent_loss
  from .utils import get_view_by_name
  from autogl.module.model.encoders.base_encoder import AutoHomogeneousEncoderMaintainer
  from ..base import BaseTrainer, EarlyStopping, _DummyModel
  from ..evaluation import Evaluation, get_feval, Acc
  from ...model import BaseAutoModel, BaseEncoderMaintainer, BaseDecoderMaintainer, EncoderUniversalRegistry, DecoderUniversalRegistry
  from ....utils import get_logger
  from ....datasets import utils
label_desc: Use image node_id 1 to create a new BaseContrastiveTrainer object from inherited base classes: BaseTrainer with example: obj = BaseContrastiveTrainer(encoder, decoder, decoder_node, num_features, num_graph_features, device, feval, loss, f_loss, views_fn, aug_ratio, graph_level, node_level, z_dim, z_node_dim, tau, p_optim, p_lr, p_lr_scheduler_type, p_weight_decay, p_epoch, p_early_stopping_round, f_optim, f_lr, f_lr_scheduler_type, f_weight_decay, f_epoch, f_early_stopping_round, model_path)