lib/svmkit/linear_model/ridge.rb in svmkit-0.4.0 vs lib/svmkit/linear_model/ridge.rb in svmkit-0.4.1

- old
+ new

@@ -37,10 +37,12 @@ # # @param reg_param [Float] The regularization parameter. # @param fit_bias [Boolean] The flag indicating whether to fit the bias term. # @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. + # 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, max_iter: 1000, batch_size: 10, optimizer: nil, random_seed: nil) check_params_float(reg_param: reg_param) check_params_integer(max_iter: max_iter, batch_size: batch_size) check_params_boolean(fit_bias: fit_bias) @@ -50,10 +52,11 @@ @params[:reg_param] = reg_param @params[:fit_bias] = fit_bias @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 @rng = Random.new(@params[:random_seed]) @@ -73,15 +76,11 @@ _n_samples, n_features = x.shape if n_outputs > 1 @weight_vec = Numo::DFloat.zeros(n_outputs, n_features) @bias_term = Numo::DFloat.zeros(n_outputs) - n_outputs.times do |n| - weight, bias = single_fit(x, y[true, n]) - @weight_vec[n, true] = weight - @bias_term[n] = bias - end + n_outputs.times { |n| @weight_vec[n, true], @bias_term[n] = single_fit(x, y[true, n]) } else @weight_vec, @bias_term = single_fit(x, y) end self @@ -122,10 +121,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)