From 76a56b38b3e99950f4a06c90e9cbef8b7f3ab5cb Mon Sep 17 00:00:00 2001 From: Ishii Norimi <47292902+ishii-norimi@users.noreply.github.com> Date: Sat, 18 Nov 2023 13:10:53 +0900 Subject: [PATCH] Skip outside calculation in maxpool layer (#691) --- lib/model/nns/layer/maxpool.js | 35 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/lib/model/nns/layer/maxpool.js b/lib/model/nns/layer/maxpool.js index 5a11dfeb..9f5632da 100644 --- a/lib/model/nns/layer/maxpool.js +++ b/lib/model/nns/layer/maxpool.js @@ -73,15 +73,12 @@ export default class MaxPoolLayer extends Layer { const offset = Array(x.dimension - 2).fill(0) let maxval = -Infinity do { - const p = idx.map((v, i) => - Math.max( - 0, - Math.min(x.sizes[i + koff] - 1, v * this._stride[i] - this._padding[i][0] + offset[i]) - ) - ) - const v = x.at(this._index(i, c, p)) - if (maxval < v) { - maxval = v + const p = idx.map((v, i) => v * this._stride[i] - this._padding[i][0] + offset[i]) + if (p.every((v, i) => 0 <= v && v < x.sizes[i + koff])) { + const v = x.at(this._index(i, c, p)) + if (maxval < v) { + maxval = v + } } for (let k = 0; k < offset.length; k++) { offset[k]++ @@ -118,19 +115,13 @@ export default class MaxPoolLayer extends Layer { let maxval = -Infinity let maxidx = null do { - const p = idx.map((v, i) => - Math.max( - 0, - Math.min( - this._i.sizes[i + koff] - 1, - v * this._stride[i] - this._padding[i][0] + offset[i] - ) - ) - ) - const v = this._i.at(this._index(i, c, p)) - if (maxval < v) { - maxval = v - maxidx = p + const p = idx.map((v, i) => v * this._stride[i] - this._padding[i][0] + offset[i]) + if (p.every((v, i) => 0 <= v && v < this._i.sizes[i + koff])) { + const v = this._i.at(this._index(i, c, p)) + if (maxval < v) { + maxval = v + maxidx = p + } } for (let k = 0; k < offset.length; k++) { offset[k]++