Scientific ML Studio
Learn/ Studio Tour/ 3.5
3.5 · The blocks, one at a time

Balance the loss

The Loss block collects the equation and every condition into one number, with a weight on each part. The weights are the first thing to change when a run does not behave.

The Loss block is where the separate requirements of the problem become one objective. It gathers three things:

  • the Network (the unknown function);
  • one or more PDE residuals (what must hold inside);
  • every BC / IC condition (what must hold at the edges and at the start).

The first is a single wire; the other two accept many wires, so connect every condition to the same BC / IC input. Its output, Loss, goes to the Train block.

The objective it builds

Each piece gives a number: the mean square of its residual over its points. The Loss block adds them up with weights:

$$ \mathcal{L} \;=\; w_{\text{pde}}\,\mathcal{L}_{\text{pde}} \;+\; \sum_i w_{\text{bc},i}\,\mathcal{L}_{\text{bc},i} \;+\; \sum_j w_{\text{ic},j}\,\mathcal{L}_{\text{ic},j}. $$

This is the loss of the residual as a loss, with a weight in front of every term. The reason for the weights is that the terms are different quantities with different sizes, and nothing says they should be equal; why the weights matter explains it in the textbook.

The settings

Term norm. How each term's residual is reduced to one number: Mean square (the standard), Mean absolute, or Huber (more forgiving of a few very large residuals).

Weighting scheme. Currently only Fixed can be chosen: the weights you type stay constant for the whole run. Normalised by term magnitude and Gradient-norm balancing are shown as coming soon and are disabled. A notebook saved with one of them keeps it until you choose Fixed, and after you switch to Fixed you cannot choose an adaptive scheme from this menu again.

PDE weight. One number that multiplies every PDE residual term. Default 1.

BC weights and IC weights. One non-negative number for each condition wired into this Loss block, written as a list in square brackets: [1, 1, 10, 10].

  • The numbers are matched to the conditions in the order shown below the field, where the panel lists exactly which block each position refers to. They follow the notebook's BC 1, BC 2, … numbering and are not Domain boundary names.
  • [1, 1, 10, 10] makes the third and fourth conditions ten times heavier than the first two. 0 switches a term off.
  • For a problem with no initial condition, the IC list is [].

The Lab refuses to generate while a list is invalid, and says why. When the weights are wrong, the Generate code and Download .py buttons are disabled, and hovering over Generate code shows the reason. Typical messages are:

Message Cause
Expected 4 weights; received 3. The list does not have one entry per connected condition
Use square brackets: [1, 1, 10]. Missing brackets
Use a list such as [1, 1, 10]. Empty entries and trailing commas are not allowed. [1, , 10] or [1, 1, ]
Weight 2 must be a finite, nonnegative number. A negative number, text, or inf

Adding or removing a condition keeps the weights you already set for the others and gives the new one weight 1.

When to change a weight

Start with all weights at 1 and look at the loss curve that the generated script saves. If the PDE term and the boundary terms are very different in size, or the answer is wrong only near an edge, raise the weight on that edge's condition, by a factor of ten at a time. The same advice is worked through on a real problem in Poisson on a square.

Your screenshot · the Loss block's panel with PDE weight 1 and BC weights [1, 1, 1, 1], and the list below it naming each BC.

Next: the optimiser and the Train block.