Loss

dragon.operators.loss.NLLLoss(
   inputs,
   axis=1,
   reduction='VALID',
   ignore_labels=(),
   **kwargs
)

Compute the negative likelihood loss with sparse labels.

Type Constraints:

  • logits (float32)
  • labels (float32, int64)
Parameters:
  • inputs (sequence of Tensor) – The inputs, represent [logits, labels].
  • axis (int, optional) – The axis to apply softmax, can be negative.
  • reduction ({'NONE', 'SUM', 'MEAN', 'BATCH_SIZE', 'VALID'}, optional) – The reduction method.
  • ignore_labels (sequence of int, optional) – The index of labels to ignore.
Returns:

The loss.

Return type:

Tensor

dragon.operators.loss.SparseSoftmaxCrossEntropy(
   inputs,
   axis=1,
   reduction='VALID',
   ignore_labels=(),
   **kwargs
)

Compute the softmax cross entropy with sparse labels.

Type Constraints:

  • logits (float32)
  • labels (float32, int64)
Parameters:
  • inputs (sequence of Tensor) – The inputs, represent [logits, labels].
  • axis (int, optional) – The axis to apply softmax, can be negative.
  • reduction ({'NONE', 'SUM', 'MEAN', 'BATCH_SIZE', 'VALID'}, optional) – The reduction method.
  • ignore_labels (sequence of int, optional) – The index of labels to ignore.
Returns:

The loss.

Return type:

Tensor

dragon.operators.loss.SigmoidCrossEntropy(inputs, reduction='VALID', **kwargs)

Compute sigmoid cross entropy with given logits and targets.

Type Constraints: float32

Parameters:
  • inputs (sequence of Tensor) – The inputs, represent [logits, targets].
  • reduction ({'NONE', 'SUM', 'MEAN', 'BATCH_SIZE', 'VALID'}, optional) – The reduction method.
Returns:

The loss.

Return type:

Tensor

dragon.operators.loss.SoftmaxCrossEntropy(inputs, axis=1, reduction='MEAN', **kwargs)

Compute the softmax cross entropy with given logits and one-hot labels.

Type Constraints: float32

Parameters:
  • inputs (sequence of Tensor) – The inputs, represent [logits, labels].
  • axis (int, optional) – The axis to apply softmax, can be negative.
  • reduction ({'NONE', 'SUM', 'MEAN', 'BATCH_SIZE'}, optional) – The reduction method.
Returns:

The loss.

Return type:

Tensor

dragon.operators.loss.SmoothL1Loss(inputs, beta=1.0, reduction='BATCH_SIZE', **kwargs)

Compute the smoothed L1 loss. [Girshick, 2015].

Note that the beta is represented as \(\, \frac{1}{\sigma^{2}}\) following the original paper.

Type Constraints: float32

Parameters:
  • inputs (sequence of Tensor) – The inputs, represent [input, targets] + [inside_w] + [outside_w].
  • beta (float, optional) – The transition point from L1 to L2 loss
  • reduction ({'SUM', 'MEAN', 'BATCH_SIZE'}, optional) – The reduction method.
Returns:

The loss.

Return type:

Tensor

dragon.operators.loss.L1Loss(inputs, scale=1.0, reduction='BATCH_SIZE', **kwargs)

Compute the L1 loss.

Type Constraints: float32

Parameters:
  • inputs (sequence of Tensor) – The inputs, represent [x] + [targets] + [inside_w].
  • scale (float, optional) – The scale factor applying on the reduced loss.
  • reduction ({'SUM', 'MEAN', 'BATCH_SIZE'}, optional) – The reduction method.
Returns:

The loss, calculated as: \(\, Loss = \frac{ \sum \left| Weight * (Input - Target) \right|}{ Normalization}\)

Return type:

Tensor

dragon.operators.loss.L2Loss(inputs, scale=1.0, reduction='BATCH_SIZE', **kwargs)

Compute the L2 loss.

Type Constraints: float32

Parameters:
  • inputs (sequence of Tensor) – The inputs, represent [x] + [targets] + [inside_w].
  • scale (float, optional) – The scale factor applying on the reduced loss.
  • reduction ({'SUM', 'MEAN', 'BATCH_SIZE'}, optional) – The reduction method.
Returns:

The loss, calculated as: \(\, Loss = \frac{ \sum \frac{1}{2}\left|\left| Weight * (Input - Target) \right|\right|}{ Normalization}\)

Return type:

Tensor

dragon.operators.loss.SigmoidFocalLoss(
   inputs,
   axis=1,
   reduction='VALID',
   alpha=0.25,
   gamma=2.0,
   neg_id=0,
   **kwargs
)

Compute the sigmoid focal loss with sparse labels. [Lin et.al, 2017].

Type Constraints: float32

Parameters:
  • inputs (sequence of Tensor) – The inputs, represent [input, labels].
  • axis (int, optional) – The axis to apply softmax, can be negative.
  • reduction ({'NONE', 'SUM', 'MEAN', 'BATCH_SIZE', 'VALID'}, optional) – The reduction method.
  • alpha (float, optional, default=0.25) – The scale factor on the rare class.
  • gamma (float, optional, default=2.0) – The exponential decay factor on the easy examples.
  • neg_id (int, optional, default=0) – The negative id.
Returns:

The loss.

Return type:

Tensor

dragon.operators.loss.SoftmaxFocalLoss(
   inputs,
   axis=1,
   reduction='VALID',
   ignore_labels=(),
   alpha=0.25,
   gamma=2.0,
   neg_id=0,
   **kwargs
)

Compute the softmax focal loss with sparse labels. [Lin et.al, 2017].

Type Constraints: float32

Parameters:
  • inputs (sequence of Tensor) – The inputs, represent [input, labels].
  • axis (int, optional) – The axis to apply softmax, can be negative.
  • reduction ({'NONE', 'SUM', 'MEAN', 'BATCH_SIZE', 'VALID'}, optional) – The reduction method.
  • ignore_labels (sequence of int, optional) – The index of labels to ignore.
  • alpha (float, optional, default=0.25) – The scale factor on the rare class.
  • gamma (float, optional, default=2.0) – The exponential decay factor on the easy examples.
  • neg_id (int, optional, default=0) – The negative id.
Returns:

The loss.

Return type:

Tensor

dragon.operators.loss.CTCLoss(
   inputs,
   blank_first=True,
   padding_mask=-1,
   use_softmax=True,
   **kwargs
)

Compute the ctc loss with batched variable length of labels. [Graves & Gomez, 2006].

The data format of inputs should be [T, N, C].

If blank_first is True, 0 is reserved and

label values are between 1 and C - 1.

Otherwise, C - 1 is reserved and 0 to C - 2 can be used.

Type Constraints: float32

Parameters:
  • inputs (sequence of Tensor) – The inputs, represent [logits, labels].
  • blank_first (bool, optional) – Whether to put the blank at 0.
  • padding_mask (int, optional) – The mask for padding the redundant labels.
  • use_softmax (bool, optional) – Whether to use softmax before computing loss.
Returns:

The loss.

Return type:

Tensor

Notes

The magnitude of loss is related to the sequence length.