Skip to content

Commit

Permalink
revised l2pool2d
Browse files Browse the repository at this point in the history
  • Loading branch information
mei1127 committed Jan 10, 2024
1 parent de9785c commit de57d69
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 72 deletions.
1 change: 0 additions & 1 deletion src/pool2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]});
Expand Down
5 changes: 4 additions & 1 deletion src/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
140 changes: 70 additions & 70 deletions test/l2pool2d_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
});

Expand All @@ -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,
]);
});

Expand All @@ -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,
]);
});

Expand All @@ -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,
]);
});

Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand Down

0 comments on commit de57d69

Please sign in to comment.