Class: Rumale::KernelApproximation::RBF
- Inherits:
-
Object
- Object
- Rumale::KernelApproximation::RBF
- Includes:
- Base::BaseEstimator, Base::Transformer
- Defined in:
- lib/rumale/kernel_approximation/rbf.rb
Overview
Class for RBF kernel feature mapping.
Refernce:
-
Rahimi and B. Recht, “Random Features for Large-Scale Kernel Machines,” Proc. NIPS’07, pp.1177–1184, 2007.
-
Instance Attribute Summary collapse
-
#random_mat ⇒ Numo::DFloat
readonly
Return the random matrix for transformation.
-
#random_vec ⇒ Numo::DFloat
readonly
Return the random vector for transformation.
-
#rng ⇒ Random
readonly
Return the random generator for transformation.
Attributes included from Base::BaseEstimator
Instance Method Summary collapse
-
#fit(x) ⇒ RBF
Fit the model with given training data.
-
#fit_transform(x) ⇒ Numo::DFloat
Fit the model with training data, and then transform them with the learned model.
-
#initialize(gamma: 1.0, n_components: 128, random_seed: nil) ⇒ RBF
constructor
Create a new transformer for mapping to RBF kernel feature space.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#transform(x) ⇒ Numo::DFloat
Transform the given data with the learned model.
Constructor Details
#initialize(gamma: 1.0, n_components: 128, random_seed: nil) ⇒ RBF
Create a new transformer for mapping to RBF kernel feature space.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rumale/kernel_approximation/rbf.rb', line 40 def initialize(gamma: 1.0, n_components: 128, random_seed: nil) check_params_float(gamma: gamma) check_params_integer(n_components: n_components) check_params_type_or_nil(Integer, random_seed: random_seed) check_params_positive(gamma: gamma, n_components: n_components) @params = {} @params[:gamma] = gamma @params[:n_components] = n_components @params[:random_seed] = random_seed @params[:random_seed] ||= srand @random_mat = nil @random_vec = nil @rng = Random.new(@params[:random_seed]) end |
Instance Attribute Details
#random_mat ⇒ Numo::DFloat (readonly)
Return the random matrix for transformation.
25 26 27 |
# File 'lib/rumale/kernel_approximation/rbf.rb', line 25 def random_mat @random_mat end |
#random_vec ⇒ Numo::DFloat (readonly)
Return the random vector for transformation.
29 30 31 |
# File 'lib/rumale/kernel_approximation/rbf.rb', line 29 def random_vec @random_vec end |
#rng ⇒ Random (readonly)
Return the random generator for transformation.
33 34 35 |
# File 'lib/rumale/kernel_approximation/rbf.rb', line 33 def rng @rng end |
Instance Method Details
#fit(x) ⇒ RBF
Fit the model with given training data.
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rumale/kernel_approximation/rbf.rb', line 62 def fit(x, _y = nil) check_sample_array(x) n_features = x.shape[1] @params[:n_components] = 2 * n_features if @params[:n_components] <= 0 @random_mat = Rumale::Utils.rand_normal([n_features, @params[:n_components]], @rng) * (2.0 * @params[:gamma])**0.5 n_half_components = @params[:n_components] / 2 @random_vec = Numo::DFloat.zeros(@params[:n_components] - n_half_components).concatenate( Numo::DFloat.ones(n_half_components) * (0.5 * Math::PI) ) self end |
#fit_transform(x) ⇒ Numo::DFloat
Fit the model with training data, and then transform them with the learned model.
81 82 83 84 85 |
# File 'lib/rumale/kernel_approximation/rbf.rb', line 81 def fit_transform(x, _y = nil) check_sample_array(x) fit(x).transform(x) end |
#marshal_dump ⇒ Hash
Dump marshal data.
103 104 105 106 107 108 |
# File 'lib/rumale/kernel_approximation/rbf.rb', line 103 def marshal_dump { params: @params, random_mat: @random_mat, random_vec: @random_vec, rng: @rng } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
112 113 114 115 116 117 118 |
# File 'lib/rumale/kernel_approximation/rbf.rb', line 112 def marshal_load(obj) @params = obj[:params] @random_mat = obj[:random_mat] @random_vec = obj[:random_vec] @rng = obj[:rng] nil end |
#transform(x) ⇒ Numo::DFloat
Transform the given data with the learned model.
93 94 95 96 97 98 99 |
# File 'lib/rumale/kernel_approximation/rbf.rb', line 93 def transform(x) check_sample_array(x) n_samples, = x.shape projection = x.dot(@random_mat) + @random_vec.tile(n_samples, 1) Numo::NMath.sin(projection) * ((2.0 / @params[:n_components])**0.5) end |