/
split.js
60 lines (55 loc) · 1.11 KB
/
split.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
import Layer from './base.js'
/**
* Split layer
*/
export default class SplitLayer extends Layer {
/**
* @param {object} config config
* @param {number} [config.axis] axis
* @param {number | number[]} config.size size
*/
constructor({ axis = 1, size, ...rest }) {
super(rest)
this._axis = axis
this._size = size
}
calc(x) {
let c = 0
this._o = []
if (typeof this._size === 'number') {
const s = x.sizes[this._axis] / this._size
for (let i = 0; i < this._size; i++) {
const n = Math.round((i + 1) * s)
this._o.push(x.slice(c, n, this._axis))
c = n
}
} else {
for (let i = 0; i < this._size.length; i++) {
this._o.push(x.slice(c, c + this._size[i], this._axis))
c += this._size[i]
}
}
return this._o
}
grad(...bo) {
for (let i = 0; i < this._o.length; i++) {
if (!bo[i]) {
bo[i] = this._o[i].copy()
bo[i].fill(0)
}
}
let bi = bo[0]
for (let i = 1; i < bo.length; i++) {
bi.concat(bo[i], this._axis)
}
return bi
}
toObject() {
return {
type: 'split',
axis: this._axis,
size: this._size,
}
}
}
SplitLayer.registLayer()