/
reduce_max.js
64 lines (59 loc) · 1.57 KB
/
reduce_max.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'
import Matrix from '../../../util/matrix.js'
import Tensor from '../../../util/tensor.js'
/**
* Reduce max layer
*/
export default class ReduceMaxLayer extends Layer {
/**
* @param {object} config config
* @param {number | number[] | string} [config.axis] axis
* @param {boolean} [config.keepdims] Keep dimensions or not
*/
constructor({ axis = -1, keepdims = true, ...rest }) {
super(rest)
this._axis = typeof axis === 'number' ? [axis] : axis
this._keepdims = keepdims
}
calc(x) {
const a = typeof this._axis === 'string' ? this.graph.getNode(this._axis).outputValue.toArray() : this._axis
if (a.includes(-1)) {
this._size = Array(x.dimension).fill(1)
} else {
this._size = x.sizes.concat()
for (let k = 0; k < a.length; k++) {
this._size[a[k]] = 1
}
}
this._i = x
if (!this._keepdims && a.includes(-1)) {
this._o = x.reduce((s, v) => Math.max(s, v), -Infinity)
return new Tensor([], this._o)
}
if (!this._keepdims && x instanceof Matrix) {
x = Tensor.fromArray(x)
}
this._o = x.reduce((s, v) => Math.max(s, v), -Infinity, a, true)
if (!this._keepdims) {
const o = this._o.copy()
o.reshape(...o.sizes.filter((v, k) => !a.includes(k)))
return o
}
return this._o
}
grad(bo) {
bo.reshape(...this._size)
const bi = this._i.copy()
bi.broadcastOperate(this._o, (a, b) => (b === a ? 1 : 0))
bi.broadcastOperate(bo, (a, b) => a * b)
return bi
}
toObject() {
return {
type: 'reduce_max',
axis: this._axis,
keepdims: this._keepdims,
}
}
}
ReduceMaxLayer.registLayer()