Class: Rumale::LinearModel::SGDClassifier
- Inherits:
-
SGDEstimator
- Object
- Base::Estimator
- BaseEstimator
- SGDEstimator
- Rumale::LinearModel::SGDClassifier
- Includes:
- Base::Classifier
- Defined in:
- rumale-linear_model/lib/rumale/linear_model/sgd_classifier.rb
Overview
SGDClassifier is a class that implements linear classifier with stochastic gradient descent optimization.
Reference
-
Shalev-Shwartz, S., and Singer, Y., “Pegasos: Primal Estimated sub-GrAdient SOlver for SVM,” Proc. ICML’07, pp. 807–814, 2007.
-
Tsuruoka, Y., Tsujii, J., and Ananiadou, S., “Stochastic Gradient Descent Training for L1-regularized Log-linear Models with Cumulative Penalty,” Proc. ACL’09, pp. 477–485, 2009.
-
Bottou, L., “Large-Scale Machine Learning with Stochastic Gradient Descent,” Proc. COMPSTAT’10, pp. 177–186, 2010.
Instance Attribute Summary collapse
-
#classes ⇒ Numo::Int32
readonly
Return the class labels.
-
#rng ⇒ Random
readonly
Return the random generator for performing random sampling.
Attributes inherited from BaseEstimator
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
-
#fit(x, y) ⇒ SGDClassifier
Fit the model with given training data.
-
#initialize(loss: 'hinge', learning_rate: 0.01, decay: nil, momentum: 0.9, penalty: 'l2', reg_param: 1.0, l1_ratio: 0.5, fit_bias: true, bias_scale: 1.0, max_iter: 1000, batch_size: 50, tol: 1e-4, n_jobs: nil, verbose: false, random_seed: nil) ⇒ SGDClassifier
constructor
Create a new linear classifier with stochastic gradient descent optimization.
-
#partial_fit(x, y) ⇒ SGDClassifier
Perform 1-epoch of stochastic gradient descent optimization with given training data.
-
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
-
#predict_proba(x) ⇒ Numo::DFloat
Predict probability for samples.
Methods included from Base::Classifier
Constructor Details
#initialize(loss: 'hinge', learning_rate: 0.01, decay: nil, momentum: 0.9, penalty: 'l2', reg_param: 1.0, l1_ratio: 0.5, fit_bias: true, bias_scale: 1.0, max_iter: 1000, batch_size: 50, tol: 1e-4, n_jobs: nil, verbose: false, random_seed: nil) ⇒ SGDClassifier
Create a new linear classifier with stochastic gradient descent optimization.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'rumale-linear_model/lib/rumale/linear_model/sgd_classifier.rb', line 63 def initialize(loss: 'hinge', learning_rate: 0.01, decay: nil, momentum: 0.9, penalty: 'l2', reg_param: 1.0, l1_ratio: 0.5, fit_bias: true, bias_scale: 1.0, max_iter: 1000, batch_size: 50, tol: 1e-4, n_jobs: nil, verbose: false, random_seed: nil) super() @params.merge!( loss: loss, learning_rate: learning_rate, decay: decay, momentum: momentum, penalty: penalty, reg_param: reg_param, l1_ratio: l1_ratio, fit_bias: fit_bias, bias_scale: bias_scale, max_iter: max_iter, batch_size: batch_size, tol: tol, n_jobs: n_jobs, verbose: verbose, random_seed: random_seed ) @params[:decay] ||= @params[:reg_param] * @params[:learning_rate] @params[:random_seed] ||= srand @rng = Random.new(@params[:random_seed]) @penalty_type = @params[:penalty] @loss_func = case @params[:loss] when Rumale::LinearModel::Loss::HingeLoss::NAME Rumale::LinearModel::Loss::HingeLoss.new when Rumale::LinearModel::Loss::LogLoss::NAME Rumale::LinearModel::Loss::LogLoss.new else raise ArgumentError, "given loss '#{loss}' is not supported." end end |
Instance Attribute Details
#classes ⇒ Numo::Int32 (readonly)
Return the class labels.
30 31 32 |
# File 'rumale-linear_model/lib/rumale/linear_model/sgd_classifier.rb', line 30 def classes @classes end |
#rng ⇒ Random (readonly)
Return the random generator for performing random sampling.
34 35 36 |
# File 'rumale-linear_model/lib/rumale/linear_model/sgd_classifier.rb', line 34 def rng @rng end |
Instance Method Details
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
147 148 149 150 151 |
# File 'rumale-linear_model/lib/rumale/linear_model/sgd_classifier.rb', line 147 def decision_function(x) x = ::Rumale::Validation.check_convert_sample_array(x) x.dot(@weight_vec.transpose) + @bias_term end |
#fit(x, y) ⇒ SGDClassifier
Fit the model with given training data.
105 106 107 108 109 110 111 112 113 114 115 |
# File 'rumale-linear_model/lib/rumale/linear_model/sgd_classifier.rb', line 105 def fit(x, y) x = Rumale::Validation.check_convert_sample_array(x) y = Rumale::Validation.check_convert_label_array(y) Rumale::Validation.check_sample_size(x, y) @classes = Numo::Int32[*y.to_a.uniq.sort] send(:"fit_#{@loss_func.name}", x, y) self end |
#partial_fit(x, y) ⇒ SGDClassifier
Perform 1-epoch of stochastic gradient descent optimization with given training data.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'rumale-linear_model/lib/rumale/linear_model/sgd_classifier.rb', line 122 def partial_fit(x, y) x = Rumale::Validation.check_convert_sample_array(x) y = Rumale::Validation.check_convert_label_array(y) Rumale::Validation.check_sample_size(x, y) n_features = x.shape[1] n_features += 1 if fit_bias? need_init = @weight.nil? || @weight.shape[0] != n_features @classes = Numo::Int32[*y.to_a.uniq.sort] if need_init negative_label = @classes[0] bin_y = Numo::Int32.cast(y.ne(negative_label)) * 2 - 1 @weight_vec, @bias_term = partial_fit_(x, bin_y, max_iter: 1, init: need_init) if @loss_func.name == Rumale::LinearModel::Loss::HingeLoss::NAME @prob_param = Rumale::ProbabilisticOutput.fit_sigmoid(x.dot(@weight_vec.transpose) + @bias_term, bin_y) end self end |
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
157 158 159 160 161 |
# File 'rumale-linear_model/lib/rumale/linear_model/sgd_classifier.rb', line 157 def predict(x) x = ::Rumale::Validation.check_convert_sample_array(x) send(:"predict_#{@loss_func.name}", x) end |
#predict_proba(x) ⇒ Numo::DFloat
Predict probability for samples.
167 168 169 170 171 |
# File 'rumale-linear_model/lib/rumale/linear_model/sgd_classifier.rb', line 167 def predict_proba(x) x = ::Rumale::Validation.check_convert_sample_array(x) send(:"predict_proba_#{@loss_func.name}", x) end |