criterion

multitask_loss

class openchem.criterion.multitask_loss.MultitaskLoss(ignore_index, n_tasks)[source]

Bases: torch.nn.modules.loss._WeightedLoss

Creates a criterion that calculated binary cross-entropy loss over n_tasks tasks given input tensors input and target. Returns loss averaged across number of samples in every task and across n_tasks. It is useful when training a classification model with n_tasks separate binary classes.

The loss can be described as:

..math::

text{loss}(y, t) = -frac{1}{n_tasks}sum_{i=1}^{n_tasks}frac{1}{N_i} sum_{j=1}^{N_i} left(t[i, j]log(1-y[i, j]) + (1-t[i, j])log(1-y[i, j])right).

Parameters
  • ignore_index (int) – specifies a target value that is ignored and does not contribute to the gradient. For every task losses are averaged only across non-ignored targets.

  • n_tasks (int) – specifies number of tasks.

Shape:
-Input: :math: (N, n_tasks). Values should be in \([0, 1]\) range,

corresponding to probability of class :math:’1’.

-Target: :math: (N, n_tasks). Values should be binary: either

\(0\) or \(1\), corresponding to class labels.

-Output: scalar.

forward(input, target)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

reduction: str