PIXELBANKv9.1.0
Menu

Problem Statement

The REINFORCE objective maximizes sum_t log pi(a_t|s_t) * G_t. Implemented as a loss to minimize, it is the negative mean:

L=−1T∑t=1Tlog⁡π(at∣st) GtL = -\frac{1}{T}\sum_{t=1}^{T} \log \pi(a_t\mid s_t)\, G_t

Given aligned lists log_probs and returns, implement reinforce_loss(log_probs, returns) returning the scalar loss (float).

Example:

Input:
reinforce_loss([-0.7, -0.7], [1.0, 1.0])
Output:
0.7
Reasoning:
  • Identify the sequence length TT from the input lists, which is 22.
  • Compute the product of each log probability and its corresponding return: (−0.7×1.0)=−0.7(-0.7 \times 1.0) = -0.7 and (−0.7×1.0)=−0.7(-0.7 \times 1.0) = -0.7.
  • Sum these products to find the total weighted log probability: −0.7+(−0.7)=−1.4-0.7 + (-0.7) = -1.4.
  • Divide the total by the sequence length TT to calculate the mean: −1.4/2=−0.7-1.4 / 2 = -0.7.
  • Negate the mean to obtain the loss value, as the objective is to minimize the negative log-likelihood: −(−0.7)=0.7-(-0.7) = 0.7.
  • The final output is 0.7

Constraints:

  • len(log_probs) == len(returns), length >= 1.
  • Return the negative mean of the elementwise products.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
REINFORCE Loss Term - Medium | PixelBank