discrete_sac#
Source code: tianshou/algorithm/modelfree/discrete_sac.py
- class DiscreteSACTrainingStats(*, train_time: float = 0.0, smoothed_loss: dict = <factory>, actor_loss: float, critic1_loss: float, critic2_loss: float, alpha: float | None = None, alpha_loss: float | None = None)[source]#
Bases:
SACTrainingStats
- class DiscreteSACPolicy(*, actor: Module, deterministic_eval: bool = True, action_space: Space, observation_space: Space | None = None)[source]#
Bases:
Policy- Parameters:
actor – the actor network following the rules (s -> dist_input_BD), where the distribution input is for a Categorical distribution.
deterministic_eval – flag indicating whether the policy should use deterministic actions (using the mode of the action distribution) instead of stochastic ones (using random sampling) during evaluation. When enabled, the policy will always select the most probable action according to the learned distribution during evaluation phases, while still using stochastic sampling during training. This creates a clear distinction between exploration (training) and exploitation (evaluation) behaviors. Deterministic actions are generally preferred for final deployment and reproducible evaluation as they provide consistent behavior, reduce variance in performance metrics, and are more interpretable for human observers. Note that this parameter only affects behavior when the policy is not within a training step. When collecting rollouts for training, actions remain stochastic regardless of this setting to maintain proper exploration behaviour.
action_space – the environment’s action_space.
observation_space – the environment’s observation space
- forward(batch: ObsBatchProtocol, state: dict | BatchProtocol | ndarray | None = None, **kwargs: Any) Batch[source]#
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class DiscreteSAC(*, policy: DiscreteSACPolicy, policy_optim: OptimizerFactory, critic: Module | DiscreteCritic, critic_optim: OptimizerFactory, critic2: Module | DiscreteCritic | None = None, critic2_optim: OptimizerFactory | None = None, tau: float = 0.005, gamma: float = 0.99, alpha: float | Alpha = 0.2, n_step_return_horizon: int = 1)[source]#
Bases:
ActorDualCriticsOffPolicyAlgorithm[DiscreteSACPolicy,DistBatchProtocol]Implementation of SAC for Discrete Action Settings. arXiv:1910.07207.
- Parameters:
policy – the policy
policy_optim – the optimizer factory for the policy’s model.
critic – the first critic network. (s -> <Q(s, a_1), …, Q(s, a_N)>).
critic_optim – the optimizer factory for the first critic network.
critic2 – the second critic network. (s -> <Q(s, a_1), …, Q(s, a_N)>). If None, copy the first critic (via deepcopy).
critic2_optim – the optimizer factory for the second critic network. If None, use the first critic’s factory.
tau – the soft update coefficient for target networks, controlling the rate at which target networks track the learned networks. When the parameters of the target network are updated with the current (source) network’s parameters, a weighted average is used: target = tau * source + (1 - tau) * target. Smaller values (closer to 0) create more stable but slower learning as target networks change more gradually. Higher values (closer to 1) allow faster learning but may reduce stability. Typically set to a small value (0.001 to 0.01) for most reinforcement learning tasks.
gamma – the discount factor in [0, 1] for future rewards. This determines how much future rewards are valued compared to immediate ones. Lower values (closer to 0) make the agent focus on immediate rewards, creating “myopic” behavior. Higher values (closer to 1) make the agent value long-term rewards more, potentially improving performance in tasks where delayed rewards are important but increasing training variance by incorporating more environmental stochasticity. Typically set between 0.9 and 0.99 for most reinforcement learning tasks
alpha – the entropy regularization coefficient alpha or an object which can be used to automatically tune it (e.g. an instance of AutoAlpha).
n_step_return_horizon – the number of future steps (> 0) to consider when computing temporal difference (TD) targets. Controls the balance between TD learning and Monte Carlo methods: higher values reduce bias (by relying less on potentially inaccurate value estimates) but increase variance (by incorporating more environmental stochasticity and reducing the averaging effect). A value of 1 corresponds to standard TD learning with immediate bootstrapping, while very large values approach Monte Carlo-like estimation that uses complete episode returns.