Class: Rumale::MetricLearning::MLKR

Inherits:
Base::Estimator show all
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.

Examples:

require 'rumale/metric_learning/mlkr'

transformer = Rumale::MetricLearning::MLKR.new
transformer.fit(training_samples, traininig_target_values)
low_samples = transformer.transform(testing_samples)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

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.

Parameters:

  • n_components (Integer) (defaults to: nil)

    The number of components.

  • init (String) (defaults to: 'random')

    The initialization method for components (‘random’ or ‘pca’).

  • max_iter (Integer) (defaults to: 100)

    The maximum number of iterations.

  • tol (Float) (defaults to: 1e-6)

    The tolerance of termination criterion. This value is given as tol / Lbfgsb::DBL_EPSILON to the factr argument of Lbfgsb.minimize method.

  • verbose (Boolean) (defaults to: false)

    The flag indicating whether to output loss during iteration. If true is given, ‘iterate.dat’ file is generated by lbfgsb.rb.

  • random_seed (Integer) (defaults to: nil)

    The seed value using to initialize the random generator.



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

#componentsNumo::DFloat (readonly)

Returns the metric components.

Returns:

  • (Numo::DFloat)

    (shape: [n_components, n_features])



30
31
32
# File 'rumale-metric_learning/lib/rumale/metric_learning/mlkr.rb', line 30

def components
  @components
end

#n_iterInteger (readonly)

Return the number of iterations run for optimization

Returns:

  • (Integer)


34
35
36
# File 'rumale-metric_learning/lib/rumale/metric_learning/mlkr.rb', line 34

def n_iter
  @n_iter
end

#rngRandom (readonly)

Return the random generator.

Returns:

  • (Random)


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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for fitting the model.

  • y (Numo::DFloat)

    (shape: [n_samples]) The target values to be used for fitting the model.

Returns:

  • (MLKR)

    The learned classifier itself.



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for fitting the model.

  • y (Numo::DFloat)

    (shape: [n_samples]) The target values to be used for fitting the model.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_components]) The transformed data



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.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The data to be transformed with the learned model.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_components]) The transformed data.



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