class_docstr: Torch version of ModelV2.
  
  Note that this class by itself is not a valid model unless you
  inherit from nn.Module and implement forward() in a subclass.
class_docstr_tok: ["Torch","version","of","ModelV2",".","Note","that","this","class","by","itself","is","not","a","valid","model","unless","you","inherit","from","nn.Module","and","implement","forward","(",")","in","a","subclass","."]
docstr: Initialize a TorchModelV2.
  
  Here is an example implementation for a subclass
  ``MyModelClass(TorchModelV2, nn.Module)``::
  
      def __init__(self, *args, **kwargs):
          TorchModelV2.__init__(self, *args, **kwargs)
          nn.Module.__init__(self)
          self._hidden_layers = nn.Sequential(...)
          self._logits = ...
          self._value_branch = ...
docstr_tok: ["Initialize","a","TorchModelV2",".","Here","is","an","example","implementation","for","a","subclass","``","MyModelClass","(","TorchModelV2",",","nn.Module",")","``",":",":","def","__init__","(","self",",","*","args",",","*","*","kwargs",")",":","TorchModelV2.__init__","(","self",",","*","args",",","*","*","kwargs",")","nn.Module.__init__","(","self",")","self._hidden_layers","=","nn.Sequential","(","...",")","self._logits","=","...","self._value_branch","=","..."]
code: def __init__(
      self,
      obs_space: gym.spaces.Space,
      action_space: gym.spaces.Space,
      num_outputs: int,
      model_config: ModelConfigDict,
      name: str,
  ):
      """Initialize a TorchModelV2.
  
      Here is an example implementation for a subclass
      ``MyModelClass(TorchModelV2, nn.Module)``::
  
          def __init__(self, *args, **kwargs):
              TorchModelV2.__init__(self, *args, **kwargs)
              nn.Module.__init__(self)
              self._hidden_layers = nn.Sequential(...)
              self._logits = ...
              self._value_branch = ...
      """
      if not isinstance(self, nn.Module):
          raise ValueError(
              "Subclasses of TorchModelV2 must also inherit from "
              "nn.Module, e.g., MyModel(TorchModelV2, nn.Module)"
          )
  
      ModelV2.__init__(
          self,
          obs_space,
          action_space,
          num_outputs,
          model_config,
          name,
          framework="torch",
      )
  
      # Dict to store per multi-gpu tower stats into.
      # In PyTorch multi-GPU, we use a single TorchPolicy and copy
      # it's Model(s) n times (1 copy for each GPU). When computing the loss
      # on each tower, we cannot store the stats (e.g. `entropy`) inside the
      # policy object as this would lead to race conditions between the
      # different towers all accessing the same property at the same time.
      self.tower_stats = {}
  
file_path: ray/python/ray/rllib/models/torch/torch_modelv2.py
imports: import gymnasium
  from typing import Dict, List, Union
  from ray.rllib.models.modelv2 import ModelV2
  from ray.rllib.utils.annotations import override, PublicAPI
  from ray.rllib.utils.framework import try_import_torch
  from ray.rllib.utils.typing import ModelConfigDict, TensorType
label_desc: Use image node_id 1 to create a new TorchModelV2 object from inherited base classes: ModelV2 with example: obj = TorchModelV2(obs_space, action_space, num_outputs, model_config, name)