Loss Functions

Losses

  • Computes the (element-wise) binary cross entropy loss on the given and expected probabilities and uses the mean as a reduction. expected and predicted are assumed to be in the interval (0, 1).

    The binary cross entropy loss is defined as

    -expected * log(predicted) - (1 - expected) * log(1 - predicted)
    

    Declaration

    Swift

    public func binaryCrossEntropy<Element, Device>(expected: Tensor<Element, Device>, actual: Tensor<Element, Device>) -> Tensor<Element, Device> where Element : NumericType, Device : DeviceType

    Parameters

    expected

    Expected values

    actual

    Predicted values

    ignoreIndex

    Value in expected, which is ignored.

    Return Value

    Loss, scalar value

  • Computes the categorical cross entropy loss on the given expected probabilities and the expected labels and uses the mean as a reduction. predicted values are assumed to be in the interval (0, 1)

    The categorical cross entropy loss is defined as

    -log(predicted[expected])
    

    Declaration

    Swift

    public func categoricalCrossEntropy<Element, Device>(expected: Tensor<Int32, Device>, actual: Tensor<Element, Device>, ignoreIndex: Int32 = -1) -> Tensor<Element, Device> where Element : NumericType, Device : DeviceType

    Parameters

    expected

    Expected labels

    actual

    Predicted values

    ignoreIndex

    Value in expected, which is ignored.

    Return Value

    Loss, scalar value

  • Computes the categorical negative log likelihood (NLL) loss on the given expected probabilities and the expected labels and uses the mean as a reduction. Predicted values are assumed to be in the interval (-infinity, 0).

    NLL loss should be used in conjunction with logSoftmax.

    The categorical NLL loss is defined as

    -predicted[expected]
    

    Declaration

    Swift

    public func categoricalNegativeLogLikelihood<Element, Device>(expected: Tensor<Int32, Device>, actual: Tensor<Element, Device>, ignoreIndex: Int32 = -1) -> Tensor<Element, Device> where Element : NumericType, Device : DeviceType

    Parameters

    expected

    Expected labels

    actual

    Predicted values

    Return Value

    Loss, scalar value

  • Computes the element-wise mean squared error between the given predicted and expected values

    Declaration

    Swift

    public func meanSquaredError<Element, Device>(expected: Tensor<Element, Device>, actual: Tensor<Element, Device>) -> Tensor<Element, Device> where Element : NumericType, Device : DeviceType

    Parameters

    expected

    Expected values

    actual

    Predicted values

  • Computes the L1 loss of the given tensor.

    Declaration

    Swift

    public func l1loss<Element, Device>(_ vector: Tensor<Element, Device>, loss: Element) -> Tensor<Element, Device> where Element : NumericType, Device : DeviceType

    Parameters

    vector

    Tensor to apply weight decay on

    loss

    Weight decay importance scaling factor

  • Computes the L2 loss of the given tensor.

    Declaration

    Swift

    public func l2loss<Element, Device>(_ vector: Tensor<Element, Device>, loss: Element) -> Tensor<Element, Device> where Element : NumericType, Device : DeviceType

    Parameters

    vector

    Tensor to apply weight decay on

    loss

    Weight decay importance scaling factor