TensorTonic
Study Plans
Study Plan
Apply One Policy-Value Head Training Step
Reinforcement LearningNeural NetworksOptimization
Hard

Apply one SGD step to policy and value heads on fixed features. Let XXX be features of shape (B,D)(B,D)(B,D), where BBB is batch size and DDD is feature width. With AAA actions, policy_weight WpW_pWp​ has shape (A,D)(A,D)(A,D) and policy_bias bpb_pbp​ has shape (A,)(A,)(A,). value_weight wvw_vwv​ has shape (D,)(D,)(D,); value_bias bvb_vbv​ is a scalar tensor or has shape (1,)(1,)(1,). The heads are

p=softmax⁡(XWpT+bp).p=\operatorname{softmax}(XW_p^{\mathsf T}+b_p).p=softmax(XWpT​+bp​). v=tanh⁡(Xwv+bv).v=\tanh(Xw_v+b_v).v=tanh(Xwv​+bv​).

Here ppp contains action probabilities, with softmax across actions, and vvv contains predicted values. For target_policies π\piπ of shape (B,A)(B,A)(B,A) and target_values zzz of shape (B,)(B,)(B,), the loss is

ℓ=−1B∑i=1B∑a=1Aπialog⁡pia+1B∑i=1B(vi−zi)2+λ∑θ∈Θ∥θ∥22.\ell=-\frac{1}{B}\sum_{i=1}^{B}\sum_{a=1}^{A}\pi_{ia}\log p_{ia}+\frac{1}{B}\sum_{i=1}^{B}(v_i-z_i)^2+\lambda\sum_{\theta\in\Theta}\lVert\theta\rVert_2^2.ℓ=−B1​i=1∑B​a=1∑A​πia​logpia​+B1​i=1∑B​(vi​−zi​)2+λθ∈Θ∑​∥θ∥22​.

Here iii indexes examples, aaa indexes actions, λ\lambdaλ is l2_coefficient, and Θ\ThetaΘ contains both weights and both biases; each squared norm sums all squared entries. Update all four parameters from the same original loss:

θ′=θ−η∇θℓ.\theta'=\theta-\eta\nabla_{\theta}\ell.θ′=θ−η∇θ​ℓ.

Here η\etaη is learning_rate and θ′\theta'θ′ 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.

Loading visualization...

Examples

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)

Hint 1

torch.log_softmax gives stable log probabilities across actions.

Hint 2

torch.autograd.grad can return gradients for a tuple of parameters.

Hint 3

Tensor.detach removes the returned tensor from the gradient graph.

Requirements

  • Use PyTorch autograd and float32 calculations.
  • Include both weights and both biases in regularization.
  • Leave every input unchanged and return tensors detached from the gradient graph.

Constraints

  • All tensor inputs are finite float32 tensors on the same device.
  • B, D, and A are positive; tensor shapes follow the description.
  • Each target-policy row is nonnegative and sums to one.
  • Target values lie between -1 and 1.
  • learning_rate and l2_coefficient are finite and nonnegative.
Try Similar Problems
Assemble a Compact Dual-Head NetworkHardRun Deterministic Neural-Guided MCTSHardCompute the Policy and Value HeadsMediumApply an AlphaGo Residual BlockMediumTrain a Tiny Micrograd MLPHardApply a Dihedral Go SymmetryMediumGradient Clipping (Global Norm)MediumSample a Recent Self-Play Replay BatchEasyGRPO Group-Relative AdvantageHardDropout from ScratchHardPPO with Reference-Policy KLHardBradley-Terry Reward Model LossMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array, number

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.

Apply One Policy-Value Head Training Step

Reinforcement LearningNeural NetworksOptimization
Hard

Apply one SGD step to policy and value heads on fixed features. Let XXX be features of shape (B,D)(B,D)(B,D), where BBB is batch size and DDD is feature width. With AAA actions, policy_weight WpW_pWp​ has shape (A,D)(A,D)(A,D) and policy_bias bpb_pbp​ has shape (A,)(A,)(A,). value_weight wvw_vwv​ has shape (D,)(D,)(D,); value_bias bvb_vbv​ is a scalar tensor or has shape (1,)(1,)(1,). The heads are

p=softmax⁡(XWpT+bp).p=\operatorname{softmax}(XW_p^{\mathsf T}+b_p).p=softmax(XWpT​+bp​). v=tanh⁡(Xwv+bv).v=\tanh(Xw_v+b_v).v=tanh(Xwv​+bv​).

Here ppp contains action probabilities, with softmax across actions, and vvv contains predicted values. For target_policies π\piπ of shape (B,A)(B,A)(B,A) and target_values zzz of shape (B,)(B,)(B,), the loss is

ℓ=−1B∑i=1B∑a=1Aπialog⁡pia+1B∑i=1B(vi−zi)2+λ∑θ∈Θ∥θ∥22.\ell=-\frac{1}{B}\sum_{i=1}^{B}\sum_{a=1}^{A}\pi_{ia}\log p_{ia}+\frac{1}{B}\sum_{i=1}^{B}(v_i-z_i)^2+\lambda\sum_{\theta\in\Theta}\lVert\theta\rVert_2^2.ℓ=−B1​i=1∑B​a=1∑A​πia​logpia​+B1​i=1∑B​(vi​−zi​)2+λθ∈Θ∑​∥θ∥22​.

Here iii indexes examples, aaa indexes actions, λ\lambdaλ is l2_coefficient, and Θ\ThetaΘ contains both weights and both biases; each squared norm sums all squared entries. Update all four parameters from the same original loss:

θ′=θ−η∇θℓ.\theta'=\theta-\eta\nabla_{\theta}\ell.θ′=θ−η∇θ​ℓ.

Here η\etaη is learning_rate and θ′\theta'θ′ 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.

Loading visualization...

Examples

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)

Hint 1

torch.log_softmax gives stable log probabilities across actions.

Hint 2

torch.autograd.grad can return gradients for a tuple of parameters.

Hint 3

Tensor.detach removes the returned tensor from the gradient graph.

Requirements

  • Use PyTorch autograd and float32 calculations.
  • Include both weights and both biases in regularization.
  • Leave every input unchanged and return tensors detached from the gradient graph.

Constraints

  • All tensor inputs are finite float32 tensors on the same device.
  • B, D, and A are positive; tensor shapes follow the description.
  • Each target-policy row is nonnegative and sums to one.
  • Target values lie between -1 and 1.
  • learning_rate and l2_coefficient are finite and nonnegative.
Try Similar Problems
Assemble a Compact Dual-Head NetworkHardRun Deterministic Neural-Guided MCTSHardCompute the Policy and Value HeadsMediumApply an AlphaGo Residual BlockMediumTrain a Tiny Micrograd MLPHardApply a Dihedral Go SymmetryMediumGradient Clipping (Global Norm)MediumSample a Recent Self-Play Replay BatchEasyGRPO Group-Relative AdvantageHardDropout from ScratchHardPPO with Reference-Policy KLHardBradley-Terry Reward Model LossMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: array, number

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.