Class: Rumale::KernelApproximation::RBF

Inherits:
Base::Estimator show all
Includes:
Base::Transformer
Defined in:
rumale-kernel_approximation/lib/rumale/kernel_approximation/rbf.rb

Overview

Class for RBF kernel feature mapping.

Refernce:

  • Rahimi, A., and Recht, B., “Random Features for Large-Scale Kernel Machines,” Proc. NIPS’07, pp.1177–1184, 2007.

Examples:

require 'rumale/kernel_approximation/rbf'

transformer = Rumale::KernelApproximation::RBF.new(gamma: 1.0, n_components: 128, random_seed: 1)
new_training_samples = transformer.fit_transform(training_samples)
new_testing_samples = transformer.transform(testing_samples)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Constructor Details

#initialize(gamma: 1.0, n_components: 128, random_seed: nil) ⇒ RBF

Create a new transformer for mapping to RBF kernel feature space.

Parameters:

  • gamma (Float) (defaults to: 1.0)

    The parameter of RBF kernel: exp(-gamma * x^2).

  • n_components (Integer) (defaults to: 128)

    The number of dimensions of the RBF kernel feature space.

  • random_seed (Integer) (defaults to: nil)

    The seed value using to initialize the random generator.



41
42
43
44
45
46
47
48
49
# File 'rumale-kernel_approximation/lib/rumale/kernel_approximation/rbf.rb', line 41

def initialize(gamma: 1.0, n_components: 128, random_seed: nil)
  super()
  @params = {
    gamma: gamma,
    n_components: n_components,
    random_seed: random_seed || srand
  }
  @rng = Random.new(@params[:random_seed])
end

Instance Attribute Details

#random_matNumo::DFloat (readonly)

Return the random matrix for transformation.

Returns:

  • (Numo::DFloat)

    (shape: [n_features, n_components])



26
27
28
# File 'rumale-kernel_approximation/lib/rumale/kernel_approximation/rbf.rb', line 26

def random_mat
  @random_mat
end

#random_vecNumo::DFloat (readonly)

Return the random vector for transformation.

Returns:

  • (Numo::DFloat)

    (shape: [n_components])



30
31
32
# File 'rumale-kernel_approximation/lib/rumale/kernel_approximation/rbf.rb', line 30

def random_vec
  @random_vec
end

#rngRandom (readonly)

Return the random generator for transformation.

Returns:

  • (Random)


34
35
36
# File 'rumale-kernel_approximation/lib/rumale/kernel_approximation/rbf.rb', line 34

def rng
  @rng
end

Instance Method Details

#fit(x) ⇒ RBF

Fit the model with given training data.

Returns The learned transformer itself.

Parameters:

  • x (Numo::NArray)

    (shape: [n_samples, n_features]) The training data to be used for fitting the model. This method uses only the number of features of the data.

Returns:

  • (RBF)

    The learned transformer itself.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'rumale-kernel_approximation/lib/rumale/kernel_approximation/rbf.rb', line 57

def fit(x, _y = nil)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  n_features = x.shape[1]
  sub_rng = @rng.dup
  @params[:n_components] = 2 * n_features if @params[:n_components] <= 0
  @random_mat = ::Rumale::Utils.rand_normal([n_features, @params[:n_components]], sub_rng) * (2.0 * @params[:gamma])**0.5
  n_half_components = @params[:n_components] / 2
  @random_vec = Numo::DFloat.zeros(@params[:n_components] - n_half_components).concatenate(
    Numo::DFloat.ones(n_half_components) * (0.5 * Math::PI)
  )
  self
end

#fit_transform(x) ⇒ Numo::DFloat

Fit the model with training data, and then transform them with the learned model.

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

Parameters:

  • x (Numo::DFloat)

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

Returns:

  • (Numo::DFloat)

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



76
77
78
79
80
# File 'rumale-kernel_approximation/lib/rumale/kernel_approximation/rbf.rb', line 76

def fit_transform(x, _y = nil)
  x = ::Rumale::Validation.check_convert_sample_array(x)

  fit(x).transform(x)
end

#transform(x) ⇒ Numo::DFloat

Transform the given data with the learned model.

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

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.



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

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

  n_samples, = x.shape
  projection = x.dot(@random_mat) + @random_vec.tile(n_samples, 1)
  Numo::NMath.sin(projection) * ((2.0 / @params[:n_components])**0.5)
end