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"]
code: def __init__(
      self,
      model: torch.nn.Module,
      device: Union[torch.device, str] = "cuda",
      optimizer: str = "AdamW",
      optimizer_parameters: Optional[dict] = None,
      learning_rate_drop_patience: Optional[int] = None,
      learning_rate_drop_div: Optional[float] = None,
      load_before_drop: bool = True,
      min_learning_rate: float = 1e-07,
      clip_norm: Optional[float] = None,
      *args,
      **kwargs,
  ):
      super().__init__(*args, **kwargs)
      self.model = model
      self.device = self._init_device(device)
      self.model.to(self.device)
      if self.device.type == "cuda" and torch.cuda.device_count() > 1:
          self.model = torch.nn.DataParallel(self.model)
      if optimizer_parameters is None:
          optimizer_parameters = {"lr": 0.01}
      self.optimizer = getattr(torch.optim, optimizer)(
          self.model.parameters(), **optimizer_parameters
      )
      self.epochs_done = 0
      self.learning_rate_drop_patience = learning_rate_drop_patience
      self.learning_rate_drop_div = learning_rate_drop_div
      self.load_before_drop = load_before_drop
      self.min_learning_rate = min_learning_rate
      self.clip_norm = clip_norm
      self.load()
      # we need to switch to eval mode here because by default it's in `train` mode.
      # But in case of `interact/build_model` usage, we need to have model in eval mode.
      self.model.eval()
      log.debug(
          f"Model was successfully initialized! Model summary:\n {self.model}"
      )
  
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 1 to create a new TorchModel object from inherited base classes: NNModel with example: obj = TorchModel(model, device, optimizer, optimizer_parameters, learning_rate_drop_patience, learning_rate_drop_div, load_before_drop, min_learning_rate, clip_norm)