#394Loss over time — sum the per-step lossesEasy

Loss over time — sum the per-step losses

Background

Unlike a static model with one loss, an RNN produces an output at every timestep, so the training loss is the sum over time of the per-step losses:

Ltotal=t=1TL(y^t,yt)\mathcal{L}_{\text{total}} = \sum_{t=1}^{T} \mathcal{L}(\hat{\mathbf{y}}_t, \mathbf{y}_t)

Here each per-step loss is the mean squared error over the output dimensions.

Problem statement

Implement sequence_loss(Y_hat, Y) returning the total loss: for each timestep take the MSE over output units, then sum across timesteps.

Input

  • Y_hat — predictions, shape (T, O).
  • Y — targets, shape (T, O).

Output

Returns a Python float: tmeano((y^t,oyt,o)2)\sum_t \text{mean}_o\big((\hat{y}_{t,o}-y_{t,o})^2\big).

Examples

Example 1

Input:  Y_hat = [[1], [2]], Y = [[1], [1]]
Output: 1.0

Explanation: step 0 loss 00, step 1 loss (21)2=1(2-1)^2 = 1; total 11.

Example 2 — multi-output step

Input:  Y_hat = [[1, 1]], Y = [[0, 0]]
Output: 1.0

Explanation: mean of {1,1}\{1, 1\} is 11.

Constraints

  • Per timestep: mean of the squared errors over the O outputs.
  • Total: sum those per-step losses over all T timesteps.
  • Return a plain float.

Notes

  • Summing (rather than averaging) over time means longer sequences accrue more loss — the signal BPTT pushes back through every timestep.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Example -- one wrong step
  • Reference multi-output over two steps
  • Sample three timesteps