#394Loss over time — sum the per-step lossesEasyRNNLoss Functions
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:
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: .
Examples
Example 1
Input: Y_hat = [[1], [2]], Y = [[1], [1]]
Output: 1.0
Explanation: step 0 loss , step 1 loss ; total .
Example 2 — multi-output step
Input: Y_hat = [[1, 1]], Y = [[0, 0]]
Output: 1.0
Explanation: mean of is .
Constraints
- Per timestep: mean of the squared errors over the
Ooutputs. - Total: sum those per-step losses over all
Ttimesteps. - 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