Class: Rumale::MetricLearning::MLKR
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::MetricLearning::MLKR
- Includes:
- Base::Transformer
- Defined in:
- rumale-metric_learning/lib/rumale/metric_learning/mlkr.rb
Overview
MLKR is a class that implements Metric Learning for Kernel Regression.
Reference
-
Weinberger, K. Q. and Tesauro, G., “Metric Learning for Kernel Regression,” Proc. AISTATS’07, pp. 612–629, 2007.
Instance Attribute Summary collapse
-
#components ⇒ Numo::DFloat
readonly
Returns the metric components.
-
#n_iter ⇒ Integer
readonly
Return the number of iterations run for optimization.
-
#rng ⇒ Random
readonly
Return the random generator.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#fit(x, y) ⇒ MLKR
Fit the model with given training data.
-
#fit_transform(x, y) ⇒ Numo::DFloat
Fit the model with training data, and then transform them with the learned model.
-
#initialize(n_components: nil, init: 'random', max_iter: 100, tol: 1e-6, verbose: false, random_seed: nil) ⇒ MLKR
constructor
Create a new transformer with MLKR.
-
#transform(x) ⇒ Numo::DFloat
Transform the given data with the learned model.
Constructor Details
#initialize(n_components: nil, init: 'random', max_iter: 100, tol: 1e-6, verbose: false, random_seed: nil) ⇒ MLKR
Create a new transformer with MLKR.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'rumale-metric_learning/lib/rumale/metric_learning/mlkr.rb', line 50 def initialize(n_components: nil, init: 'random', max_iter: 100, tol: 1e-6, verbose: false, random_seed: nil) super() @params = { n_components: n_components, init: init, max_iter: max_iter, tol: tol, verbose: verbose, random_seed: random_seed || srand } @rng = Random.new(@params[:random_seed]) end |
Instance Attribute Details
#components ⇒ Numo::DFloat (readonly)
Returns the metric components.
30 31 32 |
# File 'rumale-metric_learning/lib/rumale/metric_learning/mlkr.rb', line 30 def components @components end |
#n_iter ⇒ Integer (readonly)
Return the number of iterations run for optimization
34 35 36 |
# File 'rumale-metric_learning/lib/rumale/metric_learning/mlkr.rb', line 34 def n_iter @n_iter end |
#rng ⇒ Random (readonly)
Return the random generator.
38 39 40 |
# File 'rumale-metric_learning/lib/rumale/metric_learning/mlkr.rb', line 38 def rng @rng end |
Instance Method Details
#fit(x, y) ⇒ MLKR
Fit the model with given training data.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'rumale-metric_learning/lib/rumale/metric_learning/mlkr.rb', line 68 def fit(x, y) x = ::Rumale::Validation.check_convert_sample_array(x) y = ::Rumale::Validation.check_convert_target_value_array(y) ::Rumale::Validation.check_sample_size(x, y) n_features = x.shape[1] n_components = if @params[:n_components].nil? n_features else [n_features, @params[:n_components]].min end @components, @n_iter = optimize_components(x, y, n_features, n_components) @prototypes = x.dot(@components.transpose) @values = y self end |
#fit_transform(x, y) ⇒ Numo::DFloat
Fit the model with training data, and then transform them with the learned model.
90 91 92 93 94 95 96 |
# File 'rumale-metric_learning/lib/rumale/metric_learning/mlkr.rb', line 90 def fit_transform(x, y) x = ::Rumale::Validation.check_convert_sample_array(x) y = ::Rumale::Validation.check_convert_target_value_array(y) ::Rumale::Validation.check_sample_size(x, y) fit(x, y).transform(x) end |
#transform(x) ⇒ Numo::DFloat
Transform the given data with the learned model.
102 103 104 105 106 |
# File 'rumale-metric_learning/lib/rumale/metric_learning/mlkr.rb', line 102 def transform(x) x = ::Rumale::Validation.check_convert_sample_array(x) x.dot(@components.transpose) end |