From aa60c41f233179889dd829d455e06ae40d859706 Mon Sep 17 00:00:00 2001 From: mei1127 Date: Thu, 21 Dec 2023 15:02:07 +0800 Subject: [PATCH 1/8] implement l2Pool2d --- src/pool2d.js | 13 ++++++++++++- src/reduce.js | 13 ++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/pool2d.js b/src/pool2d.js index 85fd174..18603f8 100644 --- a/src/pool2d.js +++ b/src/pool2d.js @@ -3,7 +3,7 @@ import {computePaddingForAutoPad} from './lib/compute-padding.js'; import {Tensor} from './lib/tensor.js'; import {transpose} from './transpose.js'; -import {meanReducer, maxReducer} from './reduce.js'; +import {meanReducer, maxReducer, l2Reducer} from './reduce.js'; import {validatePool2dParams} from './lib/validate-input.js'; /** @@ -125,3 +125,14 @@ export function averagePool2d(input, options = {}) { export function maxPool2d(input, options = {}) { return pool2d(input, maxReducer, options); } + +/** + * Compute a L2 reduction operation across all the elements within the moving window over + * the input tensor. + * @param {Tensor} input + * @param {MLPool2dOptions} options + * @return {Tensor} + */ +export function l2Pool2d(input, options = {}) { + return pool2d(input, l2Reducer, options); +} diff --git a/src/reduce.js b/src/reduce.js index 390fa47..dfc0cb0 100644 --- a/src/reduce.js +++ b/src/reduce.js @@ -2,7 +2,7 @@ import {pow} from './binary.js'; import {squeeze} from './squeeze.js'; -import {abs, exp, log} from './unary.js'; +import {abs, exp, log, sqrt} from './unary.js'; import {sizeOfShape, Scalar, Tensor} from './lib/tensor.js'; import {validateReduceParams} from './lib/validate-input.js'; @@ -143,6 +143,17 @@ export function reduceL1(input, options = {}) { return reduceSum(abs(input), options); } +/* The l2 reducer */ +export function l2Reducer(previousValue, currentValue, currentIndex, array) { + if (currentIndex === array.length - 1) { + const sumOfSquares = previousValue + currentValue * currentValue; + return sqrt(sumOfSquares); + } else { + const sumOfSquares = previousValue + currentValue * currentValue; + return sumOfSquares; + } +} + /** * Compute the L2 norm of all the input values along the axes. * @param {Tensor} input From a51db0a8d77311296485597c546d15fb48fb4184 Mon Sep 17 00:00:00 2001 From: mei1127 Date: Thu, 21 Dec 2023 16:45:31 +0800 Subject: [PATCH 2/8] completed l2pool2d --- src/pool2d.js | 2 +- src/reduce.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pool2d.js b/src/pool2d.js index 18603f8..50fd3e6 100644 --- a/src/pool2d.js +++ b/src/pool2d.js @@ -3,7 +3,7 @@ import {computePaddingForAutoPad} from './lib/compute-padding.js'; import {Tensor} from './lib/tensor.js'; import {transpose} from './transpose.js'; -import {meanReducer, maxReducer, l2Reducer} from './reduce.js'; +import {l2Reducer, meanReducer, maxReducer} from './reduce.js'; import {validatePool2dParams} from './lib/validate-input.js'; /** diff --git a/src/reduce.js b/src/reduce.js index dfc0cb0..7ede5f3 100644 --- a/src/reduce.js +++ b/src/reduce.js @@ -2,7 +2,7 @@ import {pow} from './binary.js'; import {squeeze} from './squeeze.js'; -import {abs, exp, log, sqrt} from './unary.js'; +import {abs, exp, log} from './unary.js'; import {sizeOfShape, Scalar, Tensor} from './lib/tensor.js'; import {validateReduceParams} from './lib/validate-input.js'; @@ -147,7 +147,7 @@ export function reduceL1(input, options = {}) { export function l2Reducer(previousValue, currentValue, currentIndex, array) { if (currentIndex === array.length - 1) { const sumOfSquares = previousValue + currentValue * currentValue; - return sqrt(sumOfSquares); + return Math.sqrt(sumOfSquares); } else { const sumOfSquares = previousValue + currentValue * currentValue; return sumOfSquares; From de9785cca873f95625c5091bb3501a3b63a95408 Mon Sep 17 00:00:00 2001 From: mei1127 Date: Fri, 22 Dec 2023 15:32:25 +0800 Subject: [PATCH 3/8] completed l2pool2d_test.js --- test/l2pool2d_test.js | 264 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 264 insertions(+) create mode 100644 test/l2pool2d_test.js diff --git a/test/l2pool2d_test.js b/test/l2pool2d_test.js new file mode 100644 index 0000000..ba80a46 --- /dev/null +++ b/test/l2pool2d_test.js @@ -0,0 +1,264 @@ +'use strict'; +import {l2Pool2d} from '../src/pool2d.js'; +import {Tensor} from '../src/lib/tensor.js'; + +import * as utils from './utils.js'; + +describe('test pool2d', function() { + it('l2Pool2d default', function() { + const x = new Tensor([1, 1, 4, 4], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); + const windowDimensions = [3, 3]; + const y = l2Pool2d(x, {windowDimensions}); + utils.checkShape(y, [1, 1, 2, 2]); + utils.checkValue(y, [ + 20.639767440550294, + 23.259406699226016, + 31.336879231984796, + 34.07345007480164, + ]); + }); + + it('l2Pool2d nhwc', function() { + const x = new Tensor([1, 4, 4, 1], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); + const windowDimensions = [3, 3]; + const layout = 'nhwc'; + const y = l2Pool2d(x, {windowDimensions, layout}); + utils.checkShape(y, [1, 2, 2, 1]); + utils.checkValue(y, [ + 20.639767440550294, + 23.259406699226016, + 31.336879231984796, + 34.07345007480164, + ]); + }); + + it('l2Pool2d dilations default', function() { + const x = new Tensor([1, 1, 4, 4], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); + const windowDimensions = [2, 2]; + const dilations = [2, 2]; + const y = l2Pool2d(x, {windowDimensions, dilations}); + utils.checkShape(y, [1, 1, 2, 2]); + utils.checkValue(y, [ + 14.560219778561036, + 16.186414056238647, + 21.166010488516726, + 22.847319317591726, + ]); + }); + + it('l2Pool2d dilations nhwc', function() { + const x = new Tensor([1, 4, 4, 1], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); + const windowDimensions = [2, 2]; + const dilations = [2, 2]; + const layout = 'nhwc'; + const y = l2Pool2d(x, {windowDimensions, dilations, layout}); + utils.checkShape(y, [1, 2, 2, 1]); + utils.checkValue(y, [ + 14.560219778561036, + 16.186414056238647, + 21.166010488516726, + 22.847319317591726, + ]); + }); + + it('l2Pool2d pads default', function() { + const x = new Tensor([1, 1, 5, 5], [ + 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, + ]); + const windowDimensions = [5, 5]; + const padding = [2, 2, 2, 2]; + const y = l2Pool2d(x, {windowDimensions, padding}); + utils.checkShape(y, [1, 1, 5, 5]); + const expected = [ + 24.43358344574123, 29.832867780352597, + 35.21363372331802, 32.863353450309965, + 29.647934160747187, 38.28837943815329, + 46.04345773288535, 53.5723809439155, + 49.53786430600334, 44.31703961232068, + 54.037024344425184, 64.42049363362563, + 74.33034373659252, 68.32276341015489, + 60.778285596090974, 53.62835071116769, + 63.953107821277925, 73.7563556583431, + 67.63135367564367, 59.94997914928745, + 51.44900387762624, 61.48170459575759, + 70.92249290598858, 64.73020933072904, + 57, + ]; + utils.checkValue(y, expected); + }); + + it('l2Pool2d pads nhwc', function() { + const x = new Tensor([1, 5, 5, 1], [ + 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, + ]); + const windowDimensions = [5, 5]; + const padding = [2, 2, 2, 2]; + const layout = 'nhwc'; + const y = l2Pool2d(x, {windowDimensions, padding, layout}); + utils.checkShape(y, [1, 5, 5, 1]); + const expected = [ + 24.43358344574123, 29.832867780352597, + 35.21363372331802, 32.863353450309965, + 29.647934160747187, 38.28837943815329, + 46.04345773288535, 53.5723809439155, + 49.53786430600334, 44.31703961232068, + 54.037024344425184, 64.42049363362563, + 74.33034373659252, 68.32276341015489, + 60.778285596090974, 53.62835071116769, + 63.953107821277925, 73.7563556583431, + 67.63135367564367, 59.94997914928745, + 51.44900387762624, 61.48170459575759, + 70.92249290598858, 64.73020933072904, + 57, + ]; + utils.checkValue(y, expected); + }); + + it('l2Pool2d autoPad same-upper default', function() { + const x = new Tensor([1, 1, 5, 5], [ + 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, + ]); + const windowDimensions = [5, 5]; + const autoPad = 'same-upper'; + const y = l2Pool2d(x, {windowDimensions, autoPad}); + utils.checkShape(y, [1, 1, 5, 5]); + const expected = [ + 24.43358344574123, 29.832867780352597, + 35.21363372331802, 32.863353450309965, + 29.647934160747187, 38.28837943815329, + 46.04345773288535, 53.5723809439155, + 49.53786430600334, 44.31703961232068, + 54.037024344425184, 64.42049363362563, + 74.33034373659252, 68.32276341015489, + 60.778285596090974, 53.62835071116769, + 63.953107821277925, 73.7563556583431, + 67.63135367564367, 59.94997914928745, + 51.44900387762624, 61.48170459575759, + 70.92249290598858, 64.73020933072904, + 57, + ]; + utils.checkValue(y, expected); + }); + + it('l2Pool2d autoPad explicit nhwc', function() { + const x = new Tensor([1, 7, 7, 1], [ + 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, + ]); + const windowDimensions = [4, 4]; + const padding = [2, 1, 2, 1]; + const strides = [2, 2]; + const autoPad = 'explicit'; + const layout = 'nhwc'; + const y = l2Pool2d( + x, {windowDimensions, autoPad, padding, strides, layout}); + utils.checkShape(y, [1, 4, 4, 1]); + const expected = [ + 12.24744871391589, 19.8997487421324, + 24.779023386727733, 24.474476501040833, + 40.54626986542659, 60.860496218811754, + 67.77905281132217, 63.166446789415026, + 75.43208866258443, 111.59749101122301, + 119.09659944767525, 107.53604047016051, + 85.901105930017, 128.33549781724463, + 134.90737563232042, 119.8874472161285, + ]; + utils.checkValue(y, expected); + }); + + it('l2Pool2d autoPad same-lower nhwc', function() { + const x = new Tensor([1, 7, 7, 1], [ + 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, + ]); + const windowDimensions = [4, 4]; + const strides = [2, 2]; + const autoPad = 'same-lower'; + const layout = 'nhwc'; + const y = + l2Pool2d(x, {windowDimensions, autoPad, strides, layout}); + utils.checkShape(y, [1, 4, 4, 1]); + const expected = [ + 12.24744871391589, 19.8997487421324, + 24.779023386727733, 24.474476501040833, + 40.54626986542659, 60.860496218811754, + 67.77905281132217, 63.166446789415026, + 75.43208866258443, 111.59749101122301, + 119.09659944767525, 107.53604047016051, + 85.901105930017, 128.33549781724463, + 134.90737563232042, 119.8874472161285, + ]; + utils.checkValue(y, expected); + }); + + it('l2Pool2d autoPad same-upper nhwc', function() { + const x = new Tensor([1, 5, 5, 1], [ + 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, + ]); + const windowDimensions = [5, 5]; + const autoPad = 'same-upper'; + const layout = 'nhwc'; + const y = l2Pool2d(x, {windowDimensions, autoPad, layout}); + + utils.checkShape(y, [1, 5, 5, 1]); + const expected = [ + 24.43358344574123, 29.832867780352597, + 35.21363372331802, 32.863353450309965, + 29.647934160747187, 38.28837943815329, + 46.04345773288535, 53.5723809439155, + 49.53786430600334, 44.31703961232068, + 54.037024344425184, 64.42049363362563, + 74.33034373659252, 68.32276341015489, + 60.778285596090974, 53.62835071116769, + 63.953107821277925, 73.7563556583431, + 67.63135367564367, 59.94997914928745, + 51.44900387762624, 61.48170459575759, + 70.92249290598858, 64.73020933072904, + 57, + ]; + utils.checkValue(y, expected); + }); + + it('l2Pool2d strides default', function() { + const x = new Tensor([1, 1, 5, 5], [ + 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, + ]); + const windowDimensions = [2, 2]; + const strides = [2, 2]; + const y = l2Pool2d(x, {windowDimensions, strides}); + utils.checkShape(y, [1, 1, 2, 2]); + const expected = [ + 9.486832980505138, + 12.806248474865697, + 26.457513110645905, + 29.899832775452108, + ]; + utils.checkValue(y, expected); + }); + + it('l2Pool2d strides nhwc', function() { + const x = new Tensor([1, 5, 5, 1], [ + 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, + ]); + const windowDimensions = [2, 2]; + const strides = [2, 2]; + const layout = 'nhwc'; + const y = l2Pool2d(x, {windowDimensions, strides, layout}); + utils.checkShape(y, [1, 2, 2, 1]); + const expected = [ + 9.486832980505138, + 12.806248474865697, + 26.457513110645905, + 29.899832775452108, + ]; + utils.checkValue(y, expected); + }); +}); From de57d69102125f772c2aef3269511eb3e28c39b0 Mon Sep 17 00:00:00 2001 From: mei1127 Date: Wed, 10 Jan 2024 14:09:07 +0800 Subject: [PATCH 4/8] revised l2pool2d --- src/pool2d.js | 1 - src/reduce.js | 5 +- test/l2pool2d_test.js | 140 +++++++++++++++++++++--------------------- 3 files changed, 74 insertions(+), 72 deletions(-) diff --git a/src/pool2d.js b/src/pool2d.js index 50fd3e6..1ee49e3 100644 --- a/src/pool2d.js +++ b/src/pool2d.js @@ -26,7 +26,6 @@ function pool2d(input, reductionFunc, }= {}) { validatePool2dParams(...arguments); const roundingFunc = roundingType === 'floor' ? Math.floor : Math.ceil; - if (layout === 'nhwc') { // nhwc -> nchw input = transpose(input, {permutation: [0, 3, 1, 2]}); diff --git a/src/reduce.js b/src/reduce.js index 7ede5f3..3e13d46 100644 --- a/src/reduce.js +++ b/src/reduce.js @@ -145,7 +145,10 @@ export function reduceL1(input, options = {}) { /* The l2 reducer */ export function l2Reducer(previousValue, currentValue, currentIndex, array) { - if (currentIndex === array.length - 1) { + if (currentIndex== 1) { + const sumOfSquares = previousValue*previousValue + currentValue * currentValue; + return sumOfSquares; + } else if (currentIndex === array.length - 1) { const sumOfSquares = previousValue + currentValue * currentValue; return Math.sqrt(sumOfSquares); } else { diff --git a/test/l2pool2d_test.js b/test/l2pool2d_test.js index ba80a46..0237af5 100644 --- a/test/l2pool2d_test.js +++ b/test/l2pool2d_test.js @@ -12,9 +12,9 @@ describe('test pool2d', function() { utils.checkShape(y, [1, 1, 2, 2]); utils.checkValue(y, [ 20.639767440550294, - 23.259406699226016, - 31.336879231984796, - 34.07345007480164, + 23.302360395462088, + 31.654383582688826, + 34.51086785347479, ]); }); @@ -26,9 +26,9 @@ describe('test pool2d', function() { utils.checkShape(y, [1, 2, 2, 1]); utils.checkValue(y, [ 20.639767440550294, - 23.259406699226016, - 31.336879231984796, - 34.07345007480164, + 23.302360395462088, + 31.654383582688826, + 34.51086785347479, ]); }); @@ -40,9 +40,9 @@ describe('test pool2d', function() { utils.checkShape(y, [1, 1, 2, 2]); utils.checkValue(y, [ 14.560219778561036, - 16.186414056238647, - 21.166010488516726, - 22.847319317591726, + 16.24807680927192, + 21.633307652783937, + 23.49468024894146, ]); }); @@ -55,9 +55,9 @@ describe('test pool2d', function() { utils.checkShape(y, [1, 2, 2, 1]); utils.checkValue(y, [ 14.560219778561036, - 16.186414056238647, - 21.166010488516726, - 22.847319317591726, + 16.24807680927192, + 21.633307652783937, + 23.49468024894146, ]); }); @@ -72,18 +72,18 @@ describe('test pool2d', function() { utils.checkShape(y, [1, 1, 5, 5]); const expected = [ 24.43358344574123, 29.832867780352597, - 35.21363372331802, 32.863353450309965, - 29.647934160747187, 38.28837943815329, + 35.21363372331802, 32.89376840679705, + 29.748949561287034, 38.28837943815329, 46.04345773288535, 53.5723809439155, - 49.53786430600334, 44.31703961232068, + 49.558046773455466, 44.384682042344295, 54.037024344425184, 64.42049363362563, - 74.33034373659252, 68.32276341015489, - 60.778285596090974, 53.62835071116769, - 63.953107821277925, 73.7563556583431, - 67.63135367564367, 59.94997914928745, - 51.44900387762624, 61.48170459575759, - 70.92249290598858, 64.73020933072904, - 57, + 74.33034373659252, 68.33739825307956, + 60.8276253029822, 53.907327887774215, + 64.18722614352485, 73.95944834840239, + 67.94115100585212, 60.41522986797286, + 52.507142371300304, 62.369864518050704, + 71.69379331573968, 65.7419196555744, + 58.35237784358063, ]; utils.checkValue(y, expected); }); @@ -100,18 +100,18 @@ describe('test pool2d', function() { utils.checkShape(y, [1, 5, 5, 1]); const expected = [ 24.43358344574123, 29.832867780352597, - 35.21363372331802, 32.863353450309965, - 29.647934160747187, 38.28837943815329, + 35.21363372331802, 32.89376840679705, + 29.748949561287034, 38.28837943815329, 46.04345773288535, 53.5723809439155, - 49.53786430600334, 44.31703961232068, + 49.558046773455466, 44.384682042344295, 54.037024344425184, 64.42049363362563, - 74.33034373659252, 68.32276341015489, - 60.778285596090974, 53.62835071116769, - 63.953107821277925, 73.7563556583431, - 67.63135367564367, 59.94997914928745, - 51.44900387762624, 61.48170459575759, - 70.92249290598858, 64.73020933072904, - 57, + 74.33034373659252, 68.33739825307956, + 60.8276253029822, 53.907327887774215, + 64.18722614352485, 73.95944834840239, + 67.94115100585212, 60.41522986797286, + 52.507142371300304, 62.369864518050704, + 71.69379331573968, 65.7419196555744, + 58.35237784358063, ]; utils.checkValue(y, expected); }); @@ -127,18 +127,18 @@ describe('test pool2d', function() { utils.checkShape(y, [1, 1, 5, 5]); const expected = [ 24.43358344574123, 29.832867780352597, - 35.21363372331802, 32.863353450309965, - 29.647934160747187, 38.28837943815329, + 35.21363372331802, 32.89376840679705, + 29.748949561287034, 38.28837943815329, 46.04345773288535, 53.5723809439155, - 49.53786430600334, 44.31703961232068, + 49.558046773455466, 44.384682042344295, 54.037024344425184, 64.42049363362563, - 74.33034373659252, 68.32276341015489, - 60.778285596090974, 53.62835071116769, - 63.953107821277925, 73.7563556583431, - 67.63135367564367, 59.94997914928745, - 51.44900387762624, 61.48170459575759, - 70.92249290598858, 64.73020933072904, - 57, + 74.33034373659252, 68.33739825307956, + 60.8276253029822, 53.907327887774215, + 64.18722614352485, 73.95944834840239, + 67.94115100585212, 60.41522986797286, + 52.507142371300304, 62.369864518050704, + 71.69379331573968, 65.7419196555744, + 58.35237784358063, ]; utils.checkValue(y, expected); }); @@ -159,13 +159,13 @@ describe('test pool2d', function() { utils.checkShape(y, [1, 4, 4, 1]); const expected = [ 12.24744871391589, 19.8997487421324, - 24.779023386727733, 24.474476501040833, + 24.899799195977465, 24.879710609249457, 40.54626986542659, 60.860496218811754, - 67.77905281132217, 63.166446789415026, - 75.43208866258443, 111.59749101122301, - 119.09659944767525, 107.53604047016051, - 85.901105930017, 128.33549781724463, - 134.90737563232042, 119.8874472161285, + 67.82329983125268, 63.324560795950255, + 76.81145747868608, 112.5344391730816, + 120.2331069215131, 109.1146186356347, + 90.50414355155237, 131.4610208388783, + 138.31124321616085, 124.21352583354198, ]; utils.checkValue(y, expected); }); @@ -185,13 +185,13 @@ describe('test pool2d', function() { utils.checkShape(y, [1, 4, 4, 1]); const expected = [ 12.24744871391589, 19.8997487421324, - 24.779023386727733, 24.474476501040833, + 24.899799195977465, 24.879710609249457, 40.54626986542659, 60.860496218811754, - 67.77905281132217, 63.166446789415026, - 75.43208866258443, 111.59749101122301, - 119.09659944767525, 107.53604047016051, - 85.901105930017, 128.33549781724463, - 134.90737563232042, 119.8874472161285, + 67.82329983125268, 63.324560795950255, + 76.81145747868608, 112.5344391730816, + 120.2331069215131, 109.1146186356347, + 90.50414355155237, 131.4610208388783, + 138.31124321616085, 124.21352583354198, ]; utils.checkValue(y, expected); }); @@ -209,18 +209,18 @@ describe('test pool2d', function() { utils.checkShape(y, [1, 5, 5, 1]); const expected = [ 24.43358344574123, 29.832867780352597, - 35.21363372331802, 32.863353450309965, - 29.647934160747187, 38.28837943815329, + 35.21363372331802, 32.89376840679705, + 29.748949561287034, 38.28837943815329, 46.04345773288535, 53.5723809439155, - 49.53786430600334, 44.31703961232068, + 49.558046773455466, 44.384682042344295, 54.037024344425184, 64.42049363362563, - 74.33034373659252, 68.32276341015489, - 60.778285596090974, 53.62835071116769, - 63.953107821277925, 73.7563556583431, - 67.63135367564367, 59.94997914928745, - 51.44900387762624, 61.48170459575759, - 70.92249290598858, 64.73020933072904, - 57, + 74.33034373659252, 68.33739825307956, + 60.8276253029822, 53.907327887774215, + 64.18722614352485, 73.95944834840239, + 67.94115100585212, 60.41522986797286, + 52.507142371300304, 62.369864518050704, + 71.69379331573968, 65.7419196555744, + 58.35237784358063, ]; utils.checkValue(y, expected); }); @@ -236,9 +236,9 @@ describe('test pool2d', function() { utils.checkShape(y, [1, 1, 2, 2]); const expected = [ 9.486832980505138, - 12.806248474865697, - 26.457513110645905, - 29.899832775452108, + 13.038404810405298, + 28.460498941515414, + 32.4037034920393, ]; utils.checkValue(y, expected); }); @@ -255,9 +255,9 @@ describe('test pool2d', function() { utils.checkShape(y, [1, 2, 2, 1]); const expected = [ 9.486832980505138, - 12.806248474865697, - 26.457513110645905, - 29.899832775452108, + 13.038404810405298, + 28.460498941515414, + 32.4037034920393, ]; utils.checkValue(y, expected); }); From 46ce01c16f90bb8c1a074e0b41a96e39e309f8df Mon Sep 17 00:00:00 2001 From: mei1127 Date: Fri, 12 Jan 2024 16:58:28 +0800 Subject: [PATCH 5/8] revised l2pool2d --- src/reduce.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/reduce.js b/src/reduce.js index 3e13d46..5806d35 100644 --- a/src/reduce.js +++ b/src/reduce.js @@ -145,15 +145,12 @@ export function reduceL1(input, options = {}) { /* The l2 reducer */ export function l2Reducer(previousValue, currentValue, currentIndex, array) { - if (currentIndex== 1) { - const sumOfSquares = previousValue*previousValue + currentValue * currentValue; + if (currentIndex == 1) { + const sumOfSquares = previousValue * previousValue + currentValue * currentValue; return sumOfSquares; - } else if (currentIndex === array.length - 1) { - const sumOfSquares = previousValue + currentValue * currentValue; - return Math.sqrt(sumOfSquares); } else { const sumOfSquares = previousValue + currentValue * currentValue; - return sumOfSquares; + return (currentIndex === array.length - 1) ? Math.sqrt(sumOfSquares) :sumOfSquares; } } From ef5af87f60eca94743ec877080bf93b2773639cd Mon Sep 17 00:00:00 2001 From: mei1127 Date: Fri, 12 Jan 2024 17:07:49 +0800 Subject: [PATCH 6/8] revised l2pool2d_test.js --- test/l2pool2d_test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/l2pool2d_test.js b/test/l2pool2d_test.js index 0237af5..a8988bb 100644 --- a/test/l2pool2d_test.js +++ b/test/l2pool2d_test.js @@ -5,6 +5,14 @@ import {Tensor} from '../src/lib/tensor.js'; import * as utils from './utils.js'; describe('test pool2d', function() { + it('l2Pool2d default', function() { + const x = new Tensor([1, 1, 1, 1], [2]); + const windowDimensions = [1, 1]; + const y = l2Pool2d(x, {windowDimensions}); + utils.checkShape(y, [1, 1, 1, 1]); + utils.checkValue(y, [2]); + }); + it('l2Pool2d default', function() { const x = new Tensor([1, 1, 4, 4], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); const windowDimensions = [3, 3]; From 5d9c2f3dc7c2ac05ace323ce42049c061ea52b05 Mon Sep 17 00:00:00 2001 From: mei1127 Date: Tue, 16 Jan 2024 10:05:23 +0800 Subject: [PATCH 7/8] revised l2pooled_test.js --- test/l2pool2d_test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/l2pool2d_test.js b/test/l2pool2d_test.js index a8988bb..4438192 100644 --- a/test/l2pool2d_test.js +++ b/test/l2pool2d_test.js @@ -26,6 +26,19 @@ describe('test pool2d', function() { ]); }); + it('l2Pool2d default: start not from 1 ', function() { + const x = new Tensor([1, 1, 4, 4], [16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); + const windowDimensions = [3, 3]; + const y = l2Pool2d(x, {windowDimensions}); + utils.checkShape(y, [1, 1, 2, 2]); + utils.checkValue(y, [ + 34.51086785347479, + 31.654383582688826, + 23.302360395462088, + 20.639767440550294, + ]); + }); + it('l2Pool2d nhwc', function() { const x = new Tensor([1, 4, 4, 1], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); const windowDimensions = [3, 3]; From 3b3e02a5d231e5e5642b5ee37d297fa4292155d2 Mon Sep 17 00:00:00 2001 From: mei1127 Date: Tue, 16 Jan 2024 10:08:58 +0800 Subject: [PATCH 8/8] revised l2pool2d_test.js --- test/l2pool2d_test.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/l2pool2d_test.js b/test/l2pool2d_test.js index 4438192..bb565d3 100644 --- a/test/l2pool2d_test.js +++ b/test/l2pool2d_test.js @@ -5,14 +5,6 @@ import {Tensor} from '../src/lib/tensor.js'; import * as utils from './utils.js'; describe('test pool2d', function() { - it('l2Pool2d default', function() { - const x = new Tensor([1, 1, 1, 1], [2]); - const windowDimensions = [1, 1]; - const y = l2Pool2d(x, {windowDimensions}); - utils.checkShape(y, [1, 1, 1, 1]); - utils.checkValue(y, [2]); - }); - it('l2Pool2d default', function() { const x = new Tensor([1, 1, 4, 4], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); const windowDimensions = [3, 3]; @@ -26,6 +18,14 @@ describe('test pool2d', function() { ]); }); + it('l2Pool2d default: a single element test case', function() { + const x = new Tensor([1, 1, 1, 1], [2]); + const windowDimensions = [1, 1]; + const y = l2Pool2d(x, {windowDimensions}); + utils.checkShape(y, [1, 1, 1, 1]); + utils.checkValue(y, [2]); + }); + it('l2Pool2d default: start not from 1 ', function() { const x = new Tensor([1, 1, 4, 4], [16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); const windowDimensions = [3, 3];