lib/svmkit/linear_model/logistic_regression.rb in svmkit-0.4.0 vs lib/svmkit/linear_model/logistic_regression.rb in svmkit-0.4.1
- old
+ new
@@ -47,11 +47,11 @@
# @param bias_scale [Float] The scale of the bias term.
# If fit_bias is true, the feature vector v becoms [v; bias_scale].
# @param max_iter [Integer] The maximum number of iterations.
# @param batch_size [Integer] The size of the mini batches.
# @param optimizer [Optimizer] The optimizer to calculate adaptive learning rate.
- # Nadam is selected automatically on current version.
+ # If nil is given, Nadam is used.
# @param random_seed [Integer] The seed value using to initialize the random generator.
def initialize(reg_param: 1.0, fit_bias: false, bias_scale: 1.0,
max_iter: 1000, batch_size: 20, optimizer: nil, random_seed: nil)
check_params_float(reg_param: reg_param, bias_scale: bias_scale)
check_params_integer(max_iter: max_iter, batch_size: batch_size)
@@ -63,10 +63,11 @@
@params[:fit_bias] = fit_bias
@params[:bias_scale] = bias_scale
@params[:max_iter] = max_iter
@params[:batch_size] = batch_size
@params[:optimizer] = optimizer
+ @params[:optimizer] ||= Optimizer::Nadam.new
@params[:random_seed] = random_seed
@params[:random_seed] ||= srand
@weight_vec = nil
@bias_term = nil
@classes = nil
@@ -173,10 +174,10 @@
samples = @params[:fit_bias] ? expand_feature(x) : x
# Initialize some variables.
n_samples, n_features = samples.shape
rand_ids = [*0...n_samples].shuffle(random: @rng)
weight_vec = Numo::DFloat.zeros(n_features)
- optimizer = Optimizer::Nadam.new
+ optimizer = @params[:optimizer].dup
# Start optimization.
@params[:max_iter].times do |_t|
# random sampling
subset_ids = rand_ids.shift(@params[:batch_size])
rand_ids.concat(subset_ids)