File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 1
1
console . log ( "Hello Autoencoder 🚂" ) ;
2
2
3
3
import * as tf from '@tensorflow/tfjs-node'
4
+
5
+ const autoencoder = tf . sequential ( ) ;
6
+
7
+ const encoder = tf . layers . dense ( {
8
+ units : 32 ,
9
+ inputShape : [ 784 ] ,
10
+ activation : 'relu'
11
+ } ) ;
12
+ const decoder = tf . layers . dense ( {
13
+ units : 784 ,
14
+ activation : 'sigmoid'
15
+ // inputShape: [32]
16
+ } ) ;
17
+
18
+ autoencoder . add ( encoder ) ;
19
+ autoencoder . add ( decoder ) ;
20
+
21
+ autoencoder . compile ( {
22
+ optimizer : 'adam' ,
23
+ loss : 'binaryCrossentropy' ,
24
+ metrics : [ 'accuracy' ] ,
25
+ } ) ;
26
+
27
+
28
+ function generateImage ( ) {
29
+ const img = [ ] ;
30
+ for ( let i = 0 ; i < 784 ; i ++ ) {
31
+ img [ i ] = Math . random ( ) ;
32
+ }
33
+ return img ;
34
+ }
35
+
36
+ const x_inputs = [ ] ;
37
+ for ( let i = 0 ; i < 1000 ; i ++ ) {
38
+ x_inputs [ i ] = generateImage ( ) ;
39
+ }
40
+
41
+ const x_train = tf . tensor2d ( x_inputs ) ;
42
+ x_train . print ( ) ;
43
+
44
+ trainModel ( ) ;
45
+
46
+ async function trainModel ( ) {
47
+ await autoencoder . fit ( x_train , x_train , {
48
+ epochs : 50 ,
49
+ batch_size : 256 ,
50
+ shuffle : true ,
51
+ verbose : true
52
+ } ) ;
53
+ }
54
+
55
+
56
+
57
+
58
+
You can’t perform that action at this time.
0 commit comments