rumale-svm 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/rumale/svm/random_recursive_svc.rb +145 -0
- data/lib/rumale/svm/version.rb +1 -1
- data/lib/rumale/svm.rb +1 -0
- data/sig/rumale/svm/locally_linear_svc.rbs +32 -0
- data/sig/rumale/svm/random_recursive_svc.rbs +27 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d027826bfde557a8e724b62252182549d71a3bc90ebcfa7b3e5f4a4f915a553e
|
4
|
+
data.tar.gz: ba0baf950d204dcbf993b41c99ca8a79a63b0ffac87fe5204211c7b56759374e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65f9a78800033bdbc0354d146cf6150d35aa4924c07164a7bfe578704642d4cde0d49a604fcf5dd75a135462282ee2e18fbcf9157773ce8d827c5a671be6eaa6
|
7
|
+
data.tar.gz: c3d8da2ad2790c8cc656194c1dd0a083a5dbc364ef3b14c768cf5edcb83449d4ef0d61f3d00d8b358171c492e09f2053bd73c7e3b56418234d996fd70945c23b
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
# 0.10.0
|
2
|
+
- Add Rumale::SVM::RandomRecursiveSVC that is classifier with random recursive support vector machine.
|
3
|
+
- Add type declaration files for RandomRecursiveSVC and LocallyLinearSVC.
|
4
|
+
|
1
5
|
# 0.9.0
|
2
6
|
- Add Rumale::SVM::LocallyLinearSVC that is classifier with locally linear support vector machine.
|
3
7
|
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rumale/utils'
|
4
|
+
require 'rumale/base/estimator'
|
5
|
+
require 'rumale/base/classifier'
|
6
|
+
require 'rumale/svm/linear_svc'
|
7
|
+
|
8
|
+
module Rumale
|
9
|
+
module SVM
|
10
|
+
# RandomRecursiveSVC is a class that implements Random Recursive Support Vector Classifier.
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# require 'rumale/svm'
|
14
|
+
#
|
15
|
+
# estimator = Rumale::SVM::RandomRecursiveSVC.new(n_hidden_layers: 2, beta: 0.5, random_seed: 1)
|
16
|
+
# estimator.fit(training_samples, training_labels)
|
17
|
+
# results = estimator.predict(testing_samples)
|
18
|
+
#
|
19
|
+
# *Reference*
|
20
|
+
# - Vinyals, O., Jia, Y., Deng, L., and Darrell, T., "Learning with Recursive Perceptual Representations," In Proc. NIPS'12, pp. 2825--2833, 2012.
|
21
|
+
class RandomRecursiveSVC < Rumale::Base::Estimator
|
22
|
+
include Rumale::Base::Classifier
|
23
|
+
|
24
|
+
# Return the classifiers for each layer.
|
25
|
+
# @return [Array<LinearSVC>]
|
26
|
+
attr_reader :classifiers
|
27
|
+
|
28
|
+
# Return the random matrices for each hidden layer.
|
29
|
+
# @return [Array<Numo::DFloat>] (shape: [n_classes, n_features])
|
30
|
+
attr_reader :random_matrices
|
31
|
+
|
32
|
+
# Create a new classifier with Random Recursive Support Vector Machine.
|
33
|
+
#
|
34
|
+
# @param n_hidden_layers [Integer] The number of hidden layers.
|
35
|
+
# @param beta [Float] The weight parameter for the degree of moving the original data.
|
36
|
+
# @param penalty [String] The type of norm used in the penalization ('l2' or 'l1').
|
37
|
+
# @param loss [String] The type of loss function ('squared_hinge' or 'hinge').
|
38
|
+
# This parameter is ignored if penalty = 'l1'.
|
39
|
+
# @param dual [Boolean] The flag indicating whether to solve dual optimization problem.
|
40
|
+
# When n_samples > n_features, dual = false is more preferable.
|
41
|
+
# This parameter is ignored if loss = 'hinge'.
|
42
|
+
# @param reg_param [Float] The regularization parameter.
|
43
|
+
# @param fit_bias [Boolean] The flag indicating whether to fit the bias term.
|
44
|
+
# @param bias_scale [Float] The scale of the bias term.
|
45
|
+
# This parameter is ignored if fit_bias = false.
|
46
|
+
# @param tol [Float] The tolerance of termination criterion.
|
47
|
+
# @param verbose [Boolean] The flag indicating whether to output learning process message
|
48
|
+
# @param random_seed [Integer/Nil] The seed value using to initialize the random generator.
|
49
|
+
def initialize(n_hidden_layers: 2, beta: 0.5, penalty: 'l2', loss: 'squared_hinge', dual: true, reg_param: 1.0,
|
50
|
+
fit_bias: true, bias_scale: 1.0, tol: 1e-3, verbose: false, random_seed: nil)
|
51
|
+
super()
|
52
|
+
@params = {
|
53
|
+
n_hidden_layers: n_hidden_layers,
|
54
|
+
beta: beta,
|
55
|
+
penalty: penalty == 'l1' ? 'l1' : 'l2',
|
56
|
+
loss: loss == 'hinge' ? 'hinge' : 'squared_hinge',
|
57
|
+
dual: dual,
|
58
|
+
reg_param: reg_param.to_f,
|
59
|
+
fit_bias: fit_bias,
|
60
|
+
bias_scale: bias_scale.to_f,
|
61
|
+
tol: tol.to_f,
|
62
|
+
verbose: verbose,
|
63
|
+
random_seed: random_seed || Random.rand(4_294_967_295)
|
64
|
+
}
|
65
|
+
@rng = Random.new(@params[:random_seed])
|
66
|
+
end
|
67
|
+
|
68
|
+
# Fit the model with given training data.
|
69
|
+
#
|
70
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model.
|
71
|
+
# @param y [Numo::Int32] (shape: [n_samples]) The labels to be used for fitting the model.
|
72
|
+
# @return [RandomRecursiveSVC] The learned classifier itself.
|
73
|
+
def fit(x, y)
|
74
|
+
partial_fit(x, y)
|
75
|
+
self
|
76
|
+
end
|
77
|
+
|
78
|
+
# Calculate confidence scores for samples.
|
79
|
+
#
|
80
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to compute the scores.
|
81
|
+
# @return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
|
82
|
+
def decision_function(x)
|
83
|
+
@classifiers.last.decision_function(transform(x))
|
84
|
+
end
|
85
|
+
|
86
|
+
# Predict class labels for samples.
|
87
|
+
#
|
88
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
|
89
|
+
# @return [Numo::Int32] (shape: [n_samples]) Predicted class label per sample.
|
90
|
+
def predict(x)
|
91
|
+
@classifiers.last.predict(transform(x))
|
92
|
+
end
|
93
|
+
|
94
|
+
# Transform the given data with the learned model.
|
95
|
+
#
|
96
|
+
# @param x [Numo::DFloat] (shape: [n_samples, n_features]) The data to be transformed with the learned model.
|
97
|
+
# @return [Numo::DFloat] (shape: [n_samples, n_features]) The output of last hidden layer.
|
98
|
+
def transform(x)
|
99
|
+
d = x
|
100
|
+
s = Numo::DFloat.zeros(x.shape)
|
101
|
+
@random_matrices.each_with_index do |w, n|
|
102
|
+
o = @classifiers[n].predict_proba(d)
|
103
|
+
s += o.dot(w)
|
104
|
+
d = sigmoid(x + @params[:beta] * s)
|
105
|
+
end
|
106
|
+
d
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def partial_fit(x, y)
|
112
|
+
@classifiers = []
|
113
|
+
@random_matrices = []
|
114
|
+
sub_rng = @rng.dup
|
115
|
+
n_classes = y.to_a.uniq.size
|
116
|
+
n_features = x.shape[1]
|
117
|
+
d = x
|
118
|
+
s = Numo::DFloat.zeros(x.shape)
|
119
|
+
@params[:n_hidden_layers].times do
|
120
|
+
svc = LinearSVC.new(**linear_svc_params).fit(d, y)
|
121
|
+
o = svc.predict_proba(d)
|
122
|
+
w = ::Rumale::Utils.rand_normal([n_classes, n_features], sub_rng)
|
123
|
+
s += o.dot(w)
|
124
|
+
d = sigmoid(x + @params[:beta] * s)
|
125
|
+
@classifiers << svc
|
126
|
+
@random_matrices << w
|
127
|
+
end
|
128
|
+
svc = LinearSVC.new(**linear_svc_params).fit(d, y)
|
129
|
+
@classifiers << svc
|
130
|
+
end
|
131
|
+
|
132
|
+
def linear_svc_params
|
133
|
+
@params.reject { |key, _| RRSVC_PARAMS.include?(key) }.merge(probability: true)
|
134
|
+
end
|
135
|
+
|
136
|
+
def sigmoid(x)
|
137
|
+
0.5 * (Numo::NMath.tanh(0.5 * x) + 1.0)
|
138
|
+
end
|
139
|
+
|
140
|
+
RRSVC_PARAMS = %i[n_hidden_layers beta].freeze
|
141
|
+
|
142
|
+
private_constant :RRSVC_PARAMS
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
data/lib/rumale/svm/version.rb
CHANGED
data/lib/rumale/svm.rb
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
# TypeProf 0.21.8
|
2
|
+
|
3
|
+
# Classes
|
4
|
+
module Rumale
|
5
|
+
module SVM
|
6
|
+
class LocallyLinearSVC
|
7
|
+
@params: {reg_param: Float, reg_param_local: Float, max_iter: Integer, n_anchors: Integer, tol: Float, n_neighbors: Integer, fit_bias: bool, bias_scale: Float, random_seed: Integer}
|
8
|
+
@rng: Random
|
9
|
+
@coeff: Numo::DFloat
|
10
|
+
|
11
|
+
attr_reader classes: Numo::Int32
|
12
|
+
attr_reader anchors: Numo::DFloat
|
13
|
+
attr_reader weight_vec: Numo::DFloat
|
14
|
+
attr_reader bias_term: Numo::DFloat
|
15
|
+
def initialize: (?reg_param: Float, ?reg_param_local: Float, ?max_iter: Integer, ?tol: Float, ?n_anchors: Integer, ?n_neighbors: Integer, ?fit_bias: bool, ?bias_scale: Float, ?random_seed: (nil | Integer)) -> void
|
16
|
+
def fit: (Numo::DFloat x, Numo::Int32 y) -> LocallyLinearSVC
|
17
|
+
def decision_function: (Numo::DFloat x) -> Numo::DFloat
|
18
|
+
def predict: (Numo::DFloat x) -> Numo::Int32
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def partial_fit: (Numo::DFloat base_x, Numo::Int32 bin_y) -> [Numo::DFloat, Numo::DFloat]
|
23
|
+
def local_coordinates: (Numo::DFloat xi) -> Numo::DFloat
|
24
|
+
def find_neighbors: (Numo::DFloat xi) -> Numo::Int32
|
25
|
+
def find_anchors: (Numo::DFloat x) -> void
|
26
|
+
def assign_anchors: (Numo::DFloat x) -> Integer
|
27
|
+
def fit_bias?: -> bool
|
28
|
+
def expand_feature: (Numo::DFloat x) -> Numo::DFloat
|
29
|
+
def multiclass_problem?: -> bool
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# TypeProf 0.21.8
|
2
|
+
|
3
|
+
# Classes
|
4
|
+
module Rumale
|
5
|
+
module SVM
|
6
|
+
class RandomRecursiveSVC
|
7
|
+
@params: {n_hidden_layers: Integer, beta: Float, penalty: String, loss: String, dual: bool, reg_param: Float, fit_bias: bool, bias_scale: Float, tol: Float, verbose: bool, random_seed: Integer}
|
8
|
+
@rng: Random
|
9
|
+
|
10
|
+
attr_reader classifiers: Array[Rumale::SVM::LinearSVC]
|
11
|
+
attr_reader random_matrices: Array[Numo::DFloat]
|
12
|
+
def initialize: (?n_hidden_layers: Integer, ?beta: Float, ?penalty: String, ?loss: String, ?dual: bool, ?reg_param: Float, ?fit_bias: bool, ?bias_scale: Float, ?tol: Float, ?verbose: bool, ?random_seed: (nil | Integer)) -> void
|
13
|
+
def fit: (Numo::DFloat x, Numo::Int32 y) -> RandomRecursiveSVC
|
14
|
+
def decision_function: (Numo::DFloat x) -> Numo::DFloat
|
15
|
+
def predict: (Numo::DFloat x) -> Numo::Int32
|
16
|
+
def transform: (Numo::DFloat x) -> Numo::DFloat
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def partial_fit: (Numo::DFloat x, Numo::DFloat y) -> void
|
21
|
+
def linear_svc_params: -> (Hash[:beta | :bias_scale | :dual | :fit_bias | :loss | :n_hidden_layers | :penalty | :random_seed | :reg_param | :tol | :verbose, Float | Integer | String | bool])
|
22
|
+
def sigmoid: (Numo::DFloat x) -> Numo::DFloat
|
23
|
+
|
24
|
+
RRSVC_PARAMS: [:n_hidden_layers, :beta]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rumale-svm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.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-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: numo-liblinear
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- lib/rumale/svm/nu_svc.rb
|
75
75
|
- lib/rumale/svm/nu_svr.rb
|
76
76
|
- lib/rumale/svm/one_class_svm.rb
|
77
|
+
- lib/rumale/svm/random_recursive_svc.rb
|
77
78
|
- lib/rumale/svm/svc.rb
|
78
79
|
- lib/rumale/svm/svr.rb
|
79
80
|
- lib/rumale/svm/version.rb
|
@@ -81,10 +82,12 @@ files:
|
|
81
82
|
- sig/rumale/svm/linear_one_class_svm.rbs
|
82
83
|
- sig/rumale/svm/linear_svc.rbs
|
83
84
|
- sig/rumale/svm/linear_svr.rbs
|
85
|
+
- sig/rumale/svm/locally_linear_svc.rbs
|
84
86
|
- sig/rumale/svm/logistic_regression.rbs
|
85
87
|
- sig/rumale/svm/nu_svc.rbs
|
86
88
|
- sig/rumale/svm/nu_svr.rbs
|
87
89
|
- sig/rumale/svm/one_class_svm.rbs
|
90
|
+
- sig/rumale/svm/random_recursive_svc.rbs
|
88
91
|
- sig/rumale/svm/svc.rbs
|
89
92
|
- sig/rumale/svm/svr.rbs
|
90
93
|
homepage: https://github.com/yoshoku/rumale-svm
|
@@ -111,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
114
|
- !ruby/object:Gem::Version
|
112
115
|
version: '0'
|
113
116
|
requirements: []
|
114
|
-
rubygems_version: 3.4.
|
117
|
+
rubygems_version: 3.4.22
|
115
118
|
signing_key:
|
116
119
|
specification_version: 4
|
117
120
|
summary: Rumale::SVM provides support vector machine algorithms using LIBSVM and LIBLINEAR
|