/
plu.js
47 lines (42 loc) · 892 Bytes
/
plu.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
import Layer from './base.js'
/**
* Piecewise linear unit layer
*/
export default class PiecewiseLinearUnitLayer extends Layer {
/**
* @param {object} config config
* @param {number} [config.alpha] alpha
* @param {number} [config.c] b
*/
constructor({ alpha = 0.1, c = 1, ...rest }) {
super(rest)
this._alpha = alpha
this._c = c
}
calc(x) {
this._i = x
const o = x.copy()
o.map(v => Math.max(this._alpha * (v + this._c) - this._c, Math.min(this._alpha * (v - this._c) + this._c, v)))
return o
}
grad(bo) {
const bi = bo.copy()
bi.broadcastOperate(
this._i,
(a, b) =>
a *
(b < this._alpha * (b + this._c) - this._c || this._alpha * (b - this._c) + this._c < b
? this._alpha
: 1)
)
return bi
}
toObject() {
return {
type: 'plu',
alpha: this._alpha,
c: this._c,
}
}
}
PiecewiseLinearUnitLayer.registLayer('plu')