class DNN::Callbacks::EarlyStopping

A callback to stop training the model early after test on batch. @param [Symbol] trigger A log that triggers early stopping.

Specify one of train_loss, test_loss, test_accuracy.

@param [Float] tolerance Tolerance value for early stopping.

Public Class Methods

new(trigger, tolerance) click to toggle source
# File lib/dnn/core/callbacks.rb, line 63
def initialize(trigger, tolerance)
  @trigger = trigger
  @tolerance = tolerance
end

Public Instance Methods

after_epoch() click to toggle source
# File lib/dnn/core/callbacks.rb, line 72
def after_epoch
  throw :stop, "Early stopped." if judge_early_stopping_test
end
after_train_on_batch() click to toggle source
# File lib/dnn/core/callbacks.rb, line 68
def after_train_on_batch
  throw :stop, "Early stopped." if judge_early_stopping_train
end

Private Instance Methods

judge_early_stopping_test() click to toggle source
# File lib/dnn/core/callbacks.rb, line 86
def judge_early_stopping_test
  case @trigger
  when :test_loss
    return true if model.last_log[@trigger] <= @tolerance
  when :test_accuracy
    return true if model.last_log[@trigger] >= @tolerance
  end
  false
end
judge_early_stopping_train() click to toggle source
# File lib/dnn/core/callbacks.rb, line 78
def judge_early_stopping_train
  case @trigger
  when :train_loss
    return true if model.last_log[@trigger] <= @tolerance
  end
  false
end