class_docstr: Class implements torch model's main methods.
  
  Args:
      model: torch.nn.Model-based neural network model
      device: device to use
      optimizer: name of `torch.optim` optimizer
      optimizer_parameters: dictionary with optimizer parameters
      learning_rate_drop_patience: how many validations with no improvements to wait
      learning_rate_drop_div: the divider of the learning rate after `learning_rate_drop_patience` unsuccessful
          validations
      load_before_drop: whether to load best model before dropping learning rate or not
      min_learning_rate: min value of learning rate if learning rate decay is used
      args:
      kwargs: dictionary with other model parameters
  
  Attributes:
      device: `cpu` or `cuda` device to use
      opt: dictionary with all model parameters
      model: torch model
      epochs_done: number of epochs that were done
      optimizer: `torch.optim` instance
      learning_rate_drop_patience: how many validations with no improvements to wait
      learning_rate_drop_div: the divider of the learning rate after `learning_rate_drop_patience` unsuccessful
          validations
      load_before_drop: whether to load best model before dropping learning rate or not
      min_learning_rate: min value of learning rate if learning rate decay is used
      clip_norm: clip gradients by norm coefficient
class_docstr_tok: ["Class","implements","torch","model","'s","main","methods",".","Args",":","model",":","torch.nn.Model-based","neural","network","model","device",":","device","to","use","optimizer",":","name","of","`","torch.optim","`","optimizer","optimizer_parameters",":","dictionary","with","optimizer","parameters","learning_rate_drop_patience",":","how","many","validations","with","no","improvements","to","wait","learning_rate_drop_div",":","the","divider","of","the","learning","rate","after","`","learning_rate_drop_patience","`","unsuccessful","validations","load_before_drop",":","whether","to","load","best","model","before","dropping","learning","rate","or","not","min_learning_rate",":","min","value","of","learning","rate","if","learning","rate","decay","is","used","args",":","kwargs",":","dictionary","with","other","model","parameters","Attributes",":","device",":","`","cpu","`","or","`","cuda","`","device","to","use","opt",":","dictionary","with","all","model","parameters","model",":","torch","model","epochs_done",":","number","of","epochs","that","were","done","optimizer",":","`","torch.optim","`","instance","learning_rate_drop_patience",":","how","many","validations","with","no","improvements","to","wait","learning_rate_drop_div",":","the","divider","of","the","learning","rate","after","`","learning_rate_drop_patience","`","unsuccessful","validations","load_before_drop",":","whether","to","load","best","model","before","dropping","learning","rate","or","not","min_learning_rate",":","min","value","of","learning","rate","if","learning","rate","decay","is","used","clip_norm",":","clip","gradients","by","norm","coefficient"]
docstr: Load model from `fname` (if `fname` is not given, use `self.load_path`) to `self.model` along with
      the optimizer `self.optimizer`.
      If `fname` (if `fname` is not given, use `self.load_path`) does not exist, initialize model from scratch.
  
  Args:
      fname: string path to checkpoint
      *args:
      **kwargs:
  
  Returns:
      None
docstr_tok: ["Load","model","from","`","fname","`","(","if","`","fname","`","is","not","given",",","use","`","self.load_path","`",")","to","`","self.model","`","along","with","the","optimizer","`","self.optimizer","`",".","If","`","fname","`","(","if","`","fname","`","is","not","given",",","use","`","self.load_path","`",")","does","not","exist",",","initialize","model","from","scratch",".","Args",":","fname",":","string","path","to","checkpoint","*","args",":","*","*","kwargs",":","Returns",":","None"]
code: def load(self, fname: Optional[str] = None, *args, **kwargs) -> None:
      """Load model from `fname` (if `fname` is not given, use `self.load_path`) to `self.model` along with
          the optimizer `self.optimizer`.
          If `fname` (if `fname` is not given, use `self.load_path`) does not exist, initialize model from scratch.
  
      Args:
          fname: string path to checkpoint
          *args:
          **kwargs:
  
      Returns:
          None
      """
      if fname is not None:
          self.load_path = fname
  
      if self.load_path:
          log.debug(f"Load path {self.load_path} is given.")
          if (
              isinstance(self.load_path, Path)
              and not self.load_path.parent.is_dir()
          ):
              raise ConfigError("Provided load path is incorrect!")
  
          weights_path = Path(self.load_path.resolve())
          weights_path = weights_path.with_suffix(f".pth.tar")
          if weights_path.exists():
              log.debug(f"Load path {weights_path} exists.")
              log.debug(
                  f"Initializing `{self.__class__.__name__}` from saved."
              )
  
              # now load the weights, optimizer from saved
              log.debug(f"Loading weights from {weights_path}.")
              checkpoint = torch.load(
                  weights_path, map_location=self.device
              )
              model_state = checkpoint["model_state_dict"]
              optimizer_state = checkpoint["optimizer_state_dict"]
              # load a multi-gpu model on a single device
              if all(
                  [
                      key.startswith("module.")
                      for key in list(model_state.keys())
                  ]
              ):
                  model_state = {
                      key.replace("module.", "", 1): val
                      for key, val in model_state.items()
                  }
  
              if self.is_data_parallel:
                  self.model.module.load_state_dict(model_state)
              else:
                  self.model.load_state_dict(model_state)
              try:  # TODO: remove this try-except after hf models deep update
                  self.optimizer.load_state_dict(optimizer_state)
              except ValueError as e:
                  log.error(
                      f"Failed to load optimizer state due to {repr(e)}"
                  )
              self.epochs_done = checkpoint.get("epochs_done", 0)
          else:
              log.warning(
                  f"Init from scratch. Load path {weights_path} does not exist."
              )
      else:
          log.warning(
              f"Init from scratch. Load path {self.load_path} is not provided."
          )
      self.model.to(self.device)
  
file_path: DeepPavlov/deeppavlov/core/models/torch_model.py
imports: from abc import abstractmethod
  from logging import getLogger
  from pathlib import Path
  from typing import Optional, Union
  import torch
  from deeppavlov.core.common.errors import ConfigError
  from deeppavlov.core.models.nn_model import NNModel
label_desc: Use image node_id 4 for calling the TorchModel obj's underlying member method code with example usage: obj.load(fname) without return types