Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/batch_normalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import {validateBatchNormalizationParams} from './lib/validate-input.js';
* @param {MLBatchNormalizationOptions} [options]
* @return {Tensor}
*/
export function batchNormalization(input, mean, variance, {axis=1, scale, bias, epsilon=1e-5,
activation = (x) => x} = {}) {
export function batchNormalization(input, mean, variance, {axis=1, scale, bias, epsilon=1e-5}) {
validateBatchNormalizationParams(...arguments);
// The output tensor has the same shape as the input tensor.
let output = new Tensor(input.shape);
Expand All @@ -30,6 +29,5 @@ export function batchNormalization(input, mean, variance, {axis=1, scale, bias,
if (bias) {
output = add(output, reshape(bias, shape));
}
output = activation(output);
return output;
}
3 changes: 0 additions & 3 deletions src/conv2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export function conv2d(
strides = [1, 1],
groups = 1,
dilations = [1, 1],
activation = (x) => x,
inputLayout = 'nchw',
filterLayout = 'oihw',
bias,
Expand Down Expand Up @@ -118,8 +117,6 @@ export function conv2d(
}
}

output = activation(output);

if (inputLayout === 'nhwc') {
// nchw -> nhwc
output = transpose(output, {permutation: [0, 2, 3, 1]});
Expand Down
3 changes: 0 additions & 3 deletions src/conv_transpose2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export function convTranspose2d(
dilations = [1, 1],
outputPadding = [0, 0],
outputSizes,
activation = (x) => x,
inputLayout = 'nchw',
filterLayout = 'iohw',
bias,
Expand Down Expand Up @@ -154,8 +153,6 @@ export function convTranspose2d(
}
}

output = activation(output);

if (inputLayout === 'nhwc') {
// nchw -> nhwc
output = transpose(output, {permutation: [0, 2, 3, 1]});
Expand Down
94 changes: 7 additions & 87 deletions test/batch_normalization_test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
'use strict';

import {batchNormalization} from '../src/batch_normalization.js';
import {clamp} from '../src/clamp.js';
import {leakyRelu} from '../src/leaky_relu.js';
import {relu} from '../src/relu.js';
import {sigmoid} from '../src/sigmoid.js';
import {Tensor} from '../src/lib/tensor.js';
import * as utils from './utils.js';

describe('test batchNormalization', function() {
function testBatchNorm(
input, mean, variance, expected, scale = undefined, bias = undefined,
options = {}, activation = undefined, activationOptions = {}) {
options = {}) {
const inputTensor = new Tensor(input.shape, input.data);
const meanTensor = new Tensor(mean.shape, mean.data);
const varianceTensor = new Tensor(variance.shape, variance.data);
Expand All @@ -21,15 +17,6 @@ describe('test batchNormalization', function() {
if (bias) {
options.bias = new Tensor(bias.shape, bias.data);
}
if (activation === 'relu') {
options.activation = relu;
} else if (activation === 'relu6') {
options.activation = utils.bindTrailingArgs(clamp, {minValue: 0, maxValue: 6});
} else if (activation === 'sigmoid') {
options.activation = sigmoid;
} else if (activation === 'leakyRelu') {
options.activation = utils.bindTrailingArgs(leakyRelu, activationOptions);
}
const outputTensor = batchNormalization(inputTensor, meanTensor, varianceTensor, options);
utils.checkShape(outputTensor, input.shape);
utils.checkValue(outputTensor, expected);
Expand Down Expand Up @@ -120,7 +107,7 @@ describe('test batchNormalization', function() {
shape: [2],
data: [0, 1],
};
let expected = [
const expected = [
-0.9999950000374997,
0,
0.9999950000374997,
Expand All @@ -130,17 +117,7 @@ describe('test batchNormalization', function() {
];
testBatchNorm(input, mean, variance, expected, scale, bias);

expected = [
0,
0,
0.9999950000374997,
0,
1,
2.224740788929097,
];
testBatchNorm(input, mean, variance, expected, scale, bias, {}, 'relu');

let expectedScale = [
const expectedScale = [
-0.9999950000374997,
0,
0.9999950000374997,
Expand All @@ -150,18 +127,7 @@ describe('test batchNormalization', function() {
];
testBatchNorm(input, mean, variance, expectedScale, scale);

expectedScale = [
0,
0,
0.9999950000374997,
0,
0,
1.2247407889290967,
];
testBatchNorm(
input, mean, variance, expectedScale, scale, undefined, {}, 'relu');

let expectedBias = [
const expectedBias = [
-0.9999950000374997,
0,
0.9999950000374997,
Expand All @@ -170,17 +136,6 @@ describe('test batchNormalization', function() {
1.8164938592860644,
];
testBatchNorm(input, mean, variance, expectedBias, undefined, bias);

expectedBias = [
0,
0,
0.9999950000374997,
0.18350614071393556,
1,
1.8164938592860644,
];
testBatchNorm(
input, mean, variance, expectedBias, undefined, bias, {}, 'relu');
});

it('batchNormalization nhwc', function() {
Expand All @@ -204,7 +159,7 @@ describe('test batchNormalization', function() {
shape: [2],
data: [0, 1],
};
let expected = [
const expected = [
-0.9999950000374997,
-0.22474078892909666,
0,
Expand All @@ -214,18 +169,7 @@ describe('test batchNormalization', function() {
];
testBatchNorm(input, mean, variance, expected, scale, bias, {axis: 3});

expected = [
0,
0,
0,
1,
0.9999950000374997,
2.224740788929097,
];
testBatchNorm(
input, mean, variance, expected, scale, bias, {axis: 3}, 'relu');

let expectedScale = [
const expectedScale = [
-0.9999950000374997,
-1.2247407889290967,
0,
Expand All @@ -236,19 +180,7 @@ describe('test batchNormalization', function() {
testBatchNorm(
input, mean, variance, expectedScale, scale, undefined, {axis: 3});

expectedScale = [
0,
0,
0,
0,
0.9999950000374997,
1.2247407889290967,
];
testBatchNorm(
input, mean, variance, expectedScale, scale, undefined, {axis: 3},
'relu');

let expectedBias = [
const expectedBias = [
-0.9999950000374997,
0.18350614071393556,
0,
Expand All @@ -258,18 +190,6 @@ describe('test batchNormalization', function() {
];
testBatchNorm(
input, mean, variance, expectedBias, undefined, bias, {axis: 3});

expectedBias = [
0,
0.18350614071393556,
0,
1,
0.9999950000374997,
1.8164938592860644,
];
testBatchNorm(
input, mean, variance, expectedBias, undefined, bias, {axis: 3},
'relu');
});

it('batchNormalization without options', function() {
Expand Down
Loading