Loss Function Evaluation#

Parameters for the loss function are set in LossParameters.

class drdmannturb.LossAggregator(params: LossParameters, k1space: Tensor, zref: float, tb_log_dir: str | None = None, tb_comment: str = '')[source]#

Combines all loss functions and evaluates each term for the optimizer. The loss function for spectra fitting is determined by the following minimization problem:

minθ{MSE[θ]+αPen[θ]+βReg[θNN]}

where the loss terms are defined in the class’s methods. In the following, L is the number of data points fjD. The data Ji(fj) is evaluated using the Kaimal spectra. Note that the norm ||||D is defined as

gD2:=D|g(f)|2 d(logf).
Parameters:
  • params (LossParameters) – Dataclass with parameters that determine the loss function

  • k1space (torch.Tensor) – The spectra space for k1, this is assumed to be in logspace.

  • zref (float) – Reference velocity, needed for computing penalty derivatives wrt k1 z.

  • tb_log_dir (Optional[str]) – Logging directory for the TensorBoard logger. Conventions are those of TensorBoard, which by default result in the creation of a runs subdirectory where the script is being run if this parameter is left as None.

  • fn_comment (str) – Filename comment used by tensorboard; useful for distinguishing between architectures and hyperparameters. Refer to tensorboard documentation for examples of use. By default, the empty string, which results in default tensorboard filenames.

  • tb_comment (str) –

MSE_term(model: Tensor, target: Tensor, epoch: int) Tensor[source]#

Evaluates the loss term

MSE[θ]:=1Li=14j=1L(log|Ji(fj)|log|J~i(fj,θ)|)2,
Parameters:
  • model (torch.Tensor) – Model output of spectra on the k1space provided in the constructor.

  • target (torch.Tensor) – True spectra data on the k1space provided in the constructor.

  • epoch (int) – Epoch number, used for the TensorBoard loss writer.

Returns:

torch.Tensor – Evaluated MSE loss term.

Return type:

Tensor

Pen1stOrder(y: Tensor, epoch: int) Tensor[source]#

Evaluates the first-order penalization term, defined as

Pen1[θ]:=1|D|i=14ReLU(log|J~i(,θ)|logk1)D2.
Parameters:
  • y (torch.Tensor) – Model spectra output.

  • epoch (int) – Epoch number, used for the TensorBoard loss writer.

Returns:

torch.Tensor – 1st order penalty loss.

Return type:

Tensor

Pen2ndOrder(y: Tensor, epoch: int) Tensor[source]#

Evaluates he second-order penalization term, defined as

Pen2[θ]:=1|D|i=14ReLU(2log|J~i(,θ)|(logk1)2)D2,
Parameters:
  • y (torch.Tensor) – Model spectra output.

  • epoch (int) – Epoch number, used for the TensorBoard loss writer.

Returns:

torch.Tensor – 2nd order penalty loss term.

Return type:

Tensor

Regularization(theta_NN: Tensor, epoch: int) Tensor[source]#

Evaluates the regularization term, defined as

Reg[θNN]:=1Ni=1NθNN,i2.
Parameters:
  • theta_NN (torch.Tensor) – Neural network parameters.

  • epoch (int) – Epoch number, used for the TensorBoard loss writer.

Returns:

torch.Tensor – Regularization loss of neural network model.

Return type:

Tensor

eval(y: Tensor, y_data: Tensor, theta_NN: Tensor | None, epoch: int) Tensor[source]#

Evaluation method for computing the full loss term at a given epoch. This method sequentially evaluates each term in the loss and returns the sum total loss.

Parameters:
  • y (torch.Tensor) – Model spectra output on k1space in constructor.

  • y_data (torch.Tensor) – True spectra data on k1space in constructor.

  • theta_NN (Optional[torch.Tensor]) – Neural network parameters, used in the regularization loss term if activated. Can be set to None if no neural network is used.

  • epoch (int) – Epoch number, used for the TensorBoard loss writer.

Returns:

torch.Tensor – Evaluated total loss, as a weighted sum of all terms with non-zero hyperparameters.

Return type:

Tensor