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

- old
+ new

@@ -42,11 +42,11 @@ # @param bias_scale [Float] The scale of the bias term. # @param epsilon [Float] The margin of tolerance. # @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, epsilon: 0.1, max_iter: 1000, batch_size: 20, optimizer: nil, random_seed: nil) check_params_float(reg_param: reg_param, bias_scale: bias_scale, epsilon: epsilon) check_params_integer(max_iter: max_iter, batch_size: batch_size) @@ -60,10 +60,11 @@ @params[:bias_scale] = bias_scale @params[:epsilon] = epsilon @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]) @@ -83,15 +84,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 @@ -132,10 +129,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)