rumale 0.19.0 → 0.20.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -29
- data/CHANGELOG.md +28 -0
- data/lib/rumale.rb +7 -10
- data/lib/rumale/clustering/hdbscan.rb +3 -3
- data/lib/rumale/clustering/k_means.rb +1 -1
- data/lib/rumale/clustering/k_medoids.rb +1 -1
- data/lib/rumale/clustering/mini_batch_k_means.rb +139 -0
- data/lib/rumale/dataset.rb +4 -4
- data/lib/rumale/decomposition/nmf.rb +2 -2
- data/lib/rumale/ensemble/random_forest_classifier.rb +1 -1
- data/lib/rumale/ensemble/random_forest_regressor.rb +1 -1
- data/lib/rumale/feature_extraction/feature_hasher.rb +1 -1
- data/lib/rumale/feature_extraction/hash_vectorizer.rb +1 -1
- data/lib/rumale/feature_extraction/tfidf_transformer.rb +113 -0
- data/lib/rumale/kernel_approximation/nystroem.rb +1 -1
- data/lib/rumale/kernel_machine/kernel_svc.rb +1 -1
- data/lib/rumale/linear_model/base_sgd.rb +1 -1
- data/lib/rumale/manifold/tsne.rb +1 -1
- data/lib/rumale/model_selection/cross_validation.rb +3 -2
- data/lib/rumale/model_selection/group_k_fold.rb +93 -0
- data/lib/rumale/model_selection/group_shuffle_split.rb +115 -0
- data/lib/rumale/model_selection/k_fold.rb +1 -1
- data/lib/rumale/model_selection/shuffle_split.rb +5 -5
- data/lib/rumale/model_selection/stratified_k_fold.rb +1 -1
- data/lib/rumale/model_selection/stratified_shuffle_split.rb +13 -9
- data/lib/rumale/multiclass/one_vs_rest_classifier.rb +2 -2
- data/lib/rumale/nearest_neighbors/vp_tree.rb +1 -1
- data/lib/rumale/neural_network/adam.rb +1 -1
- data/lib/rumale/neural_network/base_mlp.rb +1 -1
- data/lib/rumale/preprocessing/binarizer.rb +60 -0
- data/lib/rumale/preprocessing/l1_normalizer.rb +62 -0
- data/lib/rumale/preprocessing/l2_normalizer.rb +2 -1
- data/lib/rumale/preprocessing/max_normalizer.rb +62 -0
- data/lib/rumale/probabilistic_output.rb +1 -1
- data/lib/rumale/version.rb +1 -1
- metadata +12 -15
- data/lib/rumale/linear_model/base_linear_model.rb +0 -102
- data/lib/rumale/optimizer/ada_grad.rb +0 -42
- data/lib/rumale/optimizer/adam.rb +0 -56
- data/lib/rumale/optimizer/nadam.rb +0 -67
- data/lib/rumale/optimizer/rmsprop.rb +0 -50
- data/lib/rumale/optimizer/sgd.rb +0 -46
- data/lib/rumale/optimizer/yellow_fin.rb +0 -104
- data/lib/rumale/polynomial_model/base_factorization_machine.rb +0 -125
- data/lib/rumale/polynomial_model/factorization_machine_classifier.rb +0 -220
- data/lib/rumale/polynomial_model/factorization_machine_regressor.rb +0 -134
@@ -1,42 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rumale/validation'
|
4
|
-
require 'rumale/base/base_estimator'
|
5
|
-
|
6
|
-
module Rumale
|
7
|
-
module Optimizer
|
8
|
-
# AdaGrad is a class that implements AdaGrad optimizer.
|
9
|
-
#
|
10
|
-
# @deprecated AdaGrad will be deleted in version 0.20.0.
|
11
|
-
#
|
12
|
-
# *Reference*
|
13
|
-
# - Duchi, J., Hazan, E., and Singer, Y., "Adaptive Subgradient Methods for Online Learning and Stochastic Optimization," J. Machine Learning Research, vol. 12, pp. 2121--2159, 2011.
|
14
|
-
class AdaGrad
|
15
|
-
include Base::BaseEstimator
|
16
|
-
include Validation
|
17
|
-
|
18
|
-
# Create a new optimizer with AdaGrad.
|
19
|
-
#
|
20
|
-
# @param learning_rate [Float] The initial value of learning rate.
|
21
|
-
def initialize(learning_rate: 0.01)
|
22
|
-
warn 'warning: AdaGrad is deprecated. This class will be deleted in version 0.20.0.'
|
23
|
-
check_params_numeric(learning_rate: learning_rate)
|
24
|
-
check_params_positive(learning_rate: learning_rate)
|
25
|
-
@params = {}
|
26
|
-
@params[:learning_rate] = learning_rate
|
27
|
-
@moment = nil
|
28
|
-
end
|
29
|
-
|
30
|
-
# Calculate the updated weight with AdaGrad adaptive learning rate.
|
31
|
-
#
|
32
|
-
# @param weight [Numo::DFloat] (shape: [n_features]) The weight to be updated.
|
33
|
-
# @param gradient [Numo::DFloat] (shape: [n_features]) The gradient for updating the weight.
|
34
|
-
# @return [Numo::DFloat] (shape: [n_feautres]) The updated weight.
|
35
|
-
def call(weight, gradient)
|
36
|
-
@moment ||= Numo::DFloat.zeros(weight.shape[0])
|
37
|
-
@moment += gradient**2
|
38
|
-
weight - (@params[:learning_rate] / (@moment**0.5 + 1.0e-8)) * gradient
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rumale/validation'
|
4
|
-
require 'rumale/base/base_estimator'
|
5
|
-
|
6
|
-
module Rumale
|
7
|
-
module Optimizer
|
8
|
-
# Adam is a class that implements Adam optimizer.
|
9
|
-
#
|
10
|
-
# @deprecated Adam will be deleted in version 0.20.0.
|
11
|
-
#
|
12
|
-
# *Reference*
|
13
|
-
# - Kingma, D P., and Ba, J., "Adam: A Method for Stochastic Optimization," Proc. ICLR'15, 2015.
|
14
|
-
class Adam
|
15
|
-
include Base::BaseEstimator
|
16
|
-
include Validation
|
17
|
-
|
18
|
-
# Create a new optimizer with Adam
|
19
|
-
#
|
20
|
-
# @param learning_rate [Float] The initial value of learning rate.
|
21
|
-
# @param decay1 [Float] The smoothing parameter for the first moment.
|
22
|
-
# @param decay2 [Float] The smoothing parameter for the second moment.
|
23
|
-
def initialize(learning_rate: 0.001, decay1: 0.9, decay2: 0.999)
|
24
|
-
warn 'warning: Adam is deprecated. This class will be deleted in version 0.20.0.'
|
25
|
-
check_params_numeric(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
|
26
|
-
check_params_positive(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
|
27
|
-
@params = {}
|
28
|
-
@params[:learning_rate] = learning_rate
|
29
|
-
@params[:decay1] = decay1
|
30
|
-
@params[:decay2] = decay2
|
31
|
-
@fst_moment = nil
|
32
|
-
@sec_moment = nil
|
33
|
-
@iter = 0
|
34
|
-
end
|
35
|
-
|
36
|
-
# Calculate the updated weight with Nadam adaptive learning rate.
|
37
|
-
#
|
38
|
-
# @param weight [Numo::DFloat] (shape: [n_features]) The weight to be updated.
|
39
|
-
# @param gradient [Numo::DFloat] (shape: [n_features]) The gradient for updating the weight.
|
40
|
-
# @return [Numo::DFloat] (shape: [n_feautres]) The updated weight.
|
41
|
-
def call(weight, gradient)
|
42
|
-
@fst_moment ||= Numo::DFloat.zeros(weight.shape)
|
43
|
-
@sec_moment ||= Numo::DFloat.zeros(weight.shape)
|
44
|
-
|
45
|
-
@iter += 1
|
46
|
-
|
47
|
-
@fst_moment = @params[:decay1] * @fst_moment + (1.0 - @params[:decay1]) * gradient
|
48
|
-
@sec_moment = @params[:decay2] * @sec_moment + (1.0 - @params[:decay2]) * gradient**2
|
49
|
-
nm_fst_moment = @fst_moment / (1.0 - @params[:decay1]**@iter)
|
50
|
-
nm_sec_moment = @sec_moment / (1.0 - @params[:decay2]**@iter)
|
51
|
-
|
52
|
-
weight - @params[:learning_rate] * nm_fst_moment / (nm_sec_moment**0.5 + 1e-8)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rumale/validation'
|
4
|
-
require 'rumale/base/base_estimator'
|
5
|
-
|
6
|
-
module Rumale
|
7
|
-
# This module consists of the classes that implement optimizers adaptively tuning hyperparameters.
|
8
|
-
#
|
9
|
-
# @deprecated Optimizer module will be deleted in version 0.20.0.
|
10
|
-
module Optimizer
|
11
|
-
# Nadam is a class that implements Nadam optimizer.
|
12
|
-
#
|
13
|
-
# @deprecated Nadam will be deleted in version 0.20.0.
|
14
|
-
#
|
15
|
-
# *Reference*
|
16
|
-
# - Dozat, T., "Incorporating Nesterov Momentum into Adam," Tech. Repo. Stanford University, 2015.
|
17
|
-
class Nadam
|
18
|
-
include Base::BaseEstimator
|
19
|
-
include Validation
|
20
|
-
|
21
|
-
# Create a new optimizer with Nadam
|
22
|
-
#
|
23
|
-
# @param learning_rate [Float] The initial value of learning rate.
|
24
|
-
# @param decay1 [Float] The smoothing parameter for the first moment.
|
25
|
-
# @param decay2 [Float] The smoothing parameter for the second moment.
|
26
|
-
def initialize(learning_rate: 0.01, decay1: 0.9, decay2: 0.999)
|
27
|
-
warn 'warning: Nadam is deprecated. This class will be deleted in version 0.20.0.'
|
28
|
-
check_params_numeric(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
|
29
|
-
check_params_positive(learning_rate: learning_rate, decay1: decay1, decay2: decay2)
|
30
|
-
@params = {}
|
31
|
-
@params[:learning_rate] = learning_rate
|
32
|
-
@params[:decay1] = decay1
|
33
|
-
@params[:decay2] = decay2
|
34
|
-
@fst_moment = nil
|
35
|
-
@sec_moment = nil
|
36
|
-
@decay1_prod = 1.0
|
37
|
-
@iter = 0
|
38
|
-
end
|
39
|
-
|
40
|
-
# Calculate the updated weight with Nadam adaptive learning rate.
|
41
|
-
#
|
42
|
-
# @param weight [Numo::DFloat] (shape: [n_features]) The weight to be updated.
|
43
|
-
# @param gradient [Numo::DFloat] (shape: [n_features]) The gradient for updating the weight.
|
44
|
-
# @return [Numo::DFloat] (shape: [n_feautres]) The updated weight.
|
45
|
-
def call(weight, gradient)
|
46
|
-
@fst_moment ||= Numo::DFloat.zeros(weight.shape[0])
|
47
|
-
@sec_moment ||= Numo::DFloat.zeros(weight.shape[0])
|
48
|
-
|
49
|
-
@iter += 1
|
50
|
-
|
51
|
-
decay1_curr = @params[:decay1] * (1.0 - 0.5 * 0.96**(@iter * 0.004))
|
52
|
-
decay1_next = @params[:decay1] * (1.0 - 0.5 * 0.96**((@iter + 1) * 0.004))
|
53
|
-
decay1_prod_curr = @decay1_prod * decay1_curr
|
54
|
-
decay1_prod_next = @decay1_prod * decay1_curr * decay1_next
|
55
|
-
@decay1_prod = decay1_prod_curr
|
56
|
-
|
57
|
-
@fst_moment = @params[:decay1] * @fst_moment + (1.0 - @params[:decay1]) * gradient
|
58
|
-
@sec_moment = @params[:decay2] * @sec_moment + (1.0 - @params[:decay2]) * gradient**2
|
59
|
-
nm_gradient = gradient / (1.0 - decay1_prod_curr)
|
60
|
-
nm_fst_moment = @fst_moment / (1.0 - decay1_prod_next)
|
61
|
-
nm_sec_moment = @sec_moment / (1.0 - @params[:decay2]**@iter)
|
62
|
-
|
63
|
-
weight - (@params[:learning_rate] / (nm_sec_moment**0.5 + 1e-8)) * ((1 - decay1_curr) * nm_gradient + decay1_next * nm_fst_moment)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rumale/validation'
|
4
|
-
require 'rumale/base/base_estimator'
|
5
|
-
|
6
|
-
module Rumale
|
7
|
-
module Optimizer
|
8
|
-
# RMSProp is a class that implements RMSProp optimizer.
|
9
|
-
#
|
10
|
-
# @deprecated RMSProp will be deleted in version 0.20.0.
|
11
|
-
#
|
12
|
-
# *Reference*
|
13
|
-
# - Sutskever, I., Martens, J., Dahl, G., and Hinton, G., "On the importance of initialization and momentum in deep learning," Proc. ICML' 13, pp. 1139--1147, 2013.
|
14
|
-
# - Hinton, G., Srivastava, N., and Swersky, K., "Lecture 6e rmsprop," Neural Networks for Machine Learning, 2012.
|
15
|
-
class RMSProp
|
16
|
-
include Base::BaseEstimator
|
17
|
-
include Validation
|
18
|
-
|
19
|
-
# Create a new optimizer with RMSProp.
|
20
|
-
#
|
21
|
-
# @param learning_rate [Float] The initial value of learning rate.
|
22
|
-
# @param momentum [Float] The initial value of momentum.
|
23
|
-
# @param decay [Float] The smooting parameter.
|
24
|
-
def initialize(learning_rate: 0.01, momentum: 0.9, decay: 0.9)
|
25
|
-
warn 'warning: RMSProp is deprecated. This class will be deleted in version 0.20.0.'
|
26
|
-
check_params_numeric(learning_rate: learning_rate, momentum: momentum, decay: decay)
|
27
|
-
check_params_positive(learning_rate: learning_rate, momentum: momentum, decay: decay)
|
28
|
-
@params = {}
|
29
|
-
@params[:learning_rate] = learning_rate
|
30
|
-
@params[:momentum] = momentum
|
31
|
-
@params[:decay] = decay
|
32
|
-
@moment = nil
|
33
|
-
@update = nil
|
34
|
-
end
|
35
|
-
|
36
|
-
# Calculate the updated weight with RMSProp adaptive learning rate.
|
37
|
-
#
|
38
|
-
# @param weight [Numo::DFloat] (shape: [n_features]) The weight to be updated.
|
39
|
-
# @param gradient [Numo::DFloat] (shape: [n_features]) The gradient for updating the weight.
|
40
|
-
# @return [Numo::DFloat] (shape: [n_feautres]) The updated weight.
|
41
|
-
def call(weight, gradient)
|
42
|
-
@moment ||= Numo::DFloat.zeros(weight.shape[0])
|
43
|
-
@update ||= Numo::DFloat.zeros(weight.shape[0])
|
44
|
-
@moment = @params[:decay] * @moment + (1.0 - @params[:decay]) * gradient**2
|
45
|
-
@update = @params[:momentum] * @update - (@params[:learning_rate] / (@moment**0.5 + 1.0e-8)) * gradient
|
46
|
-
weight + @update
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
data/lib/rumale/optimizer/sgd.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rumale/validation'
|
4
|
-
require 'rumale/base/base_estimator'
|
5
|
-
|
6
|
-
module Rumale
|
7
|
-
module Optimizer
|
8
|
-
# SGD is a class that implements SGD optimizer.
|
9
|
-
#
|
10
|
-
# @deprecated SGD will be deleted in version 0.20.0.
|
11
|
-
class SGD
|
12
|
-
include Base::BaseEstimator
|
13
|
-
include Validation
|
14
|
-
|
15
|
-
# Create a new optimizer with SGD.
|
16
|
-
#
|
17
|
-
# @param learning_rate [Float] The initial value of learning rate.
|
18
|
-
# @param momentum [Float] The initial value of momentum.
|
19
|
-
# @param decay [Float] The smooting parameter.
|
20
|
-
def initialize(learning_rate: 0.01, momentum: 0.0, decay: 0.0)
|
21
|
-
warn 'warning: SGD is deprecated. This class will be deleted in version 0.20.0.'
|
22
|
-
check_params_numeric(learning_rate: learning_rate, momentum: momentum, decay: decay)
|
23
|
-
check_params_positive(learning_rate: learning_rate, momentum: momentum, decay: decay)
|
24
|
-
@params = {}
|
25
|
-
@params[:learning_rate] = learning_rate
|
26
|
-
@params[:momentum] = momentum
|
27
|
-
@params[:decay] = decay
|
28
|
-
@iter = 0
|
29
|
-
@update = nil
|
30
|
-
end
|
31
|
-
|
32
|
-
# Calculate the updated weight with SGD.
|
33
|
-
#
|
34
|
-
# @param weight [Numo::DFloat] (shape: [n_features]) The weight to be updated.
|
35
|
-
# @param gradient [Numo::DFloat] (shape: [n_features]) The gradient for updating the weight.
|
36
|
-
# @return [Numo::DFloat] (shape: [n_feautres]) The updated weight.
|
37
|
-
def call(weight, gradient)
|
38
|
-
@update ||= Numo::DFloat.zeros(weight.shape[0])
|
39
|
-
current_learning_rate = @params[:learning_rate] / (1.0 + @params[:decay] * @iter)
|
40
|
-
@iter += 1
|
41
|
-
@update = @params[:momentum] * @update - current_learning_rate * gradient
|
42
|
-
weight + @update
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
@@ -1,104 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rumale/validation'
|
4
|
-
require 'rumale/base/base_estimator'
|
5
|
-
|
6
|
-
module Rumale
|
7
|
-
module Optimizer
|
8
|
-
# YellowFin is a class that implements YellowFin optimizer.
|
9
|
-
#
|
10
|
-
# @deprecated YellowFin will be deleted in version 0.20.0.
|
11
|
-
#
|
12
|
-
# *Reference*
|
13
|
-
# - Zhang, J., and Mitliagkas, I., "YellowFin and the Art of Momentum Tuning," CoRR abs/1706.03471, 2017.
|
14
|
-
class YellowFin
|
15
|
-
include Base::BaseEstimator
|
16
|
-
include Validation
|
17
|
-
|
18
|
-
# Create a new optimizer with YellowFin.
|
19
|
-
#
|
20
|
-
# @param learning_rate [Float] The initial value of learning rate.
|
21
|
-
# @param momentum [Float] The initial value of momentum.
|
22
|
-
# @param decay [Float] The smooting parameter.
|
23
|
-
# @param window_width [Integer] The sliding window width for searching curvature range.
|
24
|
-
def initialize(learning_rate: 0.01, momentum: 0.9, decay: 0.999, window_width: 20)
|
25
|
-
warn 'warning: YellowFin is deprecated. This class will be deleted in version 0.20.0.'
|
26
|
-
check_params_numeric(learning_rate: learning_rate, momentum: momentum, decay: decay, window_width: window_width)
|
27
|
-
check_params_positive(learning_rate: learning_rate, momentum: momentum, decay: decay, window_width: window_width)
|
28
|
-
@params = {}
|
29
|
-
@params[:learning_rate] = learning_rate
|
30
|
-
@params[:momentum] = momentum
|
31
|
-
@params[:decay] = decay
|
32
|
-
@params[:window_width] = window_width
|
33
|
-
@smth_learning_rate = learning_rate
|
34
|
-
@smth_momentum = momentum
|
35
|
-
@grad_norms = nil
|
36
|
-
@grad_norm_min = 0.0
|
37
|
-
@grad_norm_max = 0.0
|
38
|
-
@grad_mean_sqr = 0.0
|
39
|
-
@grad_mean = 0.0
|
40
|
-
@grad_var = 0.0
|
41
|
-
@grad_norm_mean = 0.0
|
42
|
-
@curve_mean = 0.0
|
43
|
-
@distance_mean = 0.0
|
44
|
-
@update = nil
|
45
|
-
end
|
46
|
-
|
47
|
-
# Calculate the updated weight with adaptive momentum coefficient and learning rate.
|
48
|
-
#
|
49
|
-
# @param weight [Numo::DFloat] (shape: [n_features]) The weight to be updated.
|
50
|
-
# @param gradient [Numo::DFloat] (shape: [n_features]) The gradient for updating the weight.
|
51
|
-
# @return [Numo::DFloat] (shape: [n_feautres]) The updated weight.
|
52
|
-
def call(weight, gradient)
|
53
|
-
@update ||= Numo::DFloat.zeros(weight.shape[0])
|
54
|
-
curvature_range(gradient)
|
55
|
-
gradient_variance(gradient)
|
56
|
-
distance_to_optimum(gradient)
|
57
|
-
@smth_momentum = @params[:decay] * @smth_momentum + (1 - @params[:decay]) * current_momentum
|
58
|
-
@smth_learning_rate = @params[:decay] * @smth_learning_rate + (1 - @params[:decay]) * current_learning_rate
|
59
|
-
@update = @smth_momentum * @update - @smth_learning_rate * gradient
|
60
|
-
weight + @update
|
61
|
-
end
|
62
|
-
|
63
|
-
private
|
64
|
-
|
65
|
-
def current_momentum
|
66
|
-
dr = Math.sqrt(@grad_norm_max / @grad_norm_min + 1.0e-8)
|
67
|
-
[cubic_root**2, ((dr - 1) / (dr + 1))**2].max
|
68
|
-
end
|
69
|
-
|
70
|
-
def current_learning_rate
|
71
|
-
(1.0 - Math.sqrt(@params[:momentum]))**2 / (@grad_norm_min + 1.0e-8)
|
72
|
-
end
|
73
|
-
|
74
|
-
def cubic_root
|
75
|
-
p = (@distance_mean**2 * @grad_norm_min**2) / (2 * @grad_var + 1.0e-8)
|
76
|
-
w3 = (-Math.sqrt(p**2 + 4.fdiv(27) * p**3) - p).fdiv(2)
|
77
|
-
w = (w3 >= 0.0 ? 1 : -1) * w3.abs**1.fdiv(3)
|
78
|
-
y = w - p / (3 * w + 1.0e-8)
|
79
|
-
y + 1
|
80
|
-
end
|
81
|
-
|
82
|
-
def curvature_range(gradient)
|
83
|
-
@grad_norms ||= []
|
84
|
-
@grad_norms.push((gradient**2).sum)
|
85
|
-
@grad_norms.shift(@grad_norms.size - @params[:window_width]) if @grad_norms.size > @params[:window_width]
|
86
|
-
@grad_norm_min = @params[:decay] * @grad_norm_min + (1 - @params[:decay]) * @grad_norms.min
|
87
|
-
@grad_norm_max = @params[:decay] * @grad_norm_max + (1 - @params[:decay]) * @grad_norms.max
|
88
|
-
end
|
89
|
-
|
90
|
-
def gradient_variance(gradient)
|
91
|
-
@grad_mean_sqr = @params[:decay] * @grad_mean_sqr + (1 - @params[:decay]) * gradient**2
|
92
|
-
@grad_mean = @params[:decay] * @grad_mean + (1 - @params[:decay]) * gradient
|
93
|
-
@grad_var = (@grad_mean_sqr - @grad_mean**2).sum
|
94
|
-
end
|
95
|
-
|
96
|
-
def distance_to_optimum(gradient)
|
97
|
-
grad_sqr = (gradient**2).sum
|
98
|
-
@grad_norm_mean = @params[:decay] * @grad_norm_mean + (1 - @params[:decay]) * Math.sqrt(grad_sqr + 1.0e-8)
|
99
|
-
@curve_mean = @params[:decay] * @curve_mean + (1 - @params[:decay]) * grad_sqr
|
100
|
-
@distance_mean = @params[:decay] * @distance_mean + (1 - @params[:decay]) * (@grad_norm_mean / @curve_mean)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
@@ -1,125 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rumale/base/base_estimator'
|
4
|
-
require 'rumale/optimizer/nadam'
|
5
|
-
|
6
|
-
module Rumale
|
7
|
-
# This module consists of the classes that implement polynomial models.
|
8
|
-
#
|
9
|
-
# @deprecated PolynomialModel module will be deleted in version 0.20.0.
|
10
|
-
module PolynomialModel
|
11
|
-
# BaseFactorizationMachine is an abstract class for implementation of Factorization Machine-based estimators.
|
12
|
-
# This class is used internally.
|
13
|
-
#
|
14
|
-
# @deprecated BaseFactorizationMachine will be deleted in version 0.20.0.
|
15
|
-
class BaseFactorizationMachine
|
16
|
-
include Base::BaseEstimator
|
17
|
-
|
18
|
-
# Initialize a Factorization Machine-based estimator.
|
19
|
-
#
|
20
|
-
# @param n_factors [Integer] The maximum number of iterations.
|
21
|
-
# @param loss [String] The loss function ('hinge' or 'logistic' or nil).
|
22
|
-
# @param reg_param_linear [Float] The regularization parameter for linear model.
|
23
|
-
# @param reg_param_factor [Float] The regularization parameter for factor matrix.
|
24
|
-
# @param max_iter [Integer] The maximum number of epochs that indicates
|
25
|
-
# how many times the whole data is given to the training process.
|
26
|
-
# @param batch_size [Integer] The size of the mini batches.
|
27
|
-
# @param tol [Float] The tolerance of loss for terminating optimization.
|
28
|
-
# @param optimizer [Optimizer] The optimizer to calculate adaptive learning rate.
|
29
|
-
# If nil is given, Nadam is used.
|
30
|
-
# @param n_jobs [Integer] The number of jobs for running the fit and predict methods in parallel.
|
31
|
-
# If nil is given, the methods do not execute in parallel.
|
32
|
-
# If zero or less is given, it becomes equal to the number of processors.
|
33
|
-
# This parameter is ignored if the Parallel gem is not loaded.
|
34
|
-
# @param verbose [Boolean] The flag indicating whether to output loss during iteration.
|
35
|
-
# @param random_seed [Integer] The seed value using to initialize the random generator.
|
36
|
-
def initialize(n_factors: 2, loss: nil, reg_param_linear: 1.0, reg_param_factor: 1.0,
|
37
|
-
max_iter: 200, batch_size: 50, tol: 1e-4,
|
38
|
-
optimizer: nil, n_jobs: nil, verbose: false, random_seed: nil)
|
39
|
-
@params = {}
|
40
|
-
@params[:n_factors] = n_factors
|
41
|
-
@params[:loss] = loss unless loss.nil?
|
42
|
-
@params[:reg_param_linear] = reg_param_linear
|
43
|
-
@params[:reg_param_factor] = reg_param_factor
|
44
|
-
@params[:max_iter] = max_iter
|
45
|
-
@params[:batch_size] = batch_size
|
46
|
-
@params[:tol] = tol
|
47
|
-
@params[:optimizer] = optimizer
|
48
|
-
@params[:optimizer] ||= Optimizer::Nadam.new
|
49
|
-
@params[:n_jobs] = n_jobs
|
50
|
-
@params[:verbose] = verbose
|
51
|
-
@params[:random_seed] = random_seed
|
52
|
-
@params[:random_seed] ||= srand
|
53
|
-
@factor_mat = nil
|
54
|
-
@weight_vec = nil
|
55
|
-
@bias_term = nil
|
56
|
-
@rng = Random.new(@params[:random_seed])
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
def partial_fit(x, y)
|
62
|
-
# Initialize some variables.
|
63
|
-
class_name = self.class.to_s.split('::').last if @params[:verbose]
|
64
|
-
n_samples, n_features = x.shape
|
65
|
-
sub_rng = @rng.dup
|
66
|
-
weight_vec = Numo::DFloat.zeros(n_features + 1)
|
67
|
-
factor_mat = Rumale::Utils.rand_normal([@params[:n_factors], n_features], sub_rng)
|
68
|
-
weight_optimizer = @params[:optimizer].dup
|
69
|
-
factor_optimizers = Array.new(@params[:n_factors]) { @params[:optimizer].dup }
|
70
|
-
# Start optimization.
|
71
|
-
@params[:max_iter].times do |t|
|
72
|
-
sample_ids = [*0...n_samples]
|
73
|
-
sample_ids.shuffle!(random: sub_rng)
|
74
|
-
until (subset_ids = sample_ids.shift(@params[:batch_size])).empty?
|
75
|
-
# Sampling.
|
76
|
-
sub_x = x[subset_ids, true]
|
77
|
-
sub_y = y[subset_ids]
|
78
|
-
ex_sub_x = expand_feature(sub_x)
|
79
|
-
# Calculate gradients for loss function.
|
80
|
-
loss_grad = loss_gradient(sub_x, ex_sub_x, sub_y, factor_mat, weight_vec)
|
81
|
-
next if loss_grad.ne(0.0).count.zero?
|
82
|
-
|
83
|
-
# Update each parameter.
|
84
|
-
weight_vec = weight_optimizer.call(weight_vec, weight_gradient(loss_grad, ex_sub_x, weight_vec))
|
85
|
-
@params[:n_factors].times do |n|
|
86
|
-
factor_mat[n, true] = factor_optimizers[n].call(factor_mat[n, true],
|
87
|
-
factor_gradient(loss_grad, sub_x, factor_mat[n, true]))
|
88
|
-
end
|
89
|
-
end
|
90
|
-
loss = loss_func(x, expand_feature(x), y, factor_mat, weight_vec)
|
91
|
-
puts "[#{class_name}] Loss after #{t + 1} epochs: #{loss}" if @params[:verbose]
|
92
|
-
break if loss < @params[:tol]
|
93
|
-
end
|
94
|
-
[factor_mat, *split_weight_vec_bias(weight_vec)]
|
95
|
-
end
|
96
|
-
|
97
|
-
def loss_func(_x, _expanded_x, _y, _factor, _weight)
|
98
|
-
raise NotImplementedError, "#{__method__} has to be implemented in #{self.class}."
|
99
|
-
end
|
100
|
-
|
101
|
-
def loss_gradient(_x, _expanded_x, _y, _factor, _weight)
|
102
|
-
raise NotImplementedError, "#{__method__} has to be implemented in #{self.class}."
|
103
|
-
end
|
104
|
-
|
105
|
-
def weight_gradient(loss_grad, data, weight)
|
106
|
-
(loss_grad.expand_dims(1) * data).mean(0) + @params[:reg_param_linear] * weight
|
107
|
-
end
|
108
|
-
|
109
|
-
def factor_gradient(loss_grad, data, factor)
|
110
|
-
(loss_grad.expand_dims(1) * (data * data.dot(factor).expand_dims(1) - factor * (data**2))).mean(0) +
|
111
|
-
@params[:reg_param_factor] * factor
|
112
|
-
end
|
113
|
-
|
114
|
-
def expand_feature(x)
|
115
|
-
Numo::NArray.hstack([x, Numo::DFloat.ones([x.shape[0], 1])])
|
116
|
-
end
|
117
|
-
|
118
|
-
def split_weight_vec_bias(weight_vec)
|
119
|
-
weights = weight_vec[0...-1].dup
|
120
|
-
bias = weight_vec[-1]
|
121
|
-
[weights, bias]
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|