Skip to content

Commit 3b5933d

Browse files
authored
Test the layer directly (#18)
1 parent 7602170 commit 3b5933d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+3066
-67
lines changed

lib/model/nns/layer/additive_coupling.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default class AdditiveCoupling extends FlowLayer {
5959
toObject() {
6060
return {
6161
type: 'additive_coupling',
62-
net: this._m.toObject(),
62+
net: this._m?.toObject(),
6363
}
6464
}
6565
}

lib/model/nns/layer/rnn.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ export default class RNNLayer extends Layer {
129129
toObject() {
130130
return {
131131
type: 'rnn',
132-
out_size: this._size,
132+
size: this._size,
133+
out_size: this._out_size,
133134
return_sequences: this._return_sequences,
134135
w_hy: this._w_hy.toArray(),
135136
b_hy: this._b_hy.toArray(),

tests/lib/model/nns/layer/abs.test.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,50 @@
11
import NeuralNetwork from '../../../../../lib/model/neuralnetwork.js'
22
import Matrix from '../../../../../lib/util/matrix.js'
33

4-
describe('abs', () => {
4+
import Layer from '../../../../../lib/model/nns/layer/base.js'
5+
6+
describe('layer', () => {
7+
test('construct', () => {
8+
const layer = Layer.fromObject({ type: 'abs' })
9+
expect(layer).toBeDefined()
10+
})
11+
12+
test('calc', () => {
13+
const layer = Layer.fromObject({ type: 'abs' })
14+
15+
const x = Matrix.randn(100, 10)
16+
const y = layer.calc(x)
17+
for (let i = 0; i < x.rows; i++) {
18+
for (let j = 0; j < x.cols; j++) {
19+
expect(y.at(i, j)).toBeCloseTo(Math.abs(x.at(i, j)))
20+
}
21+
}
22+
})
23+
24+
test('grad', () => {
25+
const layer = Layer.fromObject({ type: 'abs' })
26+
27+
const x = Matrix.randn(100, 10)
28+
layer.calc(x)
29+
30+
const bo = Matrix.ones(100, 10)
31+
const bi = layer.grad(bo)
32+
for (let i = 0; i < x.rows; i++) {
33+
for (let j = 0; j < x.cols; j++) {
34+
expect(bi.at(i, j)).toBeCloseTo(Math.sign(x.at(i, j)))
35+
}
36+
}
37+
})
38+
39+
test('toObject', () => {
40+
const layer = Layer.fromObject({ type: 'abs' })
41+
42+
const obj = layer.toObject()
43+
expect(obj).toEqual({ type: 'abs' })
44+
})
45+
})
46+
47+
describe('nn', () => {
548
test('calc', () => {
649
const net = NeuralNetwork.fromObject([{ type: 'input' }, { type: 'abs' }])
750
const x = Matrix.randn(10, 10)

tests/lib/model/nns/layer/acos.test.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,50 @@
11
import NeuralNetwork from '../../../../../lib/model/neuralnetwork.js'
22
import Matrix from '../../../../../lib/util/matrix.js'
33

4-
describe('acos', () => {
4+
import Layer from '../../../../../lib/model/nns/layer/base.js'
5+
6+
describe('layer', () => {
7+
test('construct', () => {
8+
const layer = Layer.fromObject({ type: 'acos' })
9+
expect(layer).toBeDefined()
10+
})
11+
12+
test('calc', () => {
13+
const layer = Layer.fromObject({ type: 'acos' })
14+
15+
const x = Matrix.random(100, 10, -1, 1)
16+
const y = layer.calc(x)
17+
for (let i = 0; i < x.rows; i++) {
18+
for (let j = 0; j < x.cols; j++) {
19+
expect(y.at(i, j)).toBeCloseTo(Math.acos(x.at(i, j)))
20+
}
21+
}
22+
})
23+
24+
test('grad', () => {
25+
const layer = Layer.fromObject({ type: 'acos' })
26+
27+
const x = Matrix.random(100, 10, -1, 1)
28+
layer.calc(x)
29+
30+
const bo = Matrix.ones(100, 10)
31+
const bi = layer.grad(bo)
32+
for (let i = 0; i < x.rows; i++) {
33+
for (let j = 0; j < x.cols; j++) {
34+
expect(bi.at(i, j)).toBeCloseTo(-1 / (Math.sqrt(1 - x.at(i, j) ** 2) + 1.0e-4))
35+
}
36+
}
37+
})
38+
39+
test('toObject', () => {
40+
const layer = Layer.fromObject({ type: 'acos' })
41+
42+
const obj = layer.toObject()
43+
expect(obj).toEqual({ type: 'acos' })
44+
})
45+
})
46+
47+
describe('nn', () => {
548
test('calc', () => {
649
const net = NeuralNetwork.fromObject([{ type: 'input' }, { type: 'acos' }])
750
const x = Matrix.random(10, 10, -1, 1)

tests/lib/model/nns/layer/acosh.test.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,50 @@
11
import NeuralNetwork from '../../../../../lib/model/neuralnetwork.js'
22
import Matrix from '../../../../../lib/util/matrix.js'
33

4-
describe('acosh', () => {
4+
import Layer from '../../../../../lib/model/nns/layer/base.js'
5+
6+
describe('layer', () => {
7+
test('construct', () => {
8+
const layer = Layer.fromObject({ type: 'acosh' })
9+
expect(layer).toBeDefined()
10+
})
11+
12+
test('calc', () => {
13+
const layer = Layer.fromObject({ type: 'acosh' })
14+
15+
const x = Matrix.random(100, 10, 1, 5)
16+
const y = layer.calc(x)
17+
for (let i = 0; i < x.rows; i++) {
18+
for (let j = 0; j < x.cols; j++) {
19+
expect(y.at(i, j)).toBeCloseTo(Math.acosh(x.at(i, j)))
20+
}
21+
}
22+
})
23+
24+
test('grad', () => {
25+
const layer = Layer.fromObject({ type: 'acosh' })
26+
27+
const x = Matrix.random(100, 10, 1, 5)
28+
layer.calc(x)
29+
30+
const bo = Matrix.ones(100, 10)
31+
const bi = layer.grad(bo)
32+
for (let i = 0; i < x.rows; i++) {
33+
for (let j = 0; j < x.cols; j++) {
34+
expect(bi.at(i, j)).toBeCloseTo(1 / (Math.sqrt(x.at(i, j) ** 2 - 1) + 1.0e-4))
35+
}
36+
}
37+
})
38+
39+
test('toObject', () => {
40+
const layer = Layer.fromObject({ type: 'acosh' })
41+
42+
const obj = layer.toObject()
43+
expect(obj).toEqual({ type: 'acosh' })
44+
})
45+
})
46+
47+
describe('nn', () => {
548
test('calc', () => {
649
const net = NeuralNetwork.fromObject([{ type: 'input' }, { type: 'acosh' }])
750
const x = Matrix.random(10, 10, 1, 5)

tests/lib/model/nns/layer/add.test.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,54 @@
11
import NeuralNetwork from '../../../../../lib/model/neuralnetwork.js'
22
import Matrix from '../../../../../lib/util/matrix.js'
33

4-
describe('add', () => {
4+
import AddLayer from '../../../../../lib/model/nns/layer/add.js'
5+
6+
describe('layer', () => {
7+
test('construct', () => {
8+
const layer = new AddLayer({})
9+
expect(layer).toBeDefined()
10+
})
11+
12+
test('calc', () => {
13+
const layer = new AddLayer({})
14+
15+
const x1 = Matrix.randn(100, 10)
16+
const x2 = Matrix.randn(100, 10)
17+
const y = layer.calc(x1, x2)
18+
for (let i = 0; i < x1.rows; i++) {
19+
for (let j = 0; j < x1.cols; j++) {
20+
expect(y.at(i, j)).toBeCloseTo(x1.at(i, j) + x2.at(i, j))
21+
}
22+
}
23+
})
24+
25+
test('grad', () => {
26+
const layer = new AddLayer({})
27+
28+
const x1 = Matrix.randn(100, 10)
29+
const x2 = Matrix.randn(100, 10)
30+
layer.calc(x1, x2)
31+
32+
const bo = Matrix.ones(100, 10)
33+
const bi = layer.grad(bo)
34+
expect(bi).toHaveLength(2)
35+
for (let i = 0; i < x1.rows; i++) {
36+
for (let j = 0; j < x1.cols; j++) {
37+
expect(bi[0].at(i, j)).toBe(1)
38+
expect(bi[1].at(i, j)).toBe(1)
39+
}
40+
}
41+
})
42+
43+
test('toObject', () => {
44+
const layer = new AddLayer({})
45+
46+
const obj = layer.toObject()
47+
expect(obj).toEqual({ type: 'add' })
48+
})
49+
})
50+
51+
describe('nn', () => {
552
test('calc', () => {
653
const net = NeuralNetwork.fromObject([
754
{ type: 'input', name: 'a' },

tests/lib/model/nns/layer/additive_coupling.test.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,70 @@
11
import NeuralNetwork from '../../../../../lib/model/neuralnetwork.js'
22
import 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

tests/lib/model/nns/layer/argmax.test.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,45 @@ jest.retryTimes(3)
44
import NeuralNetwork from '../../../../../lib/model/neuralnetwork.js'
55
import Matrix from '../../../../../lib/util/matrix.js'
66

7-
describe('argmax', () => {
7+
import ArgmaxLayer from '../../../../../lib/model/nns/layer/argmax.js'
8+
9+
describe('layer', () => {
10+
test('construct', () => {
11+
const layer = new ArgmaxLayer({})
12+
expect(layer).toBeDefined()
13+
})
14+
15+
test('calc', () => {
16+
const layer = new ArgmaxLayer({})
17+
18+
const x = Matrix.randn(100, 10)
19+
const y = layer.calc(x)
20+
const t = x.argmax(1)
21+
for (let i = 0; i < x.rows; i++) {
22+
expect(y.at(i, 0)).toBeCloseTo(t.at(i, 0))
23+
}
24+
})
25+
26+
test('grad', () => {
27+
const layer = new ArgmaxLayer({})
28+
29+
const x = Matrix.randn(100, 10)
30+
layer.calc(x)
31+
32+
const bo = Matrix.ones(100, 1)
33+
const bi = layer.grad(bo)
34+
expect(bi.sizes).toEqual([100, 10])
35+
})
36+
37+
test('toObject', () => {
38+
const layer = new ArgmaxLayer({})
39+
40+
const obj = layer.toObject()
41+
expect(obj).toEqual({ type: 'argmax' })
42+
})
43+
})
44+
45+
describe('nn', () => {
846
test('calc', () => {
947
const net = NeuralNetwork.fromObject([{ type: 'input' }, { type: 'argmax' }])
1048
const x = Matrix.random(10, 10, 0, 1)

0 commit comments

Comments
 (0)