@@ -9,7 +9,7 @@ import { accuracy } from '../../../lib/evaluate/classification.js'
9
9
describe ( 'classification' , ( ) => {
10
10
describe . each ( [ undefined , 'zero_one' , 'hinge' ] ) ( 'accuracyLoss %s' , accuracyLoss => {
11
11
test . each ( [ undefined , 'gaussian' ] ) ( 'kernel %s' , kernel => {
12
- const model = new TightestPerceptron ( 10 , kernel )
12
+ const model = new TightestPerceptron ( 10 , kernel , accuracyLoss )
13
13
const x = Matrix . concat ( Matrix . randn ( 50 , 2 , 0 , 0.2 ) , Matrix . randn ( 50 , 2 , 5 , 0.2 ) ) . toArray ( )
14
14
x [ 50 ] = [ 0.1 , 0.1 ]
15
15
const t = [ ]
@@ -25,26 +25,11 @@ describe('classification', () => {
25
25
expect ( acc ) . toBeGreaterThan ( 0.9 )
26
26
} )
27
27
28
- test . each ( [ 'polynomial' ] ) ( 'kernel %s' , kernel => {
29
- const model = new TightestPerceptron ( 10 , kernel )
30
- const x = Matrix . concat ( Matrix . randn ( 50 , 2 , 0 , 0.2 ) , Matrix . randn ( 50 , 2 , 5 , 0.2 ) ) . toArray ( )
31
- x [ 50 ] = [ 0.1 , 0.1 ]
32
- const t = [ ]
33
- for ( let i = 0 ; i < x . length ; i ++ ) {
34
- t [ i ] = Math . floor ( i / 50 ) * 2 - 1
35
- }
36
- model . init ( x , t )
37
- for ( let i = 0 ; i < 10 ; i ++ ) {
38
- model . fit ( )
39
- }
40
- const y = model . predict ( x )
41
- const acc = accuracy ( y , t )
42
- expect ( acc ) . toBeGreaterThan ( 0.7 )
43
- } )
44
-
45
28
test ( 'custom kernel' , ( ) => {
46
- const model = new TightestPerceptron ( 10 , ( a , b ) =>
47
- Math . exp ( - ( a . reduce ( ( s , v , i ) => s + ( v - b [ i ] ) ** 2 , 0 ) ** 2 ) / 0.01 )
29
+ const model = new TightestPerceptron (
30
+ 10 ,
31
+ ( a , b ) => Math . exp ( - ( a . reduce ( ( s , v , i ) => s + ( v - b [ i ] ) ** 2 , 0 ) ** 2 ) / 0.01 ) ,
32
+ accuracyLoss
48
33
)
49
34
const x = Matrix . concat ( Matrix . randn ( 50 , 2 , 0 , 0.2 ) , Matrix . randn ( 50 , 2 , 5 , 0.2 ) ) . toArray ( )
50
35
const t = [ ]
@@ -60,4 +45,39 @@ describe('classification', () => {
60
45
expect ( acc ) . toBeGreaterThan ( 0.9 )
61
46
} )
62
47
} )
48
+
49
+ test . each ( [ 'polynomial' ] ) ( 'kernel %s' , kernel => {
50
+ const model = new TightestPerceptron ( 10 , kernel )
51
+ const x = Matrix . concat ( Matrix . randn ( 50 , 2 , 0 , 0.2 ) , Matrix . randn ( 50 , 2 , 5 , 0.2 ) ) . toArray ( )
52
+ x [ 50 ] = [ 0.1 , 0.1 ]
53
+ const t = [ ]
54
+ for ( let i = 0 ; i < x . length ; i ++ ) {
55
+ t [ i ] = Math . floor ( i / 50 ) * 2 - 1
56
+ }
57
+ model . init ( x , t )
58
+ for ( let i = 0 ; i < 10 ; i ++ ) {
59
+ model . fit ( )
60
+ }
61
+ const y = model . predict ( x )
62
+ const acc = accuracy ( y , t )
63
+ expect ( acc ) . toBeGreaterThan ( 0.7 )
64
+ } )
65
+
66
+ test ( 'regularizedIncompleteBeta' , ( ) => {
67
+ const model = new TightestPerceptron ( 10 )
68
+ model . _ap = 1.1
69
+ const x = Matrix . concat ( Matrix . randn ( 50 , 2 , 0 , 0.2 ) , Matrix . randn ( 50 , 2 , 5 , 0.2 ) ) . toArray ( )
70
+ x [ 50 ] = [ 0.1 , 0.1 ]
71
+ const t = [ ]
72
+ for ( let i = 0 ; i < x . length ; i ++ ) {
73
+ t [ i ] = Math . floor ( i / 50 ) * 2 - 1
74
+ }
75
+ model . init ( x , t )
76
+ for ( let i = 0 ; i < 10 ; i ++ ) {
77
+ model . fit ( )
78
+ }
79
+ const y = model . predict ( x )
80
+ const acc = accuracy ( y , t )
81
+ expect ( acc ) . toBeGreaterThan ( 0.7 )
82
+ } )
63
83
} )
0 commit comments