Class: SVMKit::PolynomialModel::FactorizationMachineClassifier
- Inherits:
-
Object
- Object
- SVMKit::PolynomialModel::FactorizationMachineClassifier
- Includes:
- Base::BaseEstimator, Base::Classifier
- Defined in:
- lib/svmkit/polynomial_model/factorization_machine_classifier.rb
Overview
FactorizationMachineClassifier is a class that implements Fatorization Machine for binary classification with (mini-batch) stochastic gradient descent optimization. Note that this implementation uses hinge loss for the loss function.
Reference
-
Rendle, “Factorization Machines with libFM,” ACM Transactions on Intelligent Systems and Technology, vol. 3 (3), pp. 57:1–57:22, 2012.
-
-
Rendle, “Factorization Machines,” Proceedings of the 10th IEEE International Conference on Data Mining (ICDM’10), pp. 995–1000, 2010.
-
Instance Attribute Summary collapse
-
#bias_term ⇒ Float
readonly
Return the bias term for Factoriazation Machine.
-
#factor_mat ⇒ Numo::DFloat
readonly
Return the factor matrix for Factorization Machine.
-
#rng ⇒ Random
readonly
Return the random generator for transformation.
-
#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, reg_param_bias: 1.0, reg_param_weight: 1.0, reg_param_factor: 1.0, init_std: 0.1, max_iter: 1000, batch_size: 10, random_seed: nil) ⇒ FactorizationMachineClassifier
constructor
Create a new classifier with Support Vector Machine by the Pegasos algorithm.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
-
#score(x, y) ⇒ Float
Claculate the mean accuracy of the given testing data.
Constructor Details
#initialize(n_factors: 2, reg_param_bias: 1.0, reg_param_weight: 1.0, reg_param_factor: 1.0, init_std: 0.1, max_iter: 1000, batch_size: 10, random_seed: nil) ⇒ FactorizationMachineClassifier
Create a new classifier with Support Vector Machine by the Pegasos algorithm.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/svmkit/polynomial_model/factorization_machine_classifier.rb', line 53 def initialize(n_factors: 2, reg_param_bias: 1.0, reg_param_weight: 1.0, reg_param_factor: 1.0, init_std: 0.1, max_iter: 1000, batch_size: 10, random_seed: nil) @params = {} @params[:n_factors] = n_factors @params[:reg_param_bias] = reg_param_bias @params[:reg_param_weight] = reg_param_weight @params[:reg_param_factor] = reg_param_factor @params[:init_std] = init_std @params[:max_iter] = max_iter @params[:batch_size] = batch_size @params[:random_seed] = random_seed @params[:random_seed] ||= srand @factor_mat = nil @weight_vec = nil @bias_term = 0.0 @rng = Random.new(@params[:random_seed]) end |
Instance Attribute Details
#bias_term ⇒ Float (readonly)
Return the bias term for Factoriazation Machine.
37 38 39 |
# File 'lib/svmkit/polynomial_model/factorization_machine_classifier.rb', line 37 def bias_term @bias_term end |
#factor_mat ⇒ Numo::DFloat (readonly)
Return the factor matrix for Factorization Machine.
29 30 31 |
# File 'lib/svmkit/polynomial_model/factorization_machine_classifier.rb', line 29 def factor_mat @factor_mat end |
#rng ⇒ Random (readonly)
Return the random generator for transformation.
41 42 43 |
# File 'lib/svmkit/polynomial_model/factorization_machine_classifier.rb', line 41 def rng @rng end |
#weight_vec ⇒ Numo::DFloat (readonly)
Return the weight vector for Factorization Machine.
33 34 35 |
# File 'lib/svmkit/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.
111 112 113 114 115 |
# File 'lib/svmkit/polynomial_model/factorization_machine_classifier.rb', line 111 def decision_function(x) linear_term = @bias_term + x.dot(@weight_vec) factor_term = 0.5 * (@factor_mat.dot(x.transpose)**2 - (@factor_mat**2).dot(x.transpose**2)).sum 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 101 102 103 104 105 |
# File 'lib/svmkit/polynomial_model/factorization_machine_classifier.rb', line 76 def fit(x, y) # Generate binary labels. negative_label = y.to_a.uniq.sort.shift bin_y = y.map { |l| l != negative_label ? 1.0 : -1.0 } # Initialize some variables. n_samples, n_features = x.shape rand_ids = [*0...n_samples].shuffle(random: @rng) @factor_mat = rand_normal([@params[:n_factors], n_features], 0, @params[:init_std]) @weight_vec = Numo::DFloat.zeros(n_features) @bias_term = 0.0 # Start optimization. @params[:max_iter].times do |t| # Random sampling. subset_ids = rand_ids.shift(@params[:batch_size]) rand_ids.concat(subset_ids) data = x[subset_ids, true] label = bin_y[subset_ids] # Calculate gradients for loss function. loss_grad = loss_gradient(data, label) next if loss_grad.ne(0.0).count.zero? # Update each parameter. @bias_term -= learning_rate(@params[:reg_param_bias], t) * bias_gradient(loss_grad) @weight_vec -= learning_rate(@params[:reg_param_weight], t) * weight_gradient(loss_grad, data) @params[:n_factors].times do |n| @factor_mat[n, true] -= learning_rate(@params[:reg_param_factor], t) * factor_gradient(loss_grad, data, @factor_mat[n, true]) end end self end |
#marshal_dump ⇒ Hash
Dump marshal data.
138 139 140 |
# File 'lib/svmkit/polynomial_model/factorization_machine_classifier.rb', line 138 def marshal_dump { params: @params, factor_mat: @factor_mat, weight_vec: @weight_vec, bias_term: @bias_term, rng: @rng } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
144 145 146 147 148 149 150 151 |
# File 'lib/svmkit/polynomial_model/factorization_machine_classifier.rb', line 144 def marshal_load(obj) @params = obj[:params] @factor_mat = obj[:factor_mat] @weight_vec = obj[:weight_vec] @bias_term = obj[:bias_term] @rng = obj[:rng] nil end |
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
121 122 123 |
# File 'lib/svmkit/polynomial_model/factorization_machine_classifier.rb', line 121 def predict(x) Numo::Int32.cast(decision_function(x).map { |v| v >= 0.0 ? 1 : -1 }) end |
#score(x, y) ⇒ Float
Claculate the mean accuracy of the given testing data.
130 131 132 133 134 |
# File 'lib/svmkit/polynomial_model/factorization_machine_classifier.rb', line 130 def score(x, y) p = predict(x) n_hits = (y.to_a.map.with_index { |l, n| l == p[n] ? 1 : 0 }).inject(:+) n_hits / y.size.to_f end |