class_docstr: ModularTorchModel is a subclass of TorchModel that allows for components to be
  pretrained and then combined into a final model. It is designed to be subclassed
  for specific models and is not intended to be used directly. There are 3 main differences
  between ModularTorchModel and TorchModel:
  
  - The build_components() method is used to define the components of the model.
  - The components are combined into a final model with the build_model() method.
  - The loss function is defined with the loss_func method. This may access the
    components to compute the loss using intermediate values from the network, rather
    than just the full forward pass output.
  
  Here is an example of how to use ModularTorchModel to pretrain a linear layer, load
  it into another network and then finetune that network:
  
  >>> import numpy as np
  >>> import deepchem as dc
  >>> import torch
  >>> n_samples = 6
  >>> n_feat = 3
  >>> n_hidden = 2
  >>> n_tasks = 6
  >>> pt_tasks = 3
  >>> X = np.random.rand(n_samples, n_feat)
  >>> y_pretrain = np.zeros((n_samples, pt_tasks)).astype(np.float32)
  >>> dataset_pt = dc.data.NumpyDataset(X, y_pretrain)
  >>> y_finetune = np.zeros((n_samples, n_tasks)).astype(np.float32)
  >>> dataset_ft = dc.data.NumpyDataset(X, y_finetune)
  >>> components = {'linear': torch.nn.Linear(n_feat, n_hidden),
  ... 'activation': torch.nn.ReLU(), 'head': torch.nn.Linear(n_hidden, n_tasks)}
  >>> model = torch.nn.Sequential(components['linear'], components['activation'],
  ... components['head'])
  >>> modular_model = dc.models.torch_models.modular.ModularTorchModel(model, components)
  >>> def example_loss_func(inputs, labels, weights):
  ...    return (torch.nn.functional.mse_loss(model(inputs), labels[0]) * weights[0]).mean()
  >>> modular_model.loss_func = example_loss_func
  >>> def example_model_build():
  ...     return torch.nn.Sequential(components['linear'], components['activation'],
  ... components['head'])
  >>> modular_model.build_model = example_model_build
  >>> pretrain_components = {'linear': torch.nn.Linear(n_feat, n_hidden),
  ... 'activation': torch.nn.ReLU(), 'head': torch.nn.Linear(n_hidden, pt_tasks)}
  >>> pretrain_model = torch.nn.Sequential(pretrain_components['linear'],
  ... pretrain_components['activation'], pretrain_components['head'])
  >>> pretrain_modular_model = dc.models.torch_models.modular.ModularTorchModel(pretrain_model,
  ... pretrain_components)
  >>> def example_pt_loss_func(inputs, labels, weights):
  ...     return (torch.nn.functional.mse_loss(pretrain_model(inputs), labels[0]) * weights[0]).mean()
  >>> pretrain_modular_model.loss_func = example_pt_loss_func
  >>> pt_loss = pretrain_modular_model.fit(dataset_pt, nb_epoch=1)
  >>> modular_model.load_from_pretrained(pretrain_modular_model, components=['linear'])
  >>> ft_loss = modular_model.fit(dataset_ft, nb_epoch=1)
class_docstr_tok: ["ModularTorchModel","is","a","subclass","of","TorchModel","that","allows","for","components","to","be","pretrained","and","then","combined","into","a","final","model",".","It","is","designed","to","be","subclassed","for","specific","models","and","is","not","intended","to","be","used","directly",".","There","are","3","main","differences","between","ModularTorchModel","and","TorchModel",":","-","The","build_components","(",")","method","is","used","to","define","the","components","of","the","model",".","-","The","components","are","combined","into","a","final","model","with","the","build_model","(",")","method",".","-","The","loss","function","is","defined","with","the","loss_func","method",".","This","may","access","the","components","to","compute","the","loss","using","intermediate","values","from","the","network",",","rather","than","just","the","full","forward","pass","output",".","Here","is","an","example","of","how","to","use","ModularTorchModel","to","pretrain","a","linear","layer",",","load","it","into","another","network","and","then","finetune","that","network",":",">",">",">","import","numpy","as","np",">",">",">","import","deepchem","as","dc",">",">",">","import","torch",">",">",">","n_samples","=","6",">",">",">","n_feat","=","3",">",">",">","n_hidden","=","2",">",">",">","n_tasks","=","6",">",">",">","pt_tasks","=","3",">",">",">","X","=","np.random.rand","(","n_samples",",","n_feat",")",">",">",">","y_pretrain","=","np.zeros","(","(","n_samples",",","pt_tasks",")",")",".astype","(","np.float32",")",">",">",">","dataset_pt","=","dc.data.NumpyDataset","(","X",",","y_pretrain",")",">",">",">","y_finetune","=","np.zeros","(","(","n_samples",",","n_tasks",")",")",".astype","(","np.float32",")",">",">",">","dataset_ft","=","dc.data.NumpyDataset","(","X",",","y_finetune",")",">",">",">","components","=","{","'linear","'",":","torch.nn.Linear","(","n_feat",",","n_hidden",")",",","...","'activation","'",":","torch.nn.ReLU","(",")",",","'head","'",":","torch.nn.Linear","(","n_hidden",",","n_tasks",")","}",">",">",">","model","=","torch.nn.Sequential","(","components","[","'linear","'","]",",","components","[","'activation","'","]",",","...","components","[","'head","'","]",")",">",">",">","modular_model","=","dc.models.torch_models.modular.ModularTorchModel","(","model",",","components",")",">",">",">","def","example_loss_func","(","inputs",",","labels",",","weights",")",":","...","return","(","torch.nn.functional.mse_loss","(","model","(","inputs",")",",","labels","[","0","]",")","*","weights","[","0","]",")",".mean","(",")",">",">",">","modular_model.loss_func","=","example_loss_func",">",">",">","def","example_model_build","(",")",":","...","return","torch.nn.Sequential","(","components","[","'linear","'","]",",","components","[","'activation","'","]",",","...","components","[","'head","'","]",")",">",">",">","modular_model.build_model","=","example_model_build",">",">",">","pretrain_components","=","{","'linear","'",":","torch.nn.Linear","(","n_feat",",","n_hidden",")",",","...","'activation","'",":","torch.nn.ReLU","(",")",",","'head","'",":","torch.nn.Linear","(","n_hidden",",","pt_tasks",")","}",">",">",">","pretrain_model","=","torch.nn.Sequential","(","pretrain_components","[","'linear","'","]",",","...","pretrain_components","[","'activation","'","]",",","pretrain_components","[","'head","'","]",")",">",">",">","pretrain_modular_model","=","dc.models.torch_models.modular.ModularTorchModel","(","pretrain_model",",","...","pretrain_components",")",">",">",">","def","example_pt_loss_func","(","inputs",",","labels",",","weights",")",":","...","return","(","torch.nn.functional.mse_loss","(","pretrain_model","(","inputs",")",",","labels","[","0","]",")","*","weights","[","0","]",")",".mean","(",")",">",">",">","pretrain_modular_model.loss_func","=","example_pt_loss_func",">",">",">","pt_loss","=","pretrain_modular_model.fit","(","dataset_pt",",","nb_epoch=1",")",">",">",">","modular_model.load_from_pretrained","(","pretrain_modular_model",",","components=","[","'linear","'","]",")",">",">",">","ft_loss","=","modular_model.fit","(","dataset_ft",",","nb_epoch=1",")"]
docstr: Copies parameter values from a pretrained model. The pretrained model can be loaded as a source_model (ModularTorchModel object), checkpoint (pytorch .ckpt file) or a model_dir (directory with .ckpt files).
  Specific components can be chosen by passing a list of strings with the desired component names. If both a source_model and a checkpoint/model_dir are loaded, the source_model weights will be loaded.
  
  Parameters
  ----------
  source_model: dc.ModularTorchModel, 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
  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
  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",".","The","pretrained","model","can","be","loaded","as","a","source_model","(","ModularTorchModel","object",")",",","checkpoint","(","pytorch",".ckpt","file",")","or","a","model_dir","(","directory","with",".ckpt","files",")",".","Specific","components","can","be","chosen","by","passing","a","list","of","strings","with","the","desired","component","names",".","If","both","a","source_model","and","a","checkpoint\/model_dir","are","loaded",",","the","source_model","weights","will","be","loaded",".","Parameters","--","--","--","--","--","source_model",":","dc.ModularTorchModel",",","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","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","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(  # type: ignore
      self,
      source_model: Optional["ModularTorchModel"] = None,
      components: Optional[List[str]] = None,
      checkpoint: Optional[str] = None,
      model_dir: Optional[str] = None,
      inputs: Optional[Sequence[Any]] = None,
      **kwargs,
  ) -> None:
      """Copies parameter values from a pretrained model. The pretrained model can be loaded as a source_model (ModularTorchModel object), checkpoint (pytorch .ckpt file) or a model_dir (directory with .ckpt files).
      Specific components can be chosen by passing a list of strings with the desired component names. If both a source_model and a checkpoint/model_dir are loaded, the source_model weights will be loaded.
  
      Parameters
      ----------
      source_model: dc.ModularTorchModel, 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
      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
      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.
          if source_model:
              source_model.model(inputs)
          self.model(inputs)
  
      self._ensure_built()
  
      if source_model is not None:
          for name, module in source_model.components.items():
              if components is None or name in components:
                  self.components[name].load_state_dict(
                      module.state_dict(), strict=False
                  )
          self.build_model()
  
      elif source_model is None:
          self.restore(
              components=components,
              checkpoint=checkpoint,
              model_dir=model_dir,
          )
  
imports: import time
  import logging
  import os
  from collections.abc import Sequence
  from typing import Any, Callable, Iterable, List, Optional, Tuple, Union, Sequence
  import torch
  import torch.nn
  from deepchem.models.torch_models.torch_model import TorchModel
  from deepchem.models.optimizers import LearningRateSchedule
  from deepchem.utils.typing import LossFn, OneOrMany
label_desc: Use image node_id 8 for calling the ModularTorchModel obj's underlying member method code with example usage: obj.load_from_pretrained(source_model, components, checkpoint, model_dir, inputs) without return types