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 _{\boldsymbol{\theta}}\left\{\operatorname{MSE}[\boldsymbol{\theta}]+\alpha \operatorname{Pen}[\boldsymbol{\theta}]+\beta \operatorname{Reg}\left[\boldsymbol{\theta}_{\mathrm{NN}}\right]\right\}\]where the loss terms are defined in the class’s methods. In the following, \(L\) is the number of data points \(f_j \in \mathcal{D}\). The data \(J_i (f_j)\) is evaluated using the Kaimal spectra. Note that the norm \(||\cdot||_{\mathcal{D}}\) is defined as
\[\|g\|_{\mathcal{D}}^2:=\int_{\mathcal{D}}|g(f)|^2 \mathrm{~d}(\log f).\]- Parameters:
params (
LossParameters
) – Dataclass with parameters that determine the loss functionk1space (
torch.Tensor
) – The spectra space for k1, this is assumed to be in logspace.zref (
float
) – Reference velocity, needed for computing penalty derivatives wrtk1 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 aruns
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
\[\operatorname{MSE}[\boldsymbol{\theta}]:=\frac{1}{L} \sum_{i=1}^4 \sum_{j=1}^L\left(\log \left|J_i\left(f_j\right)\right|-\log \left|\widetilde{J}_i\left(f_j, \boldsymbol{\theta}\right)\right|\right)^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
\[\operatorname{Pen}_1[\boldsymbol{\theta}]:=\frac{1}{|\mathcal{D}|} \sum_{i=1}^4\left\|\operatorname{ReLU}\left(\frac{\partial \log \left|\widetilde{J}_i(\cdot, \boldsymbol{\theta})\right|}{\partial \log k_1}\right)\right\|_{\mathcal{D}}^2.\]- 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
\[\operatorname{Pen}_2[\boldsymbol{\theta}]:=\frac{1}{|\mathcal{D}|} \sum_{i=1}^4\left\|\operatorname{ReLU}\left(\frac{\partial^2 \log \left|\widetilde{J}_i(\cdot, \boldsymbol{\theta})\right|}{\left(\partial \log k_1\right)^2}\right)\right\|_{\mathcal{D}}^2,\]- 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
\[\operatorname{Reg}\left[\boldsymbol{\theta}_{\mathrm{NN}}\right]:=\frac{1}{N} \sum_{i=1}^N \theta_{\mathrm{NN}, i}^2.\]- 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