rumale-neural_network 0.27.0 → 0.28.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rumale/neural_network/base_rbf.rb +93 -0
- data/lib/rumale/neural_network/base_rvfl.rb +58 -0
- data/lib/rumale/neural_network/mlp_regressor.rb +1 -1
- data/lib/rumale/neural_network/rbf_classifier.rb +107 -0
- data/lib/rumale/neural_network/rbf_regressor.rb +85 -0
- data/lib/rumale/neural_network/rvfl_classifier.rb +108 -0
- data/lib/rumale/neural_network/rvfl_regressor.rb +86 -0
- data/lib/rumale/neural_network/version.rb +1 -1
- data/lib/rumale/neural_network.rb +6 -0
- metadata +11 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c6090291a88fdf2e017af6b56297a109ea668e19432fbb69b9c734aac643269
|
4
|
+
data.tar.gz: 8b88eb8612d9537d298db76bde03e12cfc05251235b70abbf05f302c7e043261
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c2d5585cb3b6bac2430c0070d28f30a2d7a5573a7e32e7761dfcfc01dd3b40e6664c325f596c53fc5f01ee7e2d882846d3522984d791f878148a67b9ef92c29
|
7
|
+
data.tar.gz: 6bb8ee5bc957255298794a0463a84373ee19e5a663a0fd28c2b7f0ef6e7c91b028a906e2ca88c52fb2996960806839f552e0194292974c76214a35c3b1962d1a
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rumale/base/estimator'
|
4
|
+
require 'rumale/pairwise_metric'
|
5
|
+
|
6
|
+
module Rumale
|
7
|
+
module NeuralNetwork
|
8
|
+
# BaseRBF is an abstract class for implementation of radial basis function (RBF) network estimator.
|
9
|
+
# This class is used internally.
|
10
|
+
#
|
11
|
+
# *Reference*
|
12
|
+
# - Bugmann, G., "Normalized Gaussian Radial Basis Function networks," Neural Computation, vol. 20, pp. 97--110, 1998.
|
13
|
+
# - Que, Q., and Belkin, M., "Back to the Future: Radial Basis Function Networks Revisited," Proc. of AISTATS'16, pp. 1375--1383, 2016.
|
14
|
+
class BaseRBF < ::Rumale::Base::Estimator
|
15
|
+
# Create a radial basis function network estimator.
|
16
|
+
#
|
17
|
+
# @param hidden_units [Array] The number of units in the hidden layer.
|
18
|
+
# @param gamma [Float] The parameter for the radial basis function, if nil it is 1 / n_features.
|
19
|
+
# @param reg_param [Float] The regularization parameter.
|
20
|
+
# @param normalize [Boolean] The flag indicating whether to normalize the hidden layer output or not.
|
21
|
+
# @param max_iter [Integer] The maximum number of iterations for finding centers.
|
22
|
+
# @param tol [Float] The tolerance of termination criterion for finding centers.
|
23
|
+
# @param random_seed [Integer] The seed value using to initialize the random generator.
|
24
|
+
def initialize(hidden_units: 128, gamma: nil, reg_param: 100.0, normalize: false,
|
25
|
+
max_iter: 50, tol: 1e-4, random_seed: nil)
|
26
|
+
super()
|
27
|
+
@params = {
|
28
|
+
hidden_units: hidden_units,
|
29
|
+
gamma: gamma,
|
30
|
+
reg_param: reg_param,
|
31
|
+
normalize: normalize,
|
32
|
+
max_iter: max_iter,
|
33
|
+
tol: tol,
|
34
|
+
random_seed: random_seed || srand
|
35
|
+
}
|
36
|
+
@rng = Random.new(@params[:random_seed])
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def partial_fit(x, y)
|
42
|
+
find_centers(x)
|
43
|
+
|
44
|
+
h = hidden_output(x)
|
45
|
+
|
46
|
+
n_samples = h.shape[0]
|
47
|
+
n_features = h.shape[1]
|
48
|
+
reg_term = 1.fdiv(@params[:reg_param])
|
49
|
+
|
50
|
+
@weight_vec = if n_features <= n_samples
|
51
|
+
Numo::Linalg.inv(h.transpose.dot(h) + reg_term * Numo::DFloat.eye(n_features)).dot(h.transpose).dot(y)
|
52
|
+
else
|
53
|
+
h.transpose.dot(Numo::Linalg.inv(h.dot(h.transpose) + reg_term * Numo::DFloat.eye(n_samples))).dot(y)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def hidden_output(x)
|
58
|
+
h = ::Rumale::PairwiseMetric.rbf_kernel(x, @centers, @params[:gamma])
|
59
|
+
h /= h.sum(axis: 1).expand_dims(1) if @params[:normalize]
|
60
|
+
h
|
61
|
+
end
|
62
|
+
|
63
|
+
def find_centers(x)
|
64
|
+
# initialize centers randomly
|
65
|
+
n_samples = x.shape[0]
|
66
|
+
sub_rng = @rng.dup
|
67
|
+
rand_id = Array.new(n_centers) { |_v| sub_rng.rand(0...n_samples) }
|
68
|
+
@centers = x[rand_id, true].dup
|
69
|
+
|
70
|
+
# find centers
|
71
|
+
@params[:max_iter].times do |_t|
|
72
|
+
center_ids = assign_centers(x)
|
73
|
+
old_centers = @centers.dup
|
74
|
+
n_centers.times do |n|
|
75
|
+
assigned_bits = center_ids.eq(n)
|
76
|
+
@centers[n, true] = x[assigned_bits.where, true].mean(axis: 0) if assigned_bits.count.positive?
|
77
|
+
end
|
78
|
+
error = Numo::NMath.sqrt(((old_centers - @centers)**2).sum(axis: 1)).mean
|
79
|
+
break if error <= @params[:tol]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def assign_centers(x)
|
84
|
+
distance_matrix = ::Rumale::PairwiseMetric.euclidean_distance(x, @centers)
|
85
|
+
distance_matrix.min_index(axis: 1) - Numo::Int32[*0.step(distance_matrix.size - 1, @centers.shape[0])]
|
86
|
+
end
|
87
|
+
|
88
|
+
def n_centers
|
89
|
+
@params[:hidden_units]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rumale/base/estimator'
|
4
|
+
require 'rumale/utils'
|
5
|
+
|
6
|
+
module Rumale
|
7
|
+
module NeuralNetwork
|
8
|
+
# BaseRVFL is an abstract class for implementation of random vector functional link (RVFL) network.
|
9
|
+
# This class is used internally.
|
10
|
+
#
|
11
|
+
# *Reference*
|
12
|
+
# - 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.
|
13
|
+
# - Zhang, L., and Suganthan, P. N., "A comprehensive evaluation of random vector functional link networks," Information Sciences, vol. 367--368, pp. 1094--1105, 2016.
|
14
|
+
class BaseRVFL < ::Rumale::Base::Estimator
|
15
|
+
# Create a random vector functional link network estimator.
|
16
|
+
#
|
17
|
+
# @param hidden_units [Array] The number of units in the hidden layer.
|
18
|
+
# @param reg_param [Float] The regularization parameter.
|
19
|
+
# @param scale [Float] The scale parameter for random weight and bias.
|
20
|
+
# @param random_seed [Integer] The seed value using to initialize the random generator.
|
21
|
+
def initialize(hidden_units: 128, reg_param: 100.0, scale: 1.0, random_seed: nil)
|
22
|
+
super()
|
23
|
+
@params = {
|
24
|
+
hidden_units: hidden_units,
|
25
|
+
reg_param: reg_param,
|
26
|
+
scale: scale,
|
27
|
+
random_seed: random_seed || srand
|
28
|
+
}
|
29
|
+
@rng = Random.new(@params[:random_seed])
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def partial_fit(x, y)
|
35
|
+
h = hidden_output(x)
|
36
|
+
|
37
|
+
n_samples = h.shape[0]
|
38
|
+
n_features = h.shape[1]
|
39
|
+
reg_term = 1.fdiv(@params[:reg_param])
|
40
|
+
|
41
|
+
@weight_vec = if n_features <= n_samples
|
42
|
+
Numo::Linalg.inv(h.transpose.dot(h) + reg_term * Numo::DFloat.eye(n_features)).dot(h.transpose).dot(y)
|
43
|
+
else
|
44
|
+
h.transpose.dot(Numo::Linalg.inv(h.dot(h.transpose) + reg_term * Numo::DFloat.eye(n_samples))).dot(y)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def hidden_output(x)
|
49
|
+
sub_rng = @rng.dup
|
50
|
+
n_features = x.shape[1]
|
51
|
+
@random_weight_vec = (2.0 * Rumale::Utils.rand_uniform([n_features, @params[:hidden_units]], sub_rng) - 1.0) * @params[:scale] # rubocop:disbale Layout/LineLength
|
52
|
+
@random_bias = Rumale::Utils.rand_uniform(@params[:hidden_units], sub_rng) * @params[:scale]
|
53
|
+
h = 0.5 * (Numo::NMath.tanh(0.5 * (x.dot(@random_weight_vec) + @random_bias)) + 1.0)
|
54
|
+
Numo::DFloat.hstack([x, h])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -14,7 +14,7 @@ module Rumale
|
|
14
14
|
# require 'rumale/neural_network/mlp_regressor'
|
15
15
|
#
|
16
16
|
# estimator = Rumale::NeuralNetwork::MLPRegressor.new(hidden_units: [100, 100], dropout_rate: 0.3)
|
17
|
-
# estimator.fit(training_samples,
|
17
|
+
# estimator.fit(training_samples, traininig_values)
|
18
18
|
# results = estimator.predict(testing_samples)
|
19
19
|
class MLPRegressor < BaseMLP
|
20
20
|
include ::Rumale::Base::Regressor
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rumale/base/classifier'
|
4
|
+
require 'rumale/utils'
|
5
|
+
require 'rumale/validation'
|
6
|
+
require 'rumale/neural_network/base_rbf'
|
7
|
+
|
8
|
+
module Rumale
|
9
|
+
module NeuralNetwork
|
10
|
+
# RBFClassifier is a class that implements classifier based on (k-means) radial basis function (RBF) networks.
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# require 'numo/tiny_linalg'
|
14
|
+
# Numo::Linalg = Numo::TinyLinalg
|
15
|
+
#
|
16
|
+
# require 'rumale/neural_network/rbf_classifier'
|
17
|
+
#
|
18
|
+
# estimator = Rumale::NeuralNetwork::RBFClassifier.new(hidden_units: 128, reg_param: 100.0)
|
19
|
+
# estimator.fit(training_samples, traininig_labels)
|
20
|
+
# results = estimator.predict(testing_samples)
|
21
|
+
#
|
22
|
+
# *Reference*
|
23
|
+
# - Bugmann, G., "Normalized Gaussian Radial Basis Function networks," Neural Computation, vol. 20, pp. 97--110, 1998.
|
24
|
+
# - Que, Q., and Belkin, M., "Back to the Future: Radial Basis Function Networks Revisited," Proc. of AISTATS'16, pp. 1375--1383, 2016.
|
25
|
+
class RBFClassifier < BaseRBF
|
26
|
+
include ::Rumale::Base::Classifier
|
27
|
+
|
28
|
+
# Return the class labels.
|
29
|
+
# @return [Numo::Int32] (size: n_classes)
|
30
|
+
attr_reader :classes
|
31
|
+
|
32
|
+
# Return the centers in the hidden layer of RBF network.
|
33
|
+
# @return [Numo::DFloat] (shape: [n_centers, n_features])
|
34
|
+
attr_reader :centers
|
35
|
+
|
36
|
+
# Return the weight vector.
|
37
|
+
# @return [Numo::DFloat] (shape: [n_centers, n_classes])
|
38
|
+
attr_reader :weight_vec
|
39
|
+
|
40
|
+
# Return the random generator.
|
41
|
+
# @return [Random]
|
42
|
+
attr_reader :rng
|
43
|
+
|
44
|
+
# Create a new classifier with (k-means) RBF networks.
|
45
|
+
#
|
46
|
+
# @param hidden_units [Array] The number of units in the hidden layer.
|
47
|
+
# @param gamma [Float] The parameter for the radial basis function, if nil it is 1 / n_features.
|
48
|
+
# @param reg_param [Float] The regularization parameter.
|
49
|
+
# @param normalize [Boolean] The flag indicating whether to normalize the hidden layer output or not.
|
50
|
+
# @param max_iter [Integer] The maximum number of iterations for finding centers.
|
51
|
+
# @param tol [Float] The tolerance of termination criterion for finding centers.
|
52
|
+
# @param random_seed [Integer] The seed value using to initialize the random generator.
|
53
|
+
def initialize(hidden_units: 128, gamma: nil, reg_param: 100.0, normalize: false,
|
54
|
+
max_iter: 50, tol: 1e-4, random_seed: nil)
|
55
|
+
super
|
56
|
+
end
|
57
|
+
|
58
|
+
# Fit the model with given training data.
|
59
|
+
#
|
60
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
|
61
|
+
# @param y [Numo::Int32] (shape: [n_samples]) The labels to be used for fitting the model.
|
62
|
+
# @return [RBFClassifier] The learned classifier itself.
|
63
|
+
def fit(x, y)
|
64
|
+
x = ::Rumale::Validation.check_convert_sample_array(x)
|
65
|
+
y = ::Rumale::Validation.check_convert_label_array(y)
|
66
|
+
::Rumale::Validation.check_sample_size(x, y)
|
67
|
+
raise 'RBFClassifier#fit requires Numo::Linalg but that is not loaded.' unless enable_linalg?(warning: false)
|
68
|
+
|
69
|
+
@classes = Numo::NArray[*y.to_a.uniq.sort]
|
70
|
+
|
71
|
+
partial_fit(x, one_hot_encode(y))
|
72
|
+
|
73
|
+
self
|
74
|
+
end
|
75
|
+
|
76
|
+
# Calculate confidence scores for samples.
|
77
|
+
#
|
78
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to compute the scores.
|
79
|
+
# @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
|
80
|
+
def decision_function(x)
|
81
|
+
x = ::Rumale::Validation.check_convert_sample_array(x)
|
82
|
+
|
83
|
+
h = hidden_output(x)
|
84
|
+
h.dot(@weight_vec)
|
85
|
+
end
|
86
|
+
|
87
|
+
# Predict class labels for samples.
|
88
|
+
#
|
89
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
|
90
|
+
# @return [Numo::Int32] (shape: [n_samples]) Predicted class label per sample.
|
91
|
+
def predict(x)
|
92
|
+
x = ::Rumale::Validation.check_convert_sample_array(x)
|
93
|
+
|
94
|
+
scores = decision_function(x)
|
95
|
+
n_samples, n_classes = scores.shape
|
96
|
+
label_ids = scores.max_index(axis: 1) - Numo::Int32.new(n_samples).seq * n_classes
|
97
|
+
@classes[label_ids].dup
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
def one_hot_encode(y)
|
103
|
+
Numo::DFloat.cast(::Rumale::Utils.binarize_labels(y))
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rumale/base/regressor'
|
4
|
+
require 'rumale/neural_network/base_rbf'
|
5
|
+
require 'rumale/validation'
|
6
|
+
|
7
|
+
module Rumale
|
8
|
+
module NeuralNetwork
|
9
|
+
# RBFRegressor is a class that implements regressor based on (k-means) radial basis function (RBF) networks.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# require 'numo/tiny_linalg'
|
13
|
+
# Numo::Linalg = Numo::TinyLinalg
|
14
|
+
#
|
15
|
+
# require 'rumale/neural_network/rbf_regressor'
|
16
|
+
#
|
17
|
+
# estimator = Rumale::NeuralNetwork::RBFRegressor.new(hidden_units: 128, reg_param: 100.0)
|
18
|
+
# estimator.fit(training_samples, traininig_values)
|
19
|
+
# results = estimator.predict(testing_samples)
|
20
|
+
#
|
21
|
+
# *Reference*
|
22
|
+
# - Bugmann, G., "Normalized Gaussian Radial Basis Function networks," Neural Computation, vol. 20, pp. 97--110, 1998.
|
23
|
+
# - Que, Q., and Belkin, M., "Back to the Future: Radial Basis Function Networks Revisited," Proc. of AISTATS'16, pp. 1375--1383, 2016.
|
24
|
+
class RBFRegressor < BaseRBF
|
25
|
+
include ::Rumale::Base::Regressor
|
26
|
+
|
27
|
+
# Return the centers in the hidden layer of RBF network.
|
28
|
+
# @return [Numo::DFloat] (shape: [n_centers, n_features])
|
29
|
+
attr_reader :centers
|
30
|
+
|
31
|
+
# Return the weight vector.
|
32
|
+
# @return [Numo::DFloat] (shape: [n_centers, n_outputs])
|
33
|
+
attr_reader :weight_vec
|
34
|
+
|
35
|
+
# Return the random generator.
|
36
|
+
# @return [Random]
|
37
|
+
attr_reader :rng
|
38
|
+
|
39
|
+
# Create a new regressor with (k-means) RBF networks.
|
40
|
+
#
|
41
|
+
# @param hidden_units [Array] The number of units in the hidden layer.
|
42
|
+
# @param gamma [Float] The parameter for the radial basis function, if nil it is 1 / n_features.
|
43
|
+
# @param reg_param [Float] The regularization parameter.
|
44
|
+
# @param normalize [Boolean] The flag indicating whether to normalize the hidden layer output or not.
|
45
|
+
# @param max_iter [Integer] The maximum number of iterations for finding centers.
|
46
|
+
# @param tol [Float] The tolerance of termination criterion for finding centers.
|
47
|
+
# @param random_seed [Integer] The seed value using to initialize the random generator.
|
48
|
+
def initialize(hidden_units: 128, gamma: nil, reg_param: 100.0, normalize: false,
|
49
|
+
max_iter: 50, tol: 1e-4, random_seed: nil)
|
50
|
+
super
|
51
|
+
end
|
52
|
+
|
53
|
+
# Fit the model with given training data.
|
54
|
+
#
|
55
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
|
56
|
+
# @param y [Numo::DFloat] (shape: [n_samples, n_outputs]) The taget values to be used for fitting the model.
|
57
|
+
# @return [MLPRegressor] The learned regressor itself.
|
58
|
+
def fit(x, y)
|
59
|
+
x = ::Rumale::Validation.check_convert_sample_array(x)
|
60
|
+
y = ::Rumale::Validation.check_convert_target_value_array(y)
|
61
|
+
::Rumale::Validation.check_sample_size(x, y)
|
62
|
+
raise 'RBFRegressor#fit requires Numo::Linalg but that is not loaded.' unless enable_linalg?(warning: false)
|
63
|
+
|
64
|
+
y = y.expand_dims(1) if y.ndim == 1
|
65
|
+
|
66
|
+
partial_fit(x, y)
|
67
|
+
|
68
|
+
self
|
69
|
+
end
|
70
|
+
|
71
|
+
# Predict values for samples.
|
72
|
+
#
|
73
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the values.
|
74
|
+
# @return [Numo::DFloat] (shape: [n_samples, n_outputs]) Predicted values per sample.
|
75
|
+
def predict(x)
|
76
|
+
x = ::Rumale::Validation.check_convert_sample_array(x)
|
77
|
+
|
78
|
+
h = hidden_output(x)
|
79
|
+
out = h.dot(@weight_vec)
|
80
|
+
out = out[true, 0] if out.shape[1] == 1
|
81
|
+
out
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rumale/base/classifier'
|
4
|
+
require 'rumale/neural_network/base_rvfl'
|
5
|
+
require 'rumale/utils'
|
6
|
+
require 'rumale/validation'
|
7
|
+
|
8
|
+
module Rumale
|
9
|
+
module NeuralNetwork
|
10
|
+
# RVFLClassifier is a class that implements classifier based on random vector functional link (RVFL) network.
|
11
|
+
# The current implementation uses sigmoid function as activation function.
|
12
|
+
#
|
13
|
+
# @example
|
14
|
+
# require 'numo/tiny_linalg'
|
15
|
+
# Numo::Linalg = Numo::TinyLinalg
|
16
|
+
#
|
17
|
+
# require 'rumale/neural_network/rvfl_classifier'
|
18
|
+
#
|
19
|
+
# estimator = Rumale::NeuralNetwork::RVFLClassifier.new(hidden_units: 128, reg_param: 100.0)
|
20
|
+
# estimator.fit(training_samples, traininig_labels)
|
21
|
+
# results = estimator.predict(testing_samples)
|
22
|
+
#
|
23
|
+
# *Reference*
|
24
|
+
# - 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.
|
25
|
+
# - Zhang, L., and Suganthan, P. N., "A comprehensive evaluation of random vector functional link networks," Information Sciences, vol. 367--368, pp. 1094--1105, 2016.
|
26
|
+
class RVFLClassifier < BaseRVFL
|
27
|
+
include ::Rumale::Base::Classifier
|
28
|
+
|
29
|
+
# Return the class labels.
|
30
|
+
# @return [Numo::Int32] (size: n_classes)
|
31
|
+
attr_reader :classes
|
32
|
+
|
33
|
+
# Return the weight vector in the hidden layer of RVFL network.
|
34
|
+
# @return [Numo::DFloat] (shape: [n_hidden_units, n_features])
|
35
|
+
attr_reader :random_weight_vec
|
36
|
+
|
37
|
+
# Return the bias vector in the hidden layer of RVFL network.
|
38
|
+
# @return [Numo::DFloat] (shape: [n_hidden_units])
|
39
|
+
attr_reader :random_bias
|
40
|
+
|
41
|
+
# Return the weight vector.
|
42
|
+
# @return [Numo::DFloat] (shape: [n_features + n_hidden_units, n_classes])
|
43
|
+
attr_reader :weight_vec
|
44
|
+
|
45
|
+
# Return the random generator.
|
46
|
+
# @return [Random]
|
47
|
+
attr_reader :rng
|
48
|
+
|
49
|
+
# Create a new classifier with RVFL network.
|
50
|
+
#
|
51
|
+
# @param hidden_units [Array] The number of units in the hidden layer.
|
52
|
+
# @param reg_param [Float] The regularization parameter.
|
53
|
+
# @param scale [Float] The scale parameter for random weight and bias.
|
54
|
+
# @param random_seed [Integer] The seed value using to initialize the random generator.
|
55
|
+
def initialize(hidden_units: 128, reg_param: 100.0, scale: 1.0, random_seed: nil)
|
56
|
+
super
|
57
|
+
end
|
58
|
+
|
59
|
+
# Fit the model with given training data.
|
60
|
+
#
|
61
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
|
62
|
+
# @param y [Numo::Int32] (shape: [n_samples]) The labels to be used for fitting the model.
|
63
|
+
# @return [RVFLClassifier] The learned classifier itself.
|
64
|
+
def fit(x, y)
|
65
|
+
x = ::Rumale::Validation.check_convert_sample_array(x)
|
66
|
+
y = ::Rumale::Validation.check_convert_label_array(y)
|
67
|
+
::Rumale::Validation.check_sample_size(x, y)
|
68
|
+
raise 'RVFLClassifier#fit requires Numo::Linalg but that is not loaded.' unless enable_linalg?(warning: false)
|
69
|
+
|
70
|
+
@classes = Numo::NArray[*y.to_a.uniq.sort]
|
71
|
+
|
72
|
+
partial_fit(x, one_hot_encode(y))
|
73
|
+
|
74
|
+
self
|
75
|
+
end
|
76
|
+
|
77
|
+
# Calculate confidence scores for samples.
|
78
|
+
#
|
79
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to compute the scores.
|
80
|
+
# @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
|
81
|
+
def decision_function(x)
|
82
|
+
x = ::Rumale::Validation.check_convert_sample_array(x)
|
83
|
+
|
84
|
+
h = hidden_output(x)
|
85
|
+
h.dot(@weight_vec)
|
86
|
+
end
|
87
|
+
|
88
|
+
# Predict class labels for samples.
|
89
|
+
#
|
90
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
|
91
|
+
# @return [Numo::Int32] (shape: [n_samples]) Predicted class label per sample.
|
92
|
+
def predict(x)
|
93
|
+
x = ::Rumale::Validation.check_convert_sample_array(x)
|
94
|
+
|
95
|
+
scores = decision_function(x)
|
96
|
+
n_samples, n_classes = scores.shape
|
97
|
+
label_ids = scores.max_index(axis: 1) - Numo::Int32.new(n_samples).seq * n_classes
|
98
|
+
@classes[label_ids].dup
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def one_hot_encode(y)
|
104
|
+
Numo::DFloat.cast(::Rumale::Utils.binarize_labels(y))
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rumale/base/regressor'
|
4
|
+
require 'rumale/neural_network/base_rvfl'
|
5
|
+
require 'rumale/validation'
|
6
|
+
|
7
|
+
module Rumale
|
8
|
+
module NeuralNetwork
|
9
|
+
# RVFLRegressor is a class that implements regressor based on random vector functional link (RVFL) network.
|
10
|
+
# The current implementation uses sigmoid function as activation function.
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# require 'numo/tiny_linalg'
|
14
|
+
# Numo::Linalg = Numo::TinyLinalg
|
15
|
+
#
|
16
|
+
# require 'rumale/neural_network/rvfl_regressor'
|
17
|
+
#
|
18
|
+
# estimator = Rumale::NeuralNetwork::RVFLRegressor.new(hidden_units: 128, reg_param: 100.0)
|
19
|
+
# estimator.fit(training_samples, traininig_values)
|
20
|
+
# results = estimator.predict(testing_samples)
|
21
|
+
#
|
22
|
+
# *Reference*
|
23
|
+
# - 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.
|
24
|
+
# - Zhang, L., and Suganthan, P. N., "A comprehensive evaluation of random vector functional link networks," Information Sciences, vol. 367--368, pp. 1094--1105, 2016.
|
25
|
+
class RVFLRegressor < BaseRVFL
|
26
|
+
include ::Rumale::Base::Regressor
|
27
|
+
|
28
|
+
# Return the weight vector in the hidden layer of RVFL network.
|
29
|
+
# @return [Numo::DFloat] (shape: [n_hidden_units, n_features])
|
30
|
+
attr_reader :random_weight_vec
|
31
|
+
|
32
|
+
# Return the bias vector in the hidden layer of RVFL network.
|
33
|
+
# @return [Numo::DFloat] (shape: [n_hidden_units])
|
34
|
+
attr_reader :random_bias
|
35
|
+
|
36
|
+
# Return the weight vector.
|
37
|
+
# @return [Numo::DFloat] (shape: [n_features + n_hidden_units, n_outputs])
|
38
|
+
attr_reader :weight_vec
|
39
|
+
|
40
|
+
# Return the random generator.
|
41
|
+
# @return [Random]
|
42
|
+
attr_reader :rng
|
43
|
+
|
44
|
+
# Create a new regressor with RVFL network.
|
45
|
+
#
|
46
|
+
# @param hidden_units [Array] The number of units in the hidden layer.
|
47
|
+
# @param reg_param [Float] The regularization parameter.
|
48
|
+
# @param scale [Float] The scale parameter for random weight and bias.
|
49
|
+
# @param random_seed [Integer] The seed value using to initialize the random generator.
|
50
|
+
def initialize(hidden_units: 128, reg_param: 100.0, scale: 1.0, random_seed: nil)
|
51
|
+
super
|
52
|
+
end
|
53
|
+
|
54
|
+
# Fit the model with given training data.
|
55
|
+
#
|
56
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
|
57
|
+
# @param y [Numo::DFloat] (shape: [n_samples, n_outputs]) The taget values to be used for fitting the model.
|
58
|
+
# @return [RVFLRegressor] The learned regressor itself.
|
59
|
+
def fit(x, y)
|
60
|
+
x = ::Rumale::Validation.check_convert_sample_array(x)
|
61
|
+
y = ::Rumale::Validation.check_convert_target_value_array(y)
|
62
|
+
::Rumale::Validation.check_sample_size(x, y)
|
63
|
+
raise 'RVFLRegressor#fit requires Numo::Linalg but that is not loaded.' unless enable_linalg?(warning: false)
|
64
|
+
|
65
|
+
y = y.expand_dims(1) if y.ndim == 1
|
66
|
+
|
67
|
+
partial_fit(x, y)
|
68
|
+
|
69
|
+
self
|
70
|
+
end
|
71
|
+
|
72
|
+
# Predict values for samples.
|
73
|
+
#
|
74
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the values.
|
75
|
+
# @return [Numo::DFloat] (shape: [n_samples, n_outputs]) The predicted values per sample.
|
76
|
+
def predict(x)
|
77
|
+
x = ::Rumale::Validation.check_convert_sample_array(x)
|
78
|
+
|
79
|
+
h = hidden_output(x)
|
80
|
+
out = h.dot(@weight_vec)
|
81
|
+
out = out[true, 0] if out.shape[1] == 1
|
82
|
+
out
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -7,3 +7,9 @@ require_relative 'neural_network/version'
|
|
7
7
|
require_relative 'neural_network/base_mlp'
|
8
8
|
require_relative 'neural_network/mlp_classifier'
|
9
9
|
require_relative 'neural_network/mlp_regressor'
|
10
|
+
require_relative 'neural_network/base_rbf'
|
11
|
+
require_relative 'neural_network/rbf_classifier'
|
12
|
+
require_relative 'neural_network/rbf_regressor'
|
13
|
+
require_relative 'neural_network/base_rvfl'
|
14
|
+
require_relative 'neural_network/rvfl_classifier'
|
15
|
+
require_relative 'neural_network/rvfl_regressor'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rumale-neural_network
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.28.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- yoshoku
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: numo-narray
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: 0.28.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.
|
40
|
+
version: 0.28.0
|
41
41
|
description: |
|
42
42
|
Rumale::NeuralNetwork provides classifier and regression
|
43
43
|
based on multi-layer perceptron with Rumale interface.
|
@@ -51,8 +51,14 @@ files:
|
|
51
51
|
- README.md
|
52
52
|
- lib/rumale/neural_network.rb
|
53
53
|
- lib/rumale/neural_network/base_mlp.rb
|
54
|
+
- lib/rumale/neural_network/base_rbf.rb
|
55
|
+
- lib/rumale/neural_network/base_rvfl.rb
|
54
56
|
- lib/rumale/neural_network/mlp_classifier.rb
|
55
57
|
- lib/rumale/neural_network/mlp_regressor.rb
|
58
|
+
- lib/rumale/neural_network/rbf_classifier.rb
|
59
|
+
- lib/rumale/neural_network/rbf_regressor.rb
|
60
|
+
- lib/rumale/neural_network/rvfl_classifier.rb
|
61
|
+
- lib/rumale/neural_network/rvfl_regressor.rb
|
56
62
|
- lib/rumale/neural_network/version.rb
|
57
63
|
homepage: https://github.com/yoshoku/rumale
|
58
64
|
licenses:
|
@@ -78,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
84
|
- !ruby/object:Gem::Version
|
79
85
|
version: '0'
|
80
86
|
requirements: []
|
81
|
-
rubygems_version: 3.
|
87
|
+
rubygems_version: 3.4.20
|
82
88
|
signing_key:
|
83
89
|
specification_version: 4
|
84
90
|
summary: Rumale::NeuralNetwork provides classifier and regression based on multi-layer
|