/
slu.js
64 lines (58 loc) · 1.41 KB
/
slu.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
import Layer from './base.js'
/**
* Softplus linear unit layer
*/
export default class SoftplusLinearUnitLayer extends Layer {
/**
* @param {object} config config
* @param {number} [config.alpha] alpha
* @param {number} [config.beta] beta
* @param {number} [config.gamma] gamma
*/
constructor({ alpha = 1, beta = 1, gamma = 0, ...rest }) {
super(rest)
this._alpha = alpha
this._beta = beta
this._gamma = gamma
}
calc(x) {
this._i = x
const o = x.copy()
o.map(v => (v >= 0 ? this._alpha * v : this._beta * Math.log(Math.exp(v) + 1) - this._gamma))
return o
}
grad(bo) {
this._bo = bo
const bi = bo.copy()
bi.broadcastOperate(
this._i,
(a, b) => a * (b >= 0 ? this._alpha : (this._beta * Math.exp(b)) / (Math.exp(b) + 1))
)
return bi
}
update(optimizer) {
let sa = 0
let sb = 0
let sg = 0
for (let i = 0; i < this._i.length; i++) {
if (this._i.value[i] >= 0) {
sa += this._bo.value[i] * this._i.value[i]
} else {
sb += this._bo.value[i] * Math.log(Math.exp(this._i.value[i]) + 1)
sg += this._bo.value[i]
}
}
this._alpha -= optimizer.delta('alpha', sa / this._i.length)
this._beta -= optimizer.delta('beta', sb / this._i.length)
this._gamma -= optimizer.delta('gamma', sg / this._i.length)
}
toObject() {
return {
type: 'slu',
alpha: this._alpha,
beta: this._beta,
gamma: this._gamma,
}
}
}
SoftplusLinearUnitLayer.registLayer('slu')