11import NeuralNetwork from '../../../../../lib/model/neuralnetwork.js'
22import Matrix from '../../../../../lib/util/matrix.js'
33
4- describe ( 'additive_coupling' , ( ) => {
4+ import AdditiveCoupling from '../../../../../lib/model/nns/layer/additive_coupling.js'
5+
6+ describe ( 'layer' , ( ) => {
7+ test ( 'construct' , ( ) => {
8+ const layer = new AdditiveCoupling ( { } )
9+ expect ( layer ) . toBeDefined ( )
10+ } )
11+
12+ test ( 'calc' , ( ) => {
13+ const layer = new AdditiveCoupling ( { } )
14+
15+ const x = Matrix . randn ( 100 , 10 )
16+ const y = layer . calc ( x )
17+ expect ( y . sizes ) . toEqual ( x . sizes )
18+ for ( let i = 0 ; i < x . rows ; i ++ ) {
19+ for ( let j = 0 ; j < Math . floor ( x . cols / 2 ) ; j ++ ) {
20+ expect ( y . at ( i , j ) ) . toBe ( x . at ( i , j ) )
21+ }
22+ for ( let j = Math . floor ( x . cols / 2 ) ; j < x . cols ; j ++ ) {
23+ expect ( y . at ( i , j ) ) . not . toBe ( x . at ( i , j ) )
24+ }
25+ }
26+ } )
27+
28+ test ( 'inverse' , ( ) => {
29+ const layer = new AdditiveCoupling ( { } )
30+
31+ const x = Matrix . randn ( 100 , 10 )
32+ const y = layer . calc ( x )
33+ const x0 = layer . inverse ( y )
34+ for ( let i = 0 ; i < x . rows ; i ++ ) {
35+ for ( let j = 0 ; j < x . cols ; j ++ ) {
36+ expect ( x0 . at ( i , j ) ) . toBeCloseTo ( x . at ( i , j ) )
37+ }
38+ }
39+ } )
40+
41+ test ( 'grad' , ( ) => {
42+ const layer = new AdditiveCoupling ( { } )
43+
44+ const x = Matrix . randn ( 100 , 10 )
45+ layer . calc ( x )
46+
47+ const bo = Matrix . ones ( 100 , 10 )
48+ const bi = layer . grad ( bo )
49+ for ( let i = 0 ; i < x . rows ; i ++ ) {
50+ for ( let j = 0 ; j < Math . floor ( x . cols / 2 ) ; j ++ ) {
51+ expect ( bi . at ( i , j ) ) . not . toBe ( 1 )
52+ }
53+ for ( let j = Math . floor ( x . cols / 2 ) ; j < x . cols ; j ++ ) {
54+ expect ( bi . at ( i , j ) ) . toBe ( 1 )
55+ }
56+ }
57+ } )
58+
59+ test ( 'toObject' , ( ) => {
60+ const layer = new AdditiveCoupling ( { } )
61+
62+ const obj = layer . toObject ( )
63+ expect ( obj . type ) . toBe ( 'additive_coupling' )
64+ } )
65+ } )
66+
67+ describe ( 'nn' , ( ) => {
568 test ( 'calc' , ( ) => {
669 const net = NeuralNetwork . fromObject ( [ { type : 'input' } , { type : 'additive_coupling' } ] )
770 const x = Matrix . randn ( 10 , 10 )
@@ -12,6 +75,9 @@ describe('additive_coupling', () => {
1275 for ( let j = 0 ; j < Math . floor ( x . cols / 2 ) ; j ++ ) {
1376 expect ( y . at ( i , j ) ) . toBe ( x . at ( i , j ) )
1477 }
78+ for ( let j = Math . floor ( x . cols / 2 ) ; j < x . cols ; j ++ ) {
79+ expect ( y . at ( i , j ) ) . not . toBe ( x . at ( i , j ) )
80+ }
1581 }
1682 } )
1783
0 commit comments