Class: Rumale::NeuralNetwork::RVFLClassifier

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

Overview

RVFLClassifier is a class that implements classifier based on random vector functional link (RVFL) network. The current implementation uses sigmoid function as activation function.

Reference

  • Malik, A. K., Gao, R., Ganaie, M. A., Tanveer, M., and Suganthan, P. N., “Random vector functional link network: recent developments, applications, and future directions,” Applied Soft Computing, vol. 143, 2023.

  • Zhang, L., and Suganthan, P. N., “A comprehensive evaluation of random vector functional link networks,” Information Sciences, vol. 367–368, pp. 1094–1105, 2016.

Examples:

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

require 'rumale/neural_network/rvfl_classifier'

estimator = Rumale::NeuralNetwork::RVFLClassifier.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, reg_param: 100.0, scale: 1.0, random_seed: nil) ⇒ RVFLClassifier

Create a new classifier with RVFL network.

Parameters:

  • hidden_units (Array) (defaults to: 128)

    The number of units in the hidden layer.

  • reg_param (Float) (defaults to: 100.0)

    The regularization parameter.

  • scale (Float) (defaults to: 1.0)

    The scale parameter for random weight and bias.

  • random_seed (Integer) (defaults to: nil)

    The seed value using to initialize the random generator.



55
56
57
# File 'rumale-neural_network/lib/rumale/neural_network/rvfl_classifier.rb', line 55

def initialize(hidden_units: 128, reg_param: 100.0, scale: 1.0, random_seed: nil)
  super
end

Instance Attribute Details

#classesNumo::Int32 (readonly)

Return the class labels.

Returns:

  • (Numo::Int32)

    (size: n_classes)



31
32
33
# File 'rumale-neural_network/lib/rumale/neural_network/rvfl_classifier.rb', line 31

def classes
  @classes
end

#random_biasNumo::DFloat (readonly)

Return the bias vector in the hidden layer of RVFL network.

Returns:

  • (Numo::DFloat)

    (shape: [n_hidden_units])



39
40
41
# File 'rumale-neural_network/lib/rumale/neural_network/rvfl_classifier.rb', line 39

def random_bias
  @random_bias
end

#random_weight_vecNumo::DFloat (readonly)

Return the weight vector in the hidden layer of RVFL network.

Returns:

  • (Numo::DFloat)

    (shape: [n_hidden_units, n_features])



35
36
37
# File 'rumale-neural_network/lib/rumale/neural_network/rvfl_classifier.rb', line 35

def random_weight_vec
  @random_weight_vec
end

#rngRandom (readonly)

Return the random generator.

Returns:

  • (Random)


47
48
49
# File 'rumale-neural_network/lib/rumale/neural_network/rvfl_classifier.rb', line 47

def rng
  @rng
end

#weight_vecNumo::DFloat (readonly)

Return the weight vector.

Returns:

  • (Numo::DFloat)

    (shape: [n_features + n_hidden_units, n_classes])



43
44
45
# File 'rumale-neural_network/lib/rumale/neural_network/rvfl_classifier.rb', line 43

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.



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

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

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

#fit(x, y) ⇒ RVFLClassifier

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:



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

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 'RVFLClassifier#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.



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

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