Class: Rumale::EvaluationMeasure::LogLoss

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

Overview

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

Examples:

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



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

def score(y_true, y_pred, eps = 1e-15)
  check_params_type(Numo::Int32, y_true: y_true)
  check_params_type(Numo::DFloat, y_pred: y_pred)

  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
               encoder = Rumale::Preprocessing::OneHotEncoder.new
               encoded_y_true = encoder.fit_transform(y_true)
               clipped_p /= clipped_p.sum(1).expand_dims(1)
               -(encoded_y_true * Numo::NMath.log(clipped_p)).sum(1)
             end
  log_loss.sum / n_samples
end