Class: Rumale::EvaluationMeasure::LogLoss

Inherits:
Object
  • Object
show all
Includes:
Base::Evaluator
Defined in:
rumale-evaluation_measure/lib/rumale/evaluation_measure/log_loss.rb

Overview

LogLoss is a class that calculates the logarithmic loss of predicted class probability.

Examples:

require 'rumale/evaluation_measure/log_loss'

evaluator = Rumale::EvaluationMeasure::LogLoss.new
puts evaluator.score(ground_truth, predicted)

Instance Method Summary collapse

Instance Method Details

#score(y_true, y_pred, eps = 1e-15) ⇒ Float

Calculate mean logarithmic loss. If both y_true and y_pred are array (both shapes are [n_samples]), this method calculates mean logarithmic loss for binary classification.

Parameters:

  • y_true (Numo::Int32)

    (shape: [n_samples]) Ground truth labels.

  • y_pred (Numo::DFloat)

    (shape: [n_samples, n_classes]) Predicted class probability.

  • eps (Float) (defaults to: 1e-15)

    A small value close to zero to avoid outputting infinity in logarithmic calcuation.

Returns:

  • (Float)

    mean logarithmic loss



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'rumale-evaluation_measure/lib/rumale/evaluation_measure/log_loss.rb', line 25

def score(y_true, y_pred, eps = 1e-15)
  n_samples, n_classes = y_pred.shape
  clipped_p = y_pred.clip(eps, 1 - eps)

  log_loss = if n_classes.nil?
               negative_label = y_true.to_a.uniq.min
               bin_y_true = Numo::DFloat.cast(y_true.ne(negative_label))
               -(bin_y_true * Numo::NMath.log(clipped_p) + (1 - bin_y_true) * Numo::NMath.log(1 - clipped_p))
             else
               binarized_y_true = binarize(y_true)
               clipped_p /= clipped_p.sum(axis: 1).expand_dims(1)
               -(binarized_y_true * Numo::NMath.log(clipped_p)).sum(axis: 1)
             end
  log_loss.sum / n_samples
end