@@ -30,8 +30,6 @@ pub mod svr;
30
30
31
31
use core:: fmt:: Debug ;
32
32
33
- #[ cfg( feature = "serde" ) ]
34
- use serde:: ser:: { SerializeStruct , Serializer } ;
35
33
#[ cfg( feature = "serde" ) ]
36
34
use serde:: { Deserialize , Serialize } ;
37
35
@@ -40,36 +38,20 @@ use crate::linalg::basic::arrays::{Array1, ArrayView1};
40
38
41
39
/// Defines a kernel function.
42
40
/// This is a object-safe trait.
43
- pub trait Kernel {
41
+ #[ cfg_attr(
42
+ all( feature = "serde" , not( target_arch = "wasm32" ) ) ,
43
+ typetag:: serde( tag = "type" )
44
+ ) ]
45
+ pub trait Kernel : Debug {
44
46
#[ allow( clippy:: ptr_arg) ]
45
47
/// Apply kernel function to x_i and x_j
46
48
fn apply ( & self , x_i : & Vec < f64 > , x_j : & Vec < f64 > ) -> Result < f64 , Failed > ;
47
- /// Return a serializable name
48
- fn name ( & self ) -> & ' static str ;
49
- }
50
-
51
- impl Debug for dyn Kernel {
52
- fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
53
- write ! ( f, "Kernel<f64>" )
54
- }
55
- }
56
-
57
- #[ cfg( feature = "serde" ) ]
58
- impl Serialize for dyn Kernel {
59
- fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
60
- where
61
- S : Serializer ,
62
- {
63
- let mut s = serializer. serialize_struct ( "Kernel" , 1 ) ?;
64
- s. serialize_field ( "type" , & self . name ( ) ) ?;
65
- s. end ( )
66
- }
67
49
}
68
50
69
51
/// Pre-defined kernel functions
70
52
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
71
53
#[ derive( Debug , Clone ) ]
72
- pub struct Kernels { }
54
+ pub struct Kernels ;
73
55
74
56
impl Kernels {
75
57
/// Return a default linear
@@ -211,15 +193,14 @@ impl SigmoidKernel {
211
193
}
212
194
}
213
195
196
+ #[ cfg_attr( all( feature = "serde" , not( target_arch = "wasm32" ) ) , typetag:: serde) ]
214
197
impl Kernel for LinearKernel {
215
198
fn apply ( & self , x_i : & Vec < f64 > , x_j : & Vec < f64 > ) -> Result < f64 , Failed > {
216
199
Ok ( x_i. dot ( x_j) )
217
200
}
218
- fn name ( & self ) -> & ' static str {
219
- "Linear"
220
- }
221
201
}
222
202
203
+ #[ cfg_attr( all( feature = "serde" , not( target_arch = "wasm32" ) ) , typetag:: serde) ]
223
204
impl Kernel for RBFKernel {
224
205
fn apply ( & self , x_i : & Vec < f64 > , x_j : & Vec < f64 > ) -> Result < f64 , Failed > {
225
206
if self . gamma . is_none ( ) {
@@ -231,11 +212,9 @@ impl Kernel for RBFKernel {
231
212
let v_diff = x_i. sub ( x_j) ;
232
213
Ok ( ( -self . gamma . unwrap ( ) * v_diff. mul ( & v_diff) . sum ( ) ) . exp ( ) )
233
214
}
234
- fn name ( & self ) -> & ' static str {
235
- "RBF"
236
- }
237
215
}
238
216
217
+ #[ cfg_attr( all( feature = "serde" , not( target_arch = "wasm32" ) ) , typetag:: serde) ]
239
218
impl Kernel for PolynomialKernel {
240
219
fn apply ( & self , x_i : & Vec < f64 > , x_j : & Vec < f64 > ) -> Result < f64 , Failed > {
241
220
if self . gamma . is_none ( ) || self . coef0 . is_none ( ) || self . degree . is_none ( ) {
@@ -247,11 +226,9 @@ impl Kernel for PolynomialKernel {
247
226
let dot = x_i. dot ( x_j) ;
248
227
Ok ( ( self . gamma . unwrap ( ) * dot + self . coef0 . unwrap ( ) ) . powf ( self . degree . unwrap ( ) ) )
249
228
}
250
- fn name ( & self ) -> & ' static str {
251
- "Polynomial"
252
- }
253
229
}
254
230
231
+ #[ cfg_attr( all( feature = "serde" , not( target_arch = "wasm32" ) ) , typetag:: serde) ]
255
232
impl Kernel for SigmoidKernel {
256
233
fn apply ( & self , x_i : & Vec < f64 > , x_j : & Vec < f64 > ) -> Result < f64 , Failed > {
257
234
if self . gamma . is_none ( ) || self . coef0 . is_none ( ) {
@@ -263,9 +240,6 @@ impl Kernel for SigmoidKernel {
263
240
let dot = x_i. dot ( x_j) ;
264
241
Ok ( self . gamma . unwrap ( ) * dot + self . coef0 . unwrap ( ) . tanh ( ) )
265
242
}
266
- fn name ( & self ) -> & ' static str {
267
- "Sigmoid"
268
- }
269
243
}
270
244
271
245
#[ cfg( test) ]
0 commit comments