@@ -25,102 +25,73 @@ class AutoencoderWorker extends BaseWorker {
2525 }
2626}
2727
28- var dispAEClt = function ( elm , model , platform ) {
29- const step = 8
30-
31- return async cb => {
32- const iteration = + elm . select ( '[name=iteration]' ) . property ( 'value' )
33- const batch = + elm . select ( '[name=batch]' ) . property ( 'value' )
34- const rate = + elm . select ( '[name=rate]' ) . property ( 'value' )
35- const rho = + elm . select ( '[name=rho]' ) . property ( 'value' )
36- const fite = await model . fit ( platform . trainInput , iteration , rate , batch , rho )
37- platform . plotLoss ( fite . loss )
38- let p_mat = Matrix . fromArray ( await model . reduce ( platform . trainInput ) )
39-
40- const t_mat = p_mat . argmax ( 1 ) . value . map ( v => v + 1 )
41- let tp_mat = Matrix . fromArray ( await model . reduce ( platform . testInput ( step ) ) )
42- let categories = tp_mat . argmax ( 1 )
43- categories . add ( 1 )
44- platform . trainResult = t_mat
45- platform . testResult ( categories . value )
46-
47- cb && cb ( fite . epoch )
48- }
49- }
50-
51- var dispAEad = function ( elm , model , platform ) {
52- return async cb => {
53- const iteration = + elm . select ( '[name=iteration]' ) . property ( 'value' )
54- const batch = + elm . select ( '[name=batch]' ) . property ( 'value' )
55- const rate = + elm . select ( '[name=rate]' ) . property ( 'value' )
56- const rho = + elm . select ( '[name=rho]' ) . property ( 'value' )
57- const threshold = + elm . select ( '[name=threshold]' ) . property ( 'value' )
58-
59- const tx = platform . trainInput
60- const fite = await model . fit ( tx , iteration , rate , batch , rho )
61- platform . plotLoss ( fite . loss )
62- const px = platform . testInput ( 4 )
63- let pd = [ ] . concat ( tx , px )
64- const e = await model . predict ( pd )
65- let pred = e . data . slice ( 0 , tx . length )
66- let pred_tile = e . data . slice ( tx . length )
67- let d = tx [ 0 ] . length
68-
69- const outliers = [ ]
70- for ( let i = 0 ; i < pred . length ; i ++ ) {
71- let v = 0
72- for ( let k = 0 ; k < d ; k ++ ) {
73- v += ( pred [ i ] [ k ] - tx [ i ] [ k ] ) ** 2
74- }
75- outliers . push ( v > threshold )
76- }
77- const outlier_tiles = [ ]
78- for ( let i = 0 ; i < pred_tile . length ; i ++ ) {
79- let v = 0
80- for ( let k = 0 ; k < d ; k ++ ) {
81- v += ( pred_tile [ i ] [ k ] - px [ i ] [ k ] ) ** 2
82- }
83- outlier_tiles . push ( v > threshold )
84- }
85- platform . trainResult = outliers
86- platform . testResult ( outlier_tiles )
87-
88- cb && cb ( fite . epoch )
89- }
90- }
91-
92- var dispAEdr = function ( elm , model , platform ) {
93- return async cb => {
94- const iteration = + elm . select ( '[name=iteration]' ) . property ( 'value' )
95- const batch = + elm . select ( '[name=batch]' ) . property ( 'value' )
96- const rate = + elm . select ( '[name=rate]' ) . property ( 'value' )
97- const rho = + elm . select ( '[name=rho]' ) . property ( 'value' )
98-
99- const fite = await model . fit ( platform . trainInput , iteration , rate , batch , rho )
100- platform . plotLoss ( fite . loss )
101- platform . trainResult = await model . reduce ( platform . trainInput )
102- cb && cb ( fite . epoch )
103- }
104- }
105-
106- var dispAE = function ( elm , platform ) {
28+ export default function ( platform ) {
29+ platform . setting . ml . usage =
30+ 'Click and add data point. Next, click "Initialize". Finally, click "Fit" button repeatedly.'
10731 const mode = platform . task
10832 const controller = new Controller ( platform )
10933 const model = new AutoencoderWorker ( )
11034 let epoch = 0
111- const fitModel =
112- mode === 'AD'
113- ? dispAEad ( elm , model , platform )
114- : mode === 'CT'
115- ? dispAEClt ( elm , model , platform )
116- : dispAEdr ( elm , model , platform )
35+ const fitModel = async cb => {
36+ if ( mode === 'AD' ) {
37+ const tx = platform . trainInput
38+ const fite = await model . fit ( tx , + iteration . value , rate . value , batch . value , rho . value )
39+ platform . plotLoss ( fite . loss )
40+ const px = platform . testInput ( 4 )
41+ let pd = [ ] . concat ( tx , px )
42+ const e = await model . predict ( pd )
43+ let pred = e . data . slice ( 0 , tx . length )
44+ let pred_tile = e . data . slice ( tx . length )
45+ let d = tx [ 0 ] . length
46+
47+ const outliers = [ ]
48+ for ( let i = 0 ; i < pred . length ; i ++ ) {
49+ let v = 0
50+ for ( let k = 0 ; k < d ; k ++ ) {
51+ v += ( pred [ i ] [ k ] - tx [ i ] [ k ] ) ** 2
52+ }
53+ outliers . push ( v > threshold . value )
54+ }
55+ const outlier_tiles = [ ]
56+ for ( let i = 0 ; i < pred_tile . length ; i ++ ) {
57+ let v = 0
58+ for ( let k = 0 ; k < d ; k ++ ) {
59+ v += ( pred_tile [ i ] [ k ] - px [ i ] [ k ] ) ** 2
60+ }
61+ outlier_tiles . push ( v > threshold . value )
62+ }
63+ platform . trainResult = outliers
64+ platform . testResult ( outlier_tiles )
65+
66+ cb && cb ( fite . epoch )
67+ } else if ( mode === 'CT' ) {
68+ const step = 8
69+ const fite = await model . fit ( platform . trainInput , + iteration . value , rate . value , batch . value , rho . value )
70+ platform . plotLoss ( fite . loss )
71+ let p_mat = Matrix . fromArray ( await model . reduce ( platform . trainInput ) )
72+
73+ const t_mat = p_mat . argmax ( 1 ) . value . map ( v => v + 1 )
74+ let tp_mat = Matrix . fromArray ( await model . reduce ( platform . testInput ( step ) ) )
75+ let categories = tp_mat . argmax ( 1 )
76+ categories . add ( 1 )
77+ platform . trainResult = t_mat
78+ platform . testResult ( categories . value )
79+
80+ cb && cb ( fite . epoch )
81+ } else {
82+ const fite = await model . fit ( platform . trainInput , + iteration . value , rate . value , batch . value , rho . value )
83+ platform . plotLoss ( fite . loss )
84+ platform . trainResult = await model . reduce ( platform . trainInput )
85+ cb && cb ( fite . epoch )
86+ }
87+ }
11788
11889 let rdim = null
11990 if ( mode !== 'DR' ) {
12091 rdim = controller . input . number ( { label : ' Size ' , min : 1 , max : 100 , value : 10 } )
12192 }
12293 const builder = new NeuralNetworkBuilder ( )
123- builder . makeHtml ( elm , { optimizer : true } )
94+ builder . makeHtml ( platform . setting . ml . configElement , { optimizer : true } )
12495 const slbConf = controller . stepLoopButtons ( ) . init ( done => {
12596 platform . init ( )
12697 if ( platform . datas . length === 0 ) {
@@ -131,48 +102,13 @@ var dispAE = function (elm, platform) {
131102
132103 model . initialize ( platform . datas . dimension , rd , builder . layers , builder . invlayers , builder . optimizer ) . then ( done )
133104 } )
134- elm . append ( 'span' ) . text ( ' Iteration ' )
135- elm . append ( 'select' )
136- . attr ( 'name' , 'iteration' )
137- . selectAll ( 'option' )
138- . data ( [ 1 , 10 , 100 , 1000 , 10000 ] )
139- . enter ( )
140- . append ( 'option' )
141- . property ( 'value' , d => d )
142- . text ( d => d )
143- elm . append ( 'span' ) . text ( ' Learning rate ' )
144- elm . append ( 'input' )
145- . attr ( 'type' , 'number' )
146- . attr ( 'name' , 'rate' )
147- . attr ( 'min' , 0 )
148- . attr ( 'max' , 100 )
149- . attr ( 'step' , 0.01 )
150- . attr ( 'value' , 0.001 )
151- elm . append ( 'span' ) . text ( ' Batch size ' )
152- elm . append ( 'input' )
153- . attr ( 'type' , 'number' )
154- . attr ( 'name' , 'batch' )
155- . attr ( 'value' , 10 )
156- . attr ( 'min' , 1 )
157- . attr ( 'max' , 100 )
158- . attr ( 'step' , 1 )
159- elm . append ( 'span' ) . text ( ' Sparse rho ' )
160- elm . append ( 'input' )
161- . attr ( 'type' , 'number' )
162- . attr ( 'name' , 'rho' )
163- . attr ( 'value' , 0.02 )
164- . attr ( 'min' , 0 )
165- . attr ( 'max' , 1 )
166- . attr ( 'step' , 0.01 )
105+ const iteration = controller . select ( { label : ' Iteration ' , values : [ 1 , 10 , 100 , 1000 , 10000 ] } )
106+ const rate = controller . input . number ( { label : ' Learning rate ' , min : 0 , max : 100 , step : 0.01 , value : 0.001 } )
107+ const batch = controller . input . number ( { label : ' Batch size ' , min : 1 , max : 100 , value : 10 } )
108+ const rho = controller . input . number ( { label : ' Sparse rho ' , min : 0 , max : 1 , step : 0.01 , value : 0.02 } )
109+ let threshold = null
167110 if ( mode === 'AD' ) {
168- elm . append ( 'span' ) . text ( ' threshold = ' )
169- elm . append ( 'input' )
170- . attr ( 'type' , 'number' )
171- . attr ( 'name' , 'threshold' )
172- . attr ( 'value' , 0.02 )
173- . attr ( 'min' , 0 )
174- . attr ( 'max' , 10 )
175- . attr ( 'step' , 0.01 )
111+ threshold = controller . input . number ( { label : ' threshold = ' , min : 0 , max : 10 , step : 0.01 , value : 0.02 } )
176112 }
177113 slbConf
178114 . step ( cb => {
@@ -187,9 +123,3 @@ var dispAE = function (elm, platform) {
187123 model . terminate ( )
188124 }
189125}
190-
191- export default function ( platform ) {
192- platform . setting . ml . usage =
193- 'Click and add data point. Next, click "Initialize". Finally, click "Fit" button repeatedly.'
194- platform . setting . terminate = dispAE ( platform . setting . ml . configElement , platform )
195- }
0 commit comments