Class: Rumale::PolynomialModel::FactorizationMachineClassifier
- Inherits:
-
BaseFactorizationMachine
- Object
- BaseFactorizationMachine
- Rumale::PolynomialModel::FactorizationMachineClassifier
- Includes:
- Base::Classifier
- Defined in:
- lib/rumale/polynomial_model/factorization_machine_classifier.rb
Overview
FactorizationMachineClassifier is a class that implements Factorization Machine with stochastic gradient descent (SGD) optimization. For multiclass classification problem, it uses one-vs-the-rest strategy.
Reference
-
Rendle, “Factorization Machines with libFM,” ACM TIST, vol. 3 (3), pp. 57:1–57:22, 2012.
-
-
Rendle, “Factorization Machines,” Proc. ICDM’10, pp. 995–1000, 2010.
-
Instance Attribute Summary collapse
-
#bias_term ⇒ Numo::DFloat
readonly
Return the bias term for Factoriazation Machine.
-
#classes ⇒ Numo::Int32
readonly
Return the class labels.
-
#factor_mat ⇒ Numo::DFloat
readonly
Return the factor matrix for Factorization Machine.
-
#rng ⇒ Random
readonly
Return the random generator for random sampling.
-
#weight_vec ⇒ Numo::DFloat
readonly
Return the weight vector for Factorization Machine.
Attributes included from Base::BaseEstimator
Instance Method Summary collapse
-
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
-
#fit(x, y) ⇒ FactorizationMachineClassifier
Fit the model with given training data.
-
#initialize(n_factors: 2, loss: 'hinge', reg_param_linear: 1.0, reg_param_factor: 1.0, max_iter: 1000, batch_size: 10, optimizer: nil, random_seed: nil) ⇒ FactorizationMachineClassifier
constructor
Create a new classifier with Factorization Machine.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal 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(n_factors: 2, loss: 'hinge', reg_param_linear: 1.0, reg_param_factor: 1.0, max_iter: 1000, batch_size: 10, optimizer: nil, random_seed: nil) ⇒ FactorizationMachineClassifier
Create a new classifier with Factorization Machine.
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rumale/polynomial_model/factorization_machine_classifier.rb', line 58 def initialize(n_factors: 2, loss: 'hinge', reg_param_linear: 1.0, reg_param_factor: 1.0, max_iter: 1000, batch_size: 10, optimizer: nil, random_seed: nil) check_params_float(reg_param_linear: reg_param_linear, reg_param_factor: reg_param_factor) check_params_integer(n_factors: n_factors, max_iter: max_iter, batch_size: batch_size) check_params_string(loss: loss) check_params_type_or_nil(Integer, random_seed: random_seed) check_params_positive(n_factors: n_factors, reg_param_linear: reg_param_linear, reg_param_factor: reg_param_factor, max_iter: max_iter, batch_size: batch_size) super @classes = nil end |
Instance Attribute Details
#bias_term ⇒ Numo::DFloat (readonly)
Return the bias term for Factoriazation Machine.
37 38 39 |
# File 'lib/rumale/polynomial_model/factorization_machine_classifier.rb', line 37 def bias_term @bias_term end |
#classes ⇒ Numo::Int32 (readonly)
Return the class labels.
41 42 43 |
# File 'lib/rumale/polynomial_model/factorization_machine_classifier.rb', line 41 def classes @classes end |
#factor_mat ⇒ Numo::DFloat (readonly)
Return the factor matrix for Factorization Machine.
29 30 31 |
# File 'lib/rumale/polynomial_model/factorization_machine_classifier.rb', line 29 def factor_mat @factor_mat end |
#rng ⇒ Random (readonly)
Return the random generator for random sampling.
45 46 47 |
# File 'lib/rumale/polynomial_model/factorization_machine_classifier.rb', line 45 def rng @rng end |
#weight_vec ⇒ Numo::DFloat (readonly)
Return the weight vector for Factorization Machine.
33 34 35 |
# File 'lib/rumale/polynomial_model/factorization_machine_classifier.rb', line 33 def weight_vec @weight_vec end |
Instance Method Details
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rumale/polynomial_model/factorization_machine_classifier.rb', line 106 def decision_function(x) check_sample_array(x) linear_term = @bias_term + x.dot(@weight_vec.transpose) factor_term = if @classes.size <= 2 0.5 * (@factor_mat.dot(x.transpose)**2 - (@factor_mat**2).dot(x.transpose**2)).sum(0) else 0.5 * (@factor_mat.dot(x.transpose)**2 - (@factor_mat**2).dot(x.transpose**2)).sum(1).transpose end linear_term + factor_term end |
#fit(x, y) ⇒ FactorizationMachineClassifier
Fit the model with given training data.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rumale/polynomial_model/factorization_machine_classifier.rb', line 76 def fit(x, y) check_sample_array(x) check_label_array(y) check_sample_label_size(x, y) @classes = Numo::Int32[*y.to_a.uniq.sort] n_classes = @classes.size _n_samples, n_features = x.shape if n_classes > 2 @factor_mat = Numo::DFloat.zeros(n_classes, @params[:n_factors], n_features) @weight_vec = Numo::DFloat.zeros(n_classes, n_features) @bias_term = Numo::DFloat.zeros(n_classes) n_classes.times do |n| bin_y = Numo::Int32.cast(y.eq(@classes[n])) * 2 - 1 @factor_mat[n, true, true], @weight_vec[n, true], @bias_term[n] = partial_fit(x, bin_y) end else negative_label = y.to_a.uniq.min bin_y = Numo::Int32.cast(y.ne(negative_label)) * 2 - 1 @factor_mat, @weight_vec, @bias_term = partial_fit(x, bin_y) end self end |
#marshal_dump ⇒ Hash
Dump marshal data.
148 149 150 151 152 153 154 155 |
# File 'lib/rumale/polynomial_model/factorization_machine_classifier.rb', line 148 def marshal_dump { params: @params, factor_mat: @factor_mat, weight_vec: @weight_vec, bias_term: @bias_term, classes: @classes, rng: @rng } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
159 160 161 162 163 164 165 166 167 |
# File 'lib/rumale/polynomial_model/factorization_machine_classifier.rb', line 159 def marshal_load(obj) @params = obj[:params] @factor_mat = obj[:factor_mat] @weight_vec = obj[:weight_vec] @bias_term = obj[:bias_term] @classes = obj[:classes] @rng = obj[:rng] nil end |
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
121 122 123 124 125 126 127 128 |
# File 'lib/rumale/polynomial_model/factorization_machine_classifier.rb', line 121 def predict(x) check_sample_array(x) return Numo::Int32.cast(decision_function(x).ge(0.0)) * 2 - 1 if @classes.size <= 2 n_samples, = x.shape decision_values = decision_function(x) Numo::Int32.asarray(Array.new(n_samples) { |n| @classes[decision_values[n, true].max_index] }) end |
#predict_proba(x) ⇒ Numo::DFloat
Predict probability for samples.
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rumale/polynomial_model/factorization_machine_classifier.rb', line 134 def predict_proba(x) check_sample_array(x) proba = 1.0 / (Numo::NMath.exp(-decision_function(x)) + 1.0) return (proba.transpose / proba.sum(axis: 1)).transpose if @classes.size > 2 n_samples, = x.shape probs = Numo::DFloat.zeros(n_samples, 2) probs[true, 1] = proba probs[true, 0] = 1.0 - proba probs end |