Class: Rumale::NeuralNetwork::RBFClassifier

Inherits:
BaseRBF show all
Includes:
Base::Classifier
Defined in:
rumale-neural_network/lib/rumale/neural_network/rbf_classifier.rb

Overview

RBFClassifier is a class that implements classifier based on (k-means) radial basis function (RBF) networks.

Reference

  • Bugmann, G., “Normalized Gaussian Radial Basis Function networks,” Neural Computation, vol. 20, pp. 97–110, 1998.

  • Que, Q., and Belkin, M., “Back to the Future: Radial Basis Function Networks Revisited,” Proc. of AISTATS’16, pp. 1375–1383, 2016.

Examples:

require 'numo/tiny_linalg'
Numo::Linalg = Numo::TinyLinalg

require 'rumale/neural_network/rbf_classifier'

estimator = Rumale::NeuralNetwork::RBFClassifier.new(hidden_units: 128, reg_param: 100.0)
estimator.fit(training_samples, traininig_labels)
results = estimator.predict(testing_samples)

Instance Attribute Summary collapse

Attributes inherited from Base::Estimator

#params

Instance Method Summary collapse

Methods included from Base::Classifier

#score

Constructor Details

#initialize(hidden_units: 128, gamma: nil, reg_param: 100.0, normalize: false, max_iter: 50, tol: 1e-4, random_seed: nil) ⇒ RBFClassifier

Create a new classifier with (k-means) RBF networks.

Parameters:

  • hidden_units (Array) (defaults to: 128)

    The number of units in the hidden layer.

  • gamma (Float) (defaults to: nil)

    The parameter for the radial basis function, if nil it is 1 / n_features.

  • reg_param (Float) (defaults to: 100.0)

    The regularization parameter.

  • normalize (Boolean) (defaults to: false)

    The flag indicating whether to normalize the hidden layer output or not.

  • max_iter (Integer) (defaults to: 50)

    The maximum number of iterations for finding centers.

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

    The tolerance of termination criterion for finding centers.

  • random_seed (Integer) (defaults to: nil)

    The seed value using to initialize the random generator.



53
54
55
56
# File 'rumale-neural_network/lib/rumale/neural_network/rbf_classifier.rb', line 53

def initialize(hidden_units: 128, gamma: nil, reg_param: 100.0, normalize: false,
               max_iter: 50, tol: 1e-4, random_seed: nil)
  super
end

Instance Attribute Details

#centersNumo::DFloat (readonly)

Return the centers in the hidden layer of RBF network.

Returns:

  • (Numo::DFloat)

    (shape: [n_centers, n_features])



34
35
36
# File 'rumale-neural_network/lib/rumale/neural_network/rbf_classifier.rb', line 34

def centers
  @centers
end

#classesNumo::Int32 (readonly)

Return the class labels.

Returns:

  • (Numo::Int32)

    (size: n_classes)



30
31
32
# File 'rumale-neural_network/lib/rumale/neural_network/rbf_classifier.rb', line 30

def classes
  @classes
end

#rngRandom (readonly)

Return the random generator.

Returns:

  • (Random)


42
43
44
# File 'rumale-neural_network/lib/rumale/neural_network/rbf_classifier.rb', line 42

def rng
  @rng
end

#weight_vecNumo::DFloat (readonly)

Return the weight vector.

Returns:

  • (Numo::DFloat)

    (shape: [n_centers, n_classes])



38
39
40
# File 'rumale-neural_network/lib/rumale/neural_network/rbf_classifier.rb', line 38

def weight_vec
  @weight_vec
end

Instance Method Details

#decision_function(x) ⇒ Numo::DFloat

Calculate confidence scores for samples.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to compute the scores.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_classes]) Confidence score per sample.



80
81
82
83
84
85
# File 'rumale-neural_network/lib/rumale/neural_network/rbf_classifier.rb', line 80

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

  h = hidden_output(x)
  h.dot(@weight_vec)
end

#fit(x, y) ⇒ RBFClassifier

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:



63
64
65
66
67
68
69
70
71
72
73
74
# File 'rumale-neural_network/lib/rumale/neural_network/rbf_classifier.rb', line 63

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)
  raise 'RBFClassifier#fit requires Numo::Linalg but that is not loaded.' unless enable_linalg?(warning: false)

  @classes = Numo::NArray[*y.to_a.uniq.sort]

  partial_fit(x, one_hot_encode(y))

  self
end

#predict(x) ⇒ Numo::Int32

Predict class labels for samples.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to predict the labels.

Returns:

  • (Numo::Int32)

    (shape: [n_samples]) Predicted class label per sample.



91
92
93
94
95
96
97
98
# File 'rumale-neural_network/lib/rumale/neural_network/rbf_classifier.rb', line 91

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

  scores = decision_function(x)
  n_samples, n_classes = scores.shape
  label_ids = scores.max_index(axis: 1) - Numo::Int32.new(n_samples).seq * n_classes
  @classes[label_ids].dup
end