Apply one SGD step to policy and value heads on fixed features. Let X be features of shape (B,D), where B is batch size and D is feature width. With A actions, policy_weight Wp has shape (A,D) and policy_bias bp has shape (A,). value_weight wv has shape (D,); value_bias bv is a scalar tensor or has shape (1,). The heads are
p=softmax(XWpT+bp). v=tanh(Xwv+bv).Here p contains action probabilities, with softmax across actions, and v contains predicted values. For target_policies π of shape (B,A) and target_values z of shape (B,), the loss is
ℓ=−B1i=1∑Ba=1∑Aπialogpia+B1i=1∑B(vi−zi)2+λθ∈Θ∑∥θ∥22.Here i indexes examples, a indexes actions, λ is l2_coefficient, and Θ contains both weights and both biases; each squared norm sums all squared entries. Update all four parameters from the same original loss:
θ′=θ−η∇θℓ.Here η is learning_rate and θ′ is an updated parameter. Features and targets remain fixed. Return a five-item tuple of detached PyTorch float32 tensors: the scalar pre-update loss, updated policy_weight, policy_bias, value_weight, and value_bias. Preserve each parameter's shape and the input device.
Input: features=[[1,0]], policy_weight=[[0.2,-0.1],[-0.3,0.4]], policy_bias=[0.1,-0.2], value_weight=[0.5,-0.5], value_bias=[0], target_policies=[[1,0]], target_values=[1], learning_rate=0.1, l2_coefficient=0.01
Output: (0.668918, [[0.230603,-0.0998],[-0.330403,0.3992]], [0.130803,-0.230603], [0.583603,-0.499], [0.084603])
Explanation: The returned loss is measured before updating either head; both biases are included in the regularization.
Input: features=[[0,1],[1,1]], policy_weight=[[0,0],[0,0]], policy_bias=[0,0], value_weight=[0,0], value_bias=[0], target_policies=[[0.25,0.75],[0.5,0.5]], target_values=[-1,0.5], learning_rate=0, l2_coefficient=0
Output: (1.318147, [[0,0],[0,0]], [0,0], [0,0], [0])
Input: features=[[2]], policy_weight=[[0.5],[-0.5],[0.25]], policy_bias=[0,0,0], value_weight=[0.1], value_bias=0, target_policies=[[0.2,0.3,0.5]], target_values=[0], learning_rate=0.05, l2_coefficient=0.2
Output: (1.558414, [[0.45259],[-0.46777],[0.260179]], [-0.018705,0.011115,0.00759], [0.060063], -0.018969)
torch.log_softmax gives stable log probabilities across actions.
torch.autograd.grad can return gradients for a tuple of parameters.
Tensor.detach removes the returned tensor from the gradient graph.
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array, number
Accepts: array
Accepts: array
Accepts: number
Accepts: number
Apply one SGD step to policy and value heads on fixed features. Let X be features of shape (B,D), where B is batch size and D is feature width. With A actions, policy_weight Wp has shape (A,D) and policy_bias bp has shape (A,). value_weight wv has shape (D,); value_bias bv is a scalar tensor or has shape (1,). The heads are
p=softmax(XWpT+bp). v=tanh(Xwv+bv).Here p contains action probabilities, with softmax across actions, and v contains predicted values. For target_policies π of shape (B,A) and target_values z of shape (B,), the loss is
ℓ=−B1i=1∑Ba=1∑Aπialogpia+B1i=1∑B(vi−zi)2+λθ∈Θ∑∥θ∥22.Here i indexes examples, a indexes actions, λ is l2_coefficient, and Θ contains both weights and both biases; each squared norm sums all squared entries. Update all four parameters from the same original loss:
θ′=θ−η∇θℓ.Here η is learning_rate and θ′ is an updated parameter. Features and targets remain fixed. Return a five-item tuple of detached PyTorch float32 tensors: the scalar pre-update loss, updated policy_weight, policy_bias, value_weight, and value_bias. Preserve each parameter's shape and the input device.
Input: features=[[1,0]], policy_weight=[[0.2,-0.1],[-0.3,0.4]], policy_bias=[0.1,-0.2], value_weight=[0.5,-0.5], value_bias=[0], target_policies=[[1,0]], target_values=[1], learning_rate=0.1, l2_coefficient=0.01
Output: (0.668918, [[0.230603,-0.0998],[-0.330403,0.3992]], [0.130803,-0.230603], [0.583603,-0.499], [0.084603])
Explanation: The returned loss is measured before updating either head; both biases are included in the regularization.
Input: features=[[0,1],[1,1]], policy_weight=[[0,0],[0,0]], policy_bias=[0,0], value_weight=[0,0], value_bias=[0], target_policies=[[0.25,0.75],[0.5,0.5]], target_values=[-1,0.5], learning_rate=0, l2_coefficient=0
Output: (1.318147, [[0,0],[0,0]], [0,0], [0,0], [0])
Input: features=[[2]], policy_weight=[[0.5],[-0.5],[0.25]], policy_bias=[0,0,0], value_weight=[0.1], value_bias=0, target_policies=[[0.2,0.3,0.5]], target_values=[0], learning_rate=0.05, l2_coefficient=0.2
Output: (1.558414, [[0.45259],[-0.46777],[0.260179]], [-0.018705,0.011115,0.00759], [0.060063], -0.018969)
torch.log_softmax gives stable log probabilities across actions.
torch.autograd.grad can return gradients for a tuple of parameters.
Tensor.detach removes the returned tensor from the gradient graph.
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array, number
Accepts: array
Accepts: array
Accepts: number
Accepts: number