graphnet.training.loss_functions module

Collection of loss functions.

All loss functions inherit from LossFunction which ensures a common syntax, handles per-event weights, etc.

class graphnet.training.loss_functions.LossFunction(*args, **kwargs)[source]

Bases: Model

Base class for loss functions in graphnet.

Construct LossFunction, saving model config.

Parameters:
  • args (Any)

  • kwargs (Any)

Return type:

object

forward(prediction, target, weights, return_elements)[source]

Forward pass for all loss functions.

Parameters:
  • prediction (Tensor) – Tensor containing predictions. Shape [N,P]

  • target (Tensor) – Tensor containing targets. Shape [N,T]

  • return_elements (bool, default: False) – Whether elementwise loss terms should be returned. The alternative is to return the averaged loss across examples.

  • weights (Tensor | None)

Return type:

Tensor

Returns:

Loss, either averaged to a scalar (if return_elements = False) or elementwise terms with shape [N,] (if return_elements = True).

class graphnet.training.loss_functions.MSELoss(*args, **kwargs)[source]

Bases: LossFunction

Mean squared error loss.

Construct LossFunction, saving model config.

Parameters:
  • args (Any)

  • kwargs (Any)

Return type:

object

class graphnet.training.loss_functions.RMSELoss(*args, **kwargs)[source]

Bases: MSELoss

Root mean squared error loss.

Construct LossFunction, saving model config.

Parameters:
  • args (Any)

  • kwargs (Any)

Return type:

object

class graphnet.training.loss_functions.LogCoshLoss(*args, **kwargs)[source]

Bases: LossFunction

Log-cosh loss function.

Acts like x^2 for small x; and like |x| for large x.

Construct LossFunction, saving model config.

Parameters:
  • args (Any)

  • kwargs (Any)

Return type:

object

class graphnet.training.loss_functions.CrossEntropyLoss(*args, **kwargs)[source]

Bases: LossFunction

Compute cross-entropy loss for classification tasks.

Predictions are an [N, num_class]-matrix of logits (i.e., non-softmax’ed probabilities), and targets are an [N,1]-matrix with integer values in (0, num_classes - 1).

Construct CrossEntropyLoss.

Parameters:
  • args (Any)

  • kwargs (Any)

Return type:

object

class graphnet.training.loss_functions.BinaryCrossEntropyLoss(*args, **kwargs)[source]

Bases: LossFunction

Compute binary cross entropy loss.

Predictions are vector probabilities (i.e., values between 0 and 1), and targets should be 0 and 1.

Construct LossFunction, saving model config.

Parameters:
  • args (Any)

  • kwargs (Any)

Return type:

object

class graphnet.training.loss_functions.LogCMK(*args, **kwargs)[source]

Bases: Function

MIT License.

Copyright (c) 2019 Max Ryabinin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. _____________________

From [https://github.com/mryab/vmf_loss/blob/master/losses.py] Modified to use modified Bessel function instead of exponentially scaled ditto (i.e. .ive -> .iv) as indiciated in [1812.04616] in spite of suggestion in Sec. 8.2 of this paper. The change has been validated through comparison with exact calculations for m=2 and m=3 and found to yield the correct results.

static forward(ctx, m, kappa)[source]

Forward pass.

Return type:

Tensor

Parameters:
  • ctx (Any)

  • m (int)

  • kappa (Tensor)

static backward(ctx, grad_output)[source]

Backward pass.

Return type:

Tensor

Parameters:
  • ctx (Any)

  • grad_output (Tensor)

class graphnet.training.loss_functions.VonMisesFisherLoss(*args, **kwargs)[source]

Bases: LossFunction

General class for calculating von Mises-Fisher loss.

Requires implementation for specific dimension m in which the target and prediction vectors need to be prepared.

Construct LossFunction, saving model config.

Parameters:
  • args (Any)

  • kwargs (Any)

Return type:

object

classmethod log_cmk_exact(m, kappa)[source]

Calculate $log C_{m}(k)$ term in von Mises-Fisher loss exactly.

Return type:

Tensor

Parameters:
  • m (int)

  • kappa (Tensor)

classmethod log_cmk_approx(m, kappa)[source]

Calculate $log C_{m}(k)$ term in von Mises-Fisher loss approx.

[https://arxiv.org/abs/1812.04616] Sec. 8.2 with additional minus sign.

Return type:

Tensor

Parameters:
  • m (int)

  • kappa (Tensor)

classmethod log_cmk(m, kappa, kappa_switch)[source]

Calculate $log C_{m}(k)$ term in von Mises-Fisher loss.

Since log_cmk_exact is diverges for kappa >~ 700 (using float64 precision), and since log_cmk_approx is unaccurate for small kappa, this method automatically switches between the two at kappa_switch, ensuring continuity at this point.

Return type:

Tensor

Parameters:
  • m (int)

  • kappa (Tensor)

  • kappa_switch (float)

class graphnet.training.loss_functions.VonMisesFisher2DLoss(*args, **kwargs)[source]

Bases: VonMisesFisherLoss

von Mises-Fisher loss function vectors in the 2D plane.

Construct LossFunction, saving model config.

Parameters:
  • args (Any)

  • kwargs (Any)

Return type:

object

class graphnet.training.loss_functions.EuclideanDistanceLoss(*args, **kwargs)[source]

Bases: LossFunction

Mean squared error in three dimensions.

Construct LossFunction, saving model config.

Parameters:
  • args (Any)

  • kwargs (Any)

Return type:

object

class graphnet.training.loss_functions.VonMisesFisher3DLoss(*args, **kwargs)[source]

Bases: VonMisesFisherLoss

von Mises-Fisher loss function vectors in the 3D plane.

Construct LossFunction, saving model config.

Parameters:
  • args (Any)

  • kwargs (Any)

Return type:

object

class graphnet.training.loss_functions.EnsembleLoss(*args, **kwargs)[source]

Bases: LossFunction

Chain multiple loss functions together.

Chain multiple loss functions together.

Optionally apply a weight to each loss function contribution.

E.g. Loss = RMSE*0.5 + LogCoshLoss*1.5

Parameters:
  • loss_functions (List[LossFunction]) – A list of loss functions to use. Each loss function contributes a term to the overall loss.

  • loss_factors (Optional[List[float]], default: None) – An optional list of factors that will be mulitplied

  • according (to each loss function contribution. Must be ordered)

  • given (to loss_functions. If not)

  • 1. (the weights default to)

  • prediction_keys (Optional[List[List[int]]], default: None) – An optional list of lists of indices for which prediction columns to use for each loss function. If not given, all columns are used for all loss functions.

  • args (Any)

  • kwargs (Any)

Return type:

object

class graphnet.training.loss_functions.RMSEVonMisesFisher3DLoss(*args, **kwargs)[source]

Bases: EnsembleLoss

Combine the VonMisesFisher3DLoss with RMSELoss.

VonMisesFisher3DLoss with a RMSE penality term.

The VonMisesFisher3DLoss will be weighted with vmfs_factor.

Parameters:
  • vmfs_factor (float, default: 0.05) – A factor applied to the VonMisesFisher3DLoss term.

  • 0.05. (Defaults ot)

  • args (Any)

  • kwargs (Any)

Return type:

object