squlearn.qnn.loss.CrossEntropyLoss

class squlearn.qnn.loss.CrossEntropyLoss(eps: float = 1e-08)

Cross entropy loss for classification.

Parameters:

eps (float) – Small value to avoid \(\log(0)\)

gradient(value_dict: dict, **kwargs) ndarray | tuple[ndarray, ndarray]

Returns the gradient of the cross entropy loss.

This function calculates the gradient of the cross entropy loss between the probability values in value_dict and binary ground_truth as

\[- \left(\sum_i w_i \cdot \left(\frac{y_i}{f\left(x_i\right)} - \frac{(1-y_i)}{1-f\left(x_i\right)}\right) \cdot \frac{\partial f\left(x_i\right)}{\partial \theta} \right)\]
Parameters:
  • value_dict (dict) – Contains calculated values of the model

  • ground_truth (np.ndarray) – The true values \(y\left(x_i\right)\)

  • weights (np.ndarray) – Weight for each data point, if None all data points count the same

  • multiple_output (bool) – True if the QNN has multiple outputs

Returns:

Gradient values

set_opt_param_op(opt_param_op: bool = True)

Sets the opt_param_op flag.

Parameters:

opt_param_op (bool) – True, if operator has trainable parameters

value(value_dict: dict, **kwargs) float

Calculates the cross entropy loss.

This function calculates the cross entropy loss between the probability values in value_dict and binary ground_truth as

\[- \left(\sum_i w_i \cdot \sum_k y_{i,k} \cdot \log\left(f\left(x_i\right)_k\right) \right)\]
Parameters:
  • value_dict (dict) – Contains calculated values of the model

  • ground_truth (np.ndarray) – The true values \(y\left(x_i\right)\)

  • weights (np.ndarray) – Weight for each data point, if None all data points count the same

Returns:

Loss value

variance(value_dict: dict, **kwargs) float

Calculates and returns the variance of the loss value.