Class: Rumale::MetricLearning::NeighbourhoodComponentAnalysis

Inherits:
Base::Estimator
  • Object
show all
Includes:
Base::Transformer
Defined in:
rumale-metric_learning/lib/rumale/metric_learning/neighbourhood_component_analysis.rb

Overview

NeighbourhoodComponentAnalysis is a class that implements Neighbourhood Component Analysis.

Reference

  • Goldberger, J., Roweis, S., Hinton, G., and Salakhutdinov, R., “Neighbourhood Component Analysis,” Advances in NIPS’17, pp. 513–520, 2005.

Examples:

require 'rumale/metric_learning/neighbourhood_component_analysis'

transformer = Rumale::MetricLearning::NeighbourhoodComponentAnalysis.new
transformer.fit(training_samples, traininig_labels)
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) ⇒ NeighbourhoodComponentAnalysis

Create a new transformer with NeighbourhoodComponentAnalysis.

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.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'rumale-metric_learning/lib/rumale/metric_learning/neighbourhood_component_analysis.rb', line 49

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 neighbourhood components.

Returns:

  • (Numo::DFloat)

    (shape: [n_components, n_features])



29
30
31
# File 'rumale-metric_learning/lib/rumale/metric_learning/neighbourhood_component_analysis.rb', line 29

def components
  @components
end

#n_iterInteger (readonly)

Return the number of iterations run for optimization

Returns:

  • (Integer)


33
34
35
# File 'rumale-metric_learning/lib/rumale/metric_learning/neighbourhood_component_analysis.rb', line 33

def n_iter
  @n_iter
end

#rngRandom (readonly)

Return the random generator.

Returns:

  • (Random)


37
38
39
# File 'rumale-metric_learning/lib/rumale/metric_learning/neighbourhood_component_analysis.rb', line 37

def rng
  @rng
end

Instance Method Details

#fit(x, y) ⇒ NeighbourhoodComponentAnalysis

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::Int32)

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

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'rumale-metric_learning/lib/rumale/metric_learning/neighbourhood_component_analysis.rb', line 67

def fit(x, y)
  x = ::Rumale::Validation.check_convert_sample_array(x)
  y = ::Rumale::Validation.check_convert_label_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)
  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::Int32)

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

Returns:

  • (Numo::DFloat)

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



87
88
89
90
91
92
93
# File 'rumale-metric_learning/lib/rumale/metric_learning/neighbourhood_component_analysis.rb', line 87

def fit_transform(x, y)
  x = ::Rumale::Validation.check_convert_sample_array(x)
  y = ::Rumale::Validation.check_convert_label_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.



99
100
101
102
103
# File 'rumale-metric_learning/lib/rumale/metric_learning/neighbourhood_component_analysis.rb', line 99

def transform(x)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  x.dot(@components.transpose)
end