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: Copies parameter values from a pretrained model. `source_model` can either
  be a pretrained model or a model with the same architecture. `value_map`
  is a parameter-value dictionary. If no `value_map` is provided, the parameter
  values are restored to the `source_model` from a checkpoint and a default
  `value_map` is created. `assignment_map` is a dictionary mapping parameters
  from the `source_model` to the current model. If no `assignment_map` is
  provided, one is made from scratch and assumes the model is composed of
  several different layers, with the final one being a dense layer. include_top
  is used to control whether or not the final dense layer is used. The default
  assignment map is useful in cases where the type of task is different
  (classification vs regression) and/or number of tasks in the setting.
  
  Parameters
  ----------
  source_model: dc.TorchModel, required
      source_model can either be the pretrained model or a dc.TorchModel with
      the same architecture as the pretrained model. It is used to restore from
      a checkpoint, if value_map is None and to create a default assignment map
      if assignment_map is None
  assignment_map: Dict, default None
      Dictionary mapping the source_model parameters and current model parameters
  value_map: Dict, default None
      Dictionary containing source_model trainable parameters mapped to numpy
      arrays. If value_map is None, the values are restored and a default
      parameter map is created using the restored values
  checkpoint: str, default None
      the path to the checkpoint file to load.  If this is None, the most recent
      checkpoint will be chosen automatically.  Call get_checkpoints() to get a
      list of all available checkpoints
  model_dir: str, default None
      Restore source model from custom model directory if needed
  include_top: bool, default True
      if True, copies the weights and bias associated with the final dense
      layer. Used only when assignment map is None
  inputs: List, input tensors for model
      if not None, then the weights are built for both the source and self.
docstr_tok: ["Copies","parameter","values","from","a","pretrained","model",".","`","source_model","`","can","either","be","a","pretrained","model","or","a","model","with","the","same","architecture",".","`","value_map","`","is","a","parameter-value","dictionary",".","If","no","`","value_map","`","is","provided",",","the","parameter","values","are","restored","to","the","`","source_model","`","from","a","checkpoint","and","a","default","`","value_map","`","is","created",".","`","assignment_map","`","is","a","dictionary","mapping","parameters","from","the","`","source_model","`","to","the","current","model",".","If","no","`","assignment_map","`","is","provided",",","one","is","made","from","scratch","and","assumes","the","model","is","composed","of","several","different","layers",",","with","the","final","one","being","a","dense","layer",".","include_top","is","used","to","control","whether","or","not","the","final","dense","layer","is","used",".","The","default","assignment","map","is","useful","in","cases","where","the","type","of","task","is","different","(","classification","vs","regression",")","and\/or","number","of","tasks","in","the","setting",".","Parameters","--","--","--","--","--","source_model",":","dc.TorchModel",",","required","source_model","can","either","be","the","pretrained","model","or","a","dc.TorchModel","with","the","same","architecture","as","the","pretrained","model",".","It","is","used","to","restore","from","a","checkpoint",",","if","value_map","is","None","and","to","create","a","default","assignment","map","if","assignment_map","is","None","assignment_map",":","Dict",",","default","None","Dictionary","mapping","the","source_model","parameters","and","current","model","parameters","value_map",":","Dict",",","default","None","Dictionary","containing","source_model","trainable","parameters","mapped","to","numpy","arrays",".","If","value_map","is","None",",","the","values","are","restored","and","a","default","parameter","map","is","created","using","the","restored","values","checkpoint",":","str",",","default","None","the","path","to","the","checkpoint","file","to","load",".","If","this","is","None",",","the","most","recent","checkpoint","will","be","chosen","automatically",".","Call","get_checkpoints","(",")","to","get","a","list","of","all","available","checkpoints","model_dir",":","str",",","default","None","Restore","source","model","from","custom","model","directory","if","needed","include_top",":","bool",",","default","True","if","True",",","copies","the","weights","and","bias","associated","with","the","final","dense","layer",".","Used","only","when","assignment","map","is","None","inputs",":","List",",","input","tensors","for","model","if","not","None",",","then","the","weights","are","built","for","both","the","source","and","self","."]
code: def load_from_pretrained(
      self,
      source_model: "TorchModel",
      assignment_map: Optional[Dict[Any, Any]] = None,
      value_map: Optional[Dict[Any, Any]] = None,
      checkpoint: Optional[str] = None,
      model_dir: Optional[str] = None,
      include_top: bool = True,
      inputs: Optional[Sequence[Any]] = None,
      **kwargs,
  ) -> None:
      """Copies parameter values from a pretrained model. `source_model` can either
      be a pretrained model or a model with the same architecture. `value_map`
      is a parameter-value dictionary. If no `value_map` is provided, the parameter
      values are restored to the `source_model` from a checkpoint and a default
      `value_map` is created. `assignment_map` is a dictionary mapping parameters
      from the `source_model` to the current model. If no `assignment_map` is
      provided, one is made from scratch and assumes the model is composed of
      several different layers, with the final one being a dense layer. include_top
      is used to control whether or not the final dense layer is used. The default
      assignment map is useful in cases where the type of task is different
      (classification vs regression) and/or number of tasks in the setting.
  
      Parameters
      ----------
      source_model: dc.TorchModel, required
          source_model can either be the pretrained model or a dc.TorchModel with
          the same architecture as the pretrained model. It is used to restore from
          a checkpoint, if value_map is None and to create a default assignment map
          if assignment_map is None
      assignment_map: Dict, default None
          Dictionary mapping the source_model parameters and current model parameters
      value_map: Dict, default None
          Dictionary containing source_model trainable parameters mapped to numpy
          arrays. If value_map is None, the values are restored and a default
          parameter map is created using the restored values
      checkpoint: str, default None
          the path to the checkpoint file to load.  If this is None, the most recent
          checkpoint will be chosen automatically.  Call get_checkpoints() to get a
          list of all available checkpoints
      model_dir: str, default None
          Restore source model from custom model directory if needed
      include_top: bool, default True
          if True, copies the weights and bias associated with the final dense
          layer. Used only when assignment map is None
      inputs: List, input tensors for model
          if not None, then the weights are built for both the source and self.
      """
      if inputs is not None:
          # Ensure weights for both models are built.
          source_model.model(inputs)
          self.model(inputs)
  
      self._ensure_built()
      if value_map is None:
          logger.info(
              "No value map provided. Creating default value map from restored model."
          )
          source_model.restore(
              model_dir=model_dir, checkpoint=checkpoint
          )
          value_map = self._create_value_map(source_model=source_model)
  
      if assignment_map is None:
          logger.info(
              "No assignment map provided. Creating custom assignment map."
          )
          assignment_map = self._create_assignment_map(
              source_model=source_model, include_top=include_top
          )
  
      for source_var, dest_var in assignment_map.items():
          assert source_var.shape == dest_var.shape
          dest_var.data = torch.as_tensor(
              value_map[source_var], device=self.device
          )
  
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 24 for calling the TorchModel obj's underlying member method code with example usage: obj.load_from_pretrained(source_model, assignment_map, value_map, checkpoint, model_dir, include_top, inputs) without return types