CrossEntropyLoss#

class CrossEntropyLoss[source]#

Bases: Loss

This class computes the cross entropy loss for each sample as:

\[\text{CrossEntropyLoss}(predict, target) = -\sum_{i=0}^{N_{\text{classes}}} target_i * log(predict_i).\]

Methods

evaluate(predict, target)[source]#

An abstract method for evaluating the loss function. Inputs are expected in a shape of (N, *). Where N is a number of samples. Loss is computed for each sample individually.

Parameters:
  • predict (ndarray) -- an array of predicted values using the model.

  • target (ndarray) -- an array of the true values.

Returns:

An array with values of the loss function of the shape (N, 1).

Raises:

QiskitMachineLearningError -- shapes of predict and target do not match

Return type:

ndarray

gradient(predict, target)[source]#

Assume softmax is used, and target vector may or may not be one-hot encoding

Return type:

ndarray