Class: SVMKit::KernelApproximation::RBF
- Inherits:
-
Object
- Object
- SVMKit::KernelApproximation::RBF
- Includes:
- Base::BaseEstimator, Base::Transformer
- Defined in:
- lib/svmkit/kernel_approximation/rbf.rb
Overview
Class for RBF kernel feature mapping.
transformer = SVMKit::KernelApproximation::RBF.new(gamma: 1.0, n_coponents: 128, random_seed: 1)
new_training_samples = transformer.fit_transform(training_samples)
new_testing_samples = transformer.transform(testing_samples)
-
Refernce:
-
Rahimi and B. Recht, “Random Features for Large-Scale Kernel Machines,” Proc. NIPS’07, pp.1177–1184, 2007.
-
Constant Summary collapse
- DEFAULT_PARAMS =
:nodoc:
{ # :nodoc: gamma: 1.0, n_components: 128, random_seed: nil }.freeze
Instance Attribute Summary collapse
-
#random_mat ⇒ Object
readonly
The random matrix for transformation.
-
#random_vec ⇒ Object
readonly
The random vector for transformation.
-
#rng ⇒ Object
readonly
The random generator for transformation.
Attributes included from Base::BaseEstimator
Instance Method Summary collapse
-
#fit(x, _y = nil) ⇒ Object
Fit the model with given training data.
-
#fit_transform(x, _y = nil) ⇒ Object
Fit the model with training data, and then transform them with the learned model.
-
#initialize(params = {}) ⇒ RBF
constructor
Creates a new transformer for mapping to RBF kernel feature space.
-
#marshal_dump ⇒ Object
Serializes object through Marshal#dump.
-
#marshal_load(obj) ⇒ Object
Deserialize object through Marshal#load.
-
#transform(x) ⇒ Object
Transform the given data with the learned model.
Constructor Details
#initialize(params = {}) ⇒ RBF
Creates a new transformer for mapping to RBF kernel feature space.
call-seq:
new(gamma: 1.0, n_components: 128, random_seed: 1) -> RBF
-
Arguments :
-
: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.
-
43 44 45 46 47 48 49 |
# File 'lib/svmkit/kernel_approximation/rbf.rb', line 43 def initialize(params = {}) self.params = DEFAULT_PARAMS.merge(Hash[params.map { |k, v| [k.to_sym, v] }]) self.params[:random_seed] ||= srand @rng = Random.new(self.params[:random_seed]) @random_mat = nil @random_vec = nil end |
Instance Attribute Details
#random_mat ⇒ Object (readonly)
The random matrix for transformation.
26 27 28 |
# File 'lib/svmkit/kernel_approximation/rbf.rb', line 26 def random_mat @random_mat end |
#random_vec ⇒ Object (readonly)
The random vector for transformation.
29 30 31 |
# File 'lib/svmkit/kernel_approximation/rbf.rb', line 29 def random_vec @random_vec end |
#rng ⇒ Object (readonly)
The random generator for transformation.
32 33 34 |
# File 'lib/svmkit/kernel_approximation/rbf.rb', line 32 def rng @rng end |
Instance Method Details
#fit(x, _y = nil) ⇒ Object
Fit the model with given training data.
call-seq:
fit(x) -> RBF
-
Arguments :
-
x(NMatrix, 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 :
-
The learned transformer itself.
-
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/svmkit/kernel_approximation/rbf.rb', line 60 def fit(x, _y = nil) n_features = x.shape[1] params[:n_components] = 2 * n_features if params[:n_components] <= 0 @random_mat = rand_normal([n_features, params[:n_components]]) * (2.0 * params[:gamma])**0.5 n_half_components = params[:n_components] / 2 @random_vec = NMatrix.zeros([1, params[:n_components] - n_half_components]).hconcat( NMatrix.ones([1, n_half_components]) * (0.5 * Math::PI) ) #@random_vec = rand_uniform([1, self.params[:n_components]]) * (2.0 * Math::PI) self end |
#fit_transform(x, _y = nil) ⇒ Object
Fit the model with training data, and then transform them with the learned model.
call-seq:
fit_transform(x) -> NMatrix
-
Arguments :
-
x(NMatrix, shape: [n_samples, n_features]) – The training data to be used for fitting the model.
-
-
Returns :
-
The transformed data (NMatrix, shape: [n_samples, n_components]).
-
81 82 83 |
# File 'lib/svmkit/kernel_approximation/rbf.rb', line 81 def fit_transform(x, _y = nil) fit(x).transform(x) end |
#marshal_dump ⇒ Object
Serializes object through Marshal#dump.
101 102 103 104 105 106 |
# File 'lib/svmkit/kernel_approximation/rbf.rb', line 101 def marshal_dump # :nodoc: { params: params, random_mat: Utils.dump_nmatrix(@random_mat), random_vec: Utils.dump_nmatrix(@random_vec), rng: @rng } end |
#marshal_load(obj) ⇒ Object
Deserialize object through Marshal#load.
109 110 111 112 113 114 115 |
# File 'lib/svmkit/kernel_approximation/rbf.rb', line 109 def marshal_load(obj) # :nodoc: self.params = obj[:params] @random_mat = Utils.restore_nmatrix(obj[:random_mat]) @random_vec = Utils.restore_nmatrix(obj[:random_vec]) @rng = obj[:rng] nil end |
#transform(x) ⇒ Object
Transform the given data with the learned model.
call-seq:
transform(x) -> NMatrix
-
Arguments :
-
x(NMatrix, shape: [n_samples, n_features]) – The data to be transformed with the learned model.
-
-
Returns :
-
The transformed data (NMatrix, shape: [n_samples, n_components]).
-
94 95 96 97 98 |
# File 'lib/svmkit/kernel_approximation/rbf.rb', line 94 def transform(x) n_samples, = x.shape projection = x.dot(@random_mat) + @random_vec.repeat(n_samples, 0) projection.sin * ((2.0 / params[:n_components])**0.5) end |