class_docstr: This is a DeepChem model implemented by a PyTorch model.
  
  Here is a simple example of code that uses TorchModel to train
  a PyTorch model on a DeepChem dataset.
  
  >>> import torch
  >>> import deepchem as dc
  >>> import numpy as np
  >>> X, y = np.random.random((10, 100)), np.random.random((10, 1))
  >>> dataset = dc.data.NumpyDataset(X=X, y=y)
  >>> pytorch_model = torch.nn.Sequential(
  ...   torch.nn.Linear(100, 1000),
  ...   torch.nn.Tanh(),
  ...   torch.nn.Linear(1000, 1))
  >>> model = dc.models.TorchModel(pytorch_model, loss=dc.models.losses.L2Loss())
  >>> loss = model.fit(dataset, nb_epoch=5)
  
  The loss function for a model can be defined in two different
  ways.  For models that have only a single output and use a
  standard loss function, you can simply provide a
  dc.models.losses.Loss object.  This defines the loss for each
  sample or sample/task pair.  The result is automatically
  multiplied by the weights and averaged over the batch.
  
  For more complicated cases, you can instead provide a function
  that directly computes the total loss.  It must be of the form
  f(outputs, labels, weights), taking the list of outputs from
  the model, the expected values, and any weight matrices.  It
  should return a scalar equal to the value of the loss function
  for the batch.  No additional processing is done to the
  result; it is up to you to do any weighting, averaging, adding
  of penalty terms, etc.
  
  You can optionally provide an output_types argument, which
  describes how to interpret the model's outputs.  This should
  be a list of strings, one for each output. You can use an
  arbitrary output_type for a output, but some output_types are
  special and will undergo extra processing:
  
  - 'prediction': This is a normal output, and will be returned by predict().
      If output types are not specified, all outputs are assumed
      to be of this type.
  
  - 'loss': This output will be used in place of the normal
      outputs for computing the loss function.  For example,
      models that output probability distributions usually do it
      by computing unbounded numbers (the logits), then passing
      them through a softmax function to turn them into
      probabilities.  When computing the cross entropy, it is more
      numerically stable to use the logits directly rather than
      the probabilities.  You can do this by having the model
      produce both probabilities and logits as outputs, then
      specifying output_types=['prediction', 'loss'].  When
      predict() is called, only the first output (the
      probabilities) will be returned.  But during training, it is
      the second output (the logits) that will be passed to the
      loss function.
  
  - 'variance': This output is used for estimating the
      uncertainty in another output.  To create a model that can
      estimate uncertainty, there must be the same number of
      'prediction' and 'variance' outputs.  Each variance output
      must have the same shape as the corresponding prediction
      output, and each element is an estimate of the variance in
      the corresponding prediction.  Also be aware that if a model
      supports uncertainty, it MUST use dropout on every layer,
      and dropout most be enabled during uncertainty prediction.
      Otherwise, the uncertainties it computes will be inaccurate.
  
  - other: Arbitrary output_types can be used to extract outputs
      produced by the model, but will have no additional
      processing performed.
class_docstr_tok: ["This","is","a","DeepChem","model","implemented","by","a","PyTorch","model",".","Here","is","a","simple","example","of","code","that","uses","TorchModel","to","train","a","PyTorch","model","on","a","DeepChem","dataset",".",">",">",">","import","torch",">",">",">","import","deepchem","as","dc",">",">",">","import","numpy","as","np",">",">",">","X",",","y","=","np.random.random","(","(","10",",","100",")",")",",","np.random.random","(","(","10",",","1",")",")",">",">",">","dataset","=","dc.data.NumpyDataset","(","X=X",",","y=y",")",">",">",">","pytorch_model","=","torch.nn.Sequential","(","...","torch.nn.Linear","(","100",",","1000",")",",","...","torch.nn.Tanh","(",")",",","...","torch.nn.Linear","(","1000",",","1",")",")",">",">",">","model","=","dc.models.TorchModel","(","pytorch_model",",","loss=dc.models.losses.L2Loss","(",")",")",">",">",">","loss","=","model.fit","(","dataset",",","nb_epoch=5",")","The","loss","function","for","a","model","can","be","defined","in","two","different","ways",".","For","models","that","have","only","a","single","output","and","use","a","standard","loss","function",",","you","can","simply","provide","a","dc.models.losses.Loss","object",".","This","defines","the","loss","for","each","sample","or","sample\/task","pair",".","The","result","is","automatically","multiplied","by","the","weights","and","averaged","over","the","batch",".","For","more","complicated","cases",",","you","can","instead","provide","a","function","that","directly","computes","the","total","loss",".","It","must","be","of","the","form","f","(","outputs",",","labels",",","weights",")",",","taking","the","list","of","outputs","from","the","model",",","the","expected","values",",","and","any","weight","matrices",".","It","should","return","a","scalar","equal","to","the","value","of","the","loss","function","for","the","batch",".","No","additional","processing","is","done","to","the","result",";","it","is","up","to","you","to","do","any","weighting",",","averaging",",","adding","of","penalty","terms",",","etc",".","You","can","optionally","provide","an","output_types","argument",",","which","describes","how","to","interpret","the","model","'s","outputs",".","This","should","be","a","list","of","strings",",","one","for","each","output",".","You","can","use","an","arbitrary","output_type","for","a","output",",","but","some","output_types","are","special","and","will","undergo","extra","processing",":","-","'prediction","'",":","This","is","a","normal","output",",","and","will","be","returned","by","predict","(",")",".","If","output","types","are","not","specified",",","all","outputs","are","assumed","to","be","of","this","type",".","-","'loss","'",":","This","output","will","be","used","in","place","of","the","normal","outputs","for","computing","the","loss","function",".","For","example",",","models","that","output","probability","distributions","usually","do","it","by","computing","unbounded","numbers","(","the","logits",")",",","then","passing","them","through","a","softmax","function","to","turn","them","into","probabilities",".","When","computing","the","cross","entropy",",","it","is","more","numerically","stable","to","use","the","logits","directly","rather","than","the","probabilities",".","You","can","do","this","by","having","the","model","produce","both","probabilities","and","logits","as","outputs",",","then","specifying","output_types=","[","'prediction","'",",","'loss","'","]",".","When","predict","(",")","is","called",",","only","the","first","output","(","the","probabilities",")","will","be","returned",".","But","during","training",",","it","is","the","second","output","(","the","logits",")","that","will","be","passed","to","the","loss","function",".","-","'variance","'",":","This","output","is","used","for","estimating","the","uncertainty","in","another","output",".","To","create","a","model","that","can","estimate","uncertainty",",","there","must","be","the","same","number","of","'prediction","'","and","'variance","'","outputs",".","Each","variance","output","must","have","the","same","shape","as","the","corresponding","prediction","output",",","and","each","element","is","an","estimate","of","the","variance","in","the","corresponding","prediction",".","Also","be","aware","that","if","a","model","supports","uncertainty",",","it","MUST","use","dropout","on","every","layer",",","and","dropout","most","be","enabled","during","uncertainty","prediction",".","Otherwise",",","the","uncertainties","it","computes","will","be","inaccurate",".","-","other",":","Arbitrary","output_types","can","be","used","to","extract","outputs","produced","by","the","model",",","but","will","have","no","additional","processing","performed","."]
docstr: Create a new TorchModel.
  
  Parameters
  ----------
  model: torch.nn.Module
      the PyTorch model implementing the calculation
  loss: dc.models.losses.Loss or function
      a Loss or function defining how to compute the training loss for each
      batch, as described above
  output_types: list of strings, optional (default None)
      the type of each output from the model, as described above
  batch_size: int, optional (default 100)
      default batch size for training and evaluating
  model_dir: str, optional (default None)
      the directory on disk where the model will be stored.  If this is None,
      a temporary directory is created.
  learning_rate: float or LearningRateSchedule, optional (default 0.001)
      the learning rate to use for fitting.  If optimizer is specified, this is
      ignored.
  optimizer: Optimizer, optional (default None)
      the optimizer to use for fitting.  If this is specified, learning_rate is
      ignored.
  tensorboard: bool, optional (default False)
      whether to log progress to TensorBoard during training
  wandb: bool, optional (default False)
      whether to log progress to Weights & Biases during training
  log_frequency: int, optional (default 100)
      The frequency at which to log data. Data is logged using
      `logging` by default. If `tensorboard` is set, data is also
      logged to TensorBoard. If `wandb` is set, data is also logged
      to Weights & Biases. Logging happens at global steps. Roughly,
      a global step corresponds to one batch of training. If you'd
      like a printout every 10 batch steps, you'd set
      `log_frequency=10` for example.
  device: torch.device, optional (default None)
      the device on which to run computations.  If None, a device is
      chosen automatically.
  regularization_loss: Callable, optional
      a function that takes no arguments, and returns an extra contribution to add
      to the loss function
  wandb_logger: WandbLogger
      the Weights & Biases logger object used to log data and metrics
docstr_tok: ["Create","a","new","TorchModel",".","Parameters","--","--","--","--","--","model",":","torch.nn.Module","the","PyTorch","model","implementing","the","calculation","loss",":","dc.models.losses.Loss","or","function","a","Loss","or","function","defining","how","to","compute","the","training","loss","for","each","batch",",","as","described","above","output_types",":","list","of","strings",",","optional","(","default","None",")","the","type","of","each","output","from","the","model",",","as","described","above","batch_size",":","int",",","optional","(","default","100",")","default","batch","size","for","training","and","evaluating","model_dir",":","str",",","optional","(","default","None",")","the","directory","on","disk","where","the","model","will","be","stored",".","If","this","is","None",",","a","temporary","directory","is","created",".","learning_rate",":","float","or","LearningRateSchedule",",","optional","(","default","0.001",")","the","learning","rate","to","use","for","fitting",".","If","optimizer","is","specified",",","this","is","ignored",".","optimizer",":","Optimizer",",","optional","(","default","None",")","the","optimizer","to","use","for","fitting",".","If","this","is","specified",",","learning_rate","is","ignored",".","tensorboard",":","bool",",","optional","(","default","False",")","whether","to","log","progress","to","TensorBoard","during","training","wandb",":","bool",",","optional","(","default","False",")","whether","to","log","progress","to","Weights","&","Biases","during","training","log_frequency",":","int",",","optional","(","default","100",")","The","frequency","at","which","to","log","data",".","Data","is","logged","using","`","logging","`","by","default",".","If","`","tensorboard","`","is","set",",","data","is","also","logged","to","TensorBoard",".","If","`","wandb","`","is","set",",","data","is","also","logged","to","Weights","&","Biases",".","Logging","happens","at","global","steps",".","Roughly",",","a","global","step","corresponds","to","one","batch","of","training",".","If","you'd","like","a","printout","every","10","batch","steps",",","you","'d","set","`","log_frequency=10","`","for","example",".","device",":","torch.device",",","optional","(","default","None",")","the","device","on","which","to","run","computations",".","If","None",",","a","device","is","chosen","automatically",".","regularization_loss",":","Callable",",","optional","a","function","that","takes","no","arguments",",","and","returns","an","extra","contribution","to","add","to","the","loss","function","wandb_logger",":","WandbLogger","the","Weights","&","Biases","logger","object","used","to","log","data","and","metrics"]
code: def __init__(
      self,
      model: torch.nn.Module,
      loss: Union[Loss, LossFn],
      output_types: Optional[List[str]] = None,
      batch_size: int = 100,
      model_dir: Optional[str] = None,
      learning_rate: Union[float, LearningRateSchedule] = 0.001,
      optimizer: Optional[Optimizer] = None,
      tensorboard: bool = False,
      wandb: bool = False,
      log_frequency: int = 100,
      device: Optional[torch.device] = None,
      regularization_loss: Optional[Callable] = None,
      wandb_logger: Optional[WandbLogger] = None,
      **kwargs,
  ) -> None:
      """Create a new TorchModel.
  
      Parameters
      ----------
      model: torch.nn.Module
          the PyTorch model implementing the calculation
      loss: dc.models.losses.Loss or function
          a Loss or function defining how to compute the training loss for each
          batch, as described above
      output_types: list of strings, optional (default None)
          the type of each output from the model, as described above
      batch_size: int, optional (default 100)
          default batch size for training and evaluating
      model_dir: str, optional (default None)
          the directory on disk where the model will be stored.  If this is None,
          a temporary directory is created.
      learning_rate: float or LearningRateSchedule, optional (default 0.001)
          the learning rate to use for fitting.  If optimizer is specified, this is
          ignored.
      optimizer: Optimizer, optional (default None)
          the optimizer to use for fitting.  If this is specified, learning_rate is
          ignored.
      tensorboard: bool, optional (default False)
          whether to log progress to TensorBoard during training
      wandb: bool, optional (default False)
          whether to log progress to Weights & Biases during training
      log_frequency: int, optional (default 100)
          The frequency at which to log data. Data is logged using
          `logging` by default. If `tensorboard` is set, data is also
          logged to TensorBoard. If `wandb` is set, data is also logged
          to Weights & Biases. Logging happens at global steps. Roughly,
          a global step corresponds to one batch of training. If you'd
          like a printout every 10 batch steps, you'd set
          `log_frequency=10` for example.
      device: torch.device, optional (default None)
          the device on which to run computations.  If None, a device is
          chosen automatically.
      regularization_loss: Callable, optional
          a function that takes no arguments, and returns an extra contribution to add
          to the loss function
      wandb_logger: WandbLogger
          the Weights & Biases logger object used to log data and metrics
      """
      super(TorchModel, self).__init__(
          model=model, model_dir=model_dir, **kwargs
      )
      self.loss = loss  # not used
      self.learning_rate = learning_rate
      self.output_types = output_types  # not used
      if isinstance(loss, Loss):
          self._loss_fn: LossFn = _StandardLoss(self, loss)
      else:
          self._loss_fn = loss
      self.batch_size = batch_size
      if optimizer is None:
          self.optimizer: Optimizer = Adam(learning_rate=learning_rate)
      else:
          self.optimizer = optimizer
      self.tensorboard = tensorboard
      self.regularization_loss = regularization_loss
  
      # Select a device.
  
      if device is None:
          if torch.cuda.is_available():
              device = torch.device("cuda")
          elif torch.backends.mps.is_available():
              device = torch.device("mps")
          else:
              device = torch.device("cpu")
      self.device = device
      self.model = model.to(device)
  
      # W&B logging
      if wandb:
          logger.warning(
              "`wandb` argument is deprecated. Please use `wandb_logger` instead. "
              "This argument will be removed in a future release of DeepChem."
          )
      if wandb and not _has_wandb:
          logger.warning(
              "You set wandb to True but W&B is not installed. To use wandb logging, "
              "run `pip install wandb; wandb login` see https://docs.wandb.com/huggingface."
          )
      self.wandb = wandb and _has_wandb
  
      self.wandb_logger = wandb_logger
      # If `wandb=True` and no logger is provided, initialize default logger
      if self.wandb and (self.wandb_logger is None):
          self.wandb_logger = WandbLogger()
  
      # Setup and initialize W&B logging
      if (self.wandb_logger is not None) and (
          not self.wandb_logger.initialized
      ):
          self.wandb_logger.setup()
  
      # Update config with KerasModel params
      wandb_logger_config = dict(
          loss=loss,
          output_types=output_types,
          batch_size=batch_size,
          model_dir=model_dir,
          learning_rate=learning_rate,
          optimizer=optimizer,
          tensorboard=tensorboard,
          log_frequency=log_frequency,
          regularization_loss=regularization_loss,
      )
      wandb_logger_config.update(**kwargs)
  
      if self.wandb_logger is not None:
          self.wandb_logger.update_config(wandb_logger_config)
  
      self.log_frequency = log_frequency
      if self.tensorboard and not _has_tensorboard:
          raise ImportError(
              "This class requires tensorboard to be installed."
          )
      if self.tensorboard:
          self._summary_writer = torch.utils.tensorboard.SummaryWriter(
              self.model_dir
          )
      if output_types is None:
          self._prediction_outputs = None
          self._loss_outputs = None
          self._variance_outputs = None
          self._other_outputs = None
      else:
          self._prediction_outputs = []
          self._loss_outputs = []
          self._variance_outputs = []
          self._other_outputs = []
          for i, type in enumerate(output_types):
              if type == "prediction":
                  self._prediction_outputs.append(i)
              elif type == "loss":
                  self._loss_outputs.append(i)
              elif type == "variance":
                  self._variance_outputs.append(i)
              else:
                  self._other_outputs.append(i)
          if len(self._loss_outputs) == 0:
              self._loss_outputs = self._prediction_outputs
      self._built = False
      self._output_functions: Dict[Any, Any] = {}
      self._optimizer_for_vars: Dict[Any, Any] = {}
  
imports: import numpy
  import torch
  import time
  import logging
  import os
  import datetime
  from deepchem.data import Dataset, NumpyDataset
  from deepchem.metrics import Metric
  from deepchem.models.losses import Loss
  from deepchem.models.models import Model
  from deepchem.models.optimizers import Adam, Optimizer, LearningRateSchedule
  from deepchem.trans import Transformer, undo_transforms
  from deepchem.utils.evaluate import GeneratorEvaluator
  from collections.abc import Sequence
  from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union
  from deepchem.utils.typing import LossFn, OneOrMany
  from deepchem.models.wandblogger import WandbLogger
label_desc: Use image node_id 1 to create a new TorchModel object from inherited base classes: Model with example: obj = TorchModel(model, loss, output_types, batch_size, model_dir, learning_rate, optimizer, tensorboard, wandb, log_frequency, device, regularization_loss, wandb_logger)