DQNModel
DQNModel(observation_shape: tuple[int, ...], action_shape: int, model_config: DQNModelConfiguration | None = None) -> NoneDQN model with convolutional and fully connected layers.
Initialize the DQNModel.
Parameters:
observation_shape(tuple[int, ...]) – The shape of the environment observations.action_shape(int) – The number of possible actions.model_config(DQNModelConfiguration | None) – Optional configuration for the model architecture.
Methods
forward
forward(x: torch.Tensor) -> torch.TensorPerform a forward pass through the model.
Parameters:
x(Tensor) – The input tensor in the source observation dtype.
Returns:
Tensor– The output Q-values (non-categorical) or probabilities (categorical). For categorical, returns shape (batch, actions, atoms) with probabilities. For non-categorical, returns shape (batch, actions) with Q-values.
get_q_values
get_q_values(x: torch.Tensor) -> torch.TensorCompute Q-values for the given input states.
For categorical DQN, this computes the expected Q-value from the probability distribution: Q(s, a) = sum_i(p_i * z_i) where z_i is the support atom value and p_i is the probability of that atom.
For non-categorical DQN, this is equivalent to forward().
Parameters:
x(Tensor) – The input tensor of shape (batch, *observation_shape).
Returns:
Tensor– Q-values of shape (batch, actions).
reset_noise
reset_noise() -> NoneReset noise for all noisy layers in the model. Uses batched noise generation to reduce kernel launch overhead.
set_advantage_mask
set_advantage_mask(action_masker: ActionMasker) -> NoneSet a mask for the duelling advantage mean computation.
When action masking is used, only a subset of actions receive meaningful gradient updates. The advantage mean should be computed over only those actions so the centring property holds for the trained action subspace.
Parameters:
action_masker(ActionMasker) – The ActionMasker whose mask will be used to compute the centred advantage.
Values
action_shape
action_shape = action_shapeadvantage_head
advantage_head = nn.Sequential(*adv_layers)categorical
categorical = model_config.categorical is not Noneconv
conv = build_conv_stack(input_channels=channels, conv_filters=model_config.conv_filters, activation=conv_activation)dueling
dueling = model_config.duelingfc
fc = nn.Sequential(*fc_layers)noisy_layers
noisy_layers: list[NoisyLinear] = [m for m in self.modules() if isinstance(m, NoisyLinear)]num_atoms
num_atoms = model_config.categorical.num_atomsv_max
v_max = model_config.categorical.v_maxv_min
v_min = model_config.categorical.v_minvalue_head
value_head = nn.Sequential(*val_layers)