/
additive_coupling.js
75 lines (67 loc) · 1.62 KB
/
additive_coupling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import Matrix from '../../../util/matrix.js'
import NeuralNetwork from '../../neuralnetwork.js'
import { FlowLayer } from './base.js'
/**
* Additive coupling layer
*/
export default class AdditiveCoupling extends FlowLayer {
/**
* @param {object} config object
* @param {number | null} [config.d] Number of dimensions to input the inner network
* @param {NeuralNetwork | *[] | null} [config.net] Inner network
*/
constructor({ d = null, net = null, ...rest }) {
super(rest)
this._d = d
this._m = net == null ? null : net instanceof NeuralNetwork ? net : NeuralNetwork.fromObject(net)
}
calc(x) {
if (!this._d) {
this._d = Math.floor(x.cols / 2)
}
if (!this._m) {
this._m = NeuralNetwork.fromObject(
[
{ type: 'input' },
{ type: 'full', out_size: 20, activation: 'leaky_relu' },
{ type: 'full', out_size: x.cols - this._d, activation: 'leaky_relu' },
],
null,
'adam'
)
}
this._o = x.copy()
const a = Matrix.zeros(...x.sizes)
a.set(0, this._d, this._m.calc(x.slice(0, this._d, 1)))
this._o.add(a)
return this._o
}
inverse(y) {
this._o = y.copy()
const a = Matrix.zeros(...y.sizes)
a.set(0, this._d, this._m.calc(y.slice(0, this._d, 1)))
this._o.sub(a)
return this._o
}
jacobianDeterminant() {
return 1
}
grad(bo) {
const bi = bo.copy()
const a = Matrix.zeros(...bo.sizes)
const bm = this._m.grad(bo.slice(this._d, null, 1))
a.set(0, 0, bm)
bi.add(a)
return bi
}
update(optimizer) {
this._m.update(optimizer.lr)
}
toObject() {
return {
type: 'additive_coupling',
net: this._m?.toObject(),
}
}
}
AdditiveCoupling.registLayer()