tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.36k stars 1.92k forks source link

BatchNormalization does not work if scale=false while training #2868

Open 1-bite-only opened 4 years ago

1-bite-only commented 4 years ago

TensorFlow.js version

tfjs-node: 1.5.2

Browser version

Describe the problem

TypeError: Cannot read property 'read' of undefined
    at node_modules\@tensorflow\tfjs-layers\dist\layers\normalization.js:240:66
    at node_modules\@tensorflow\tfjs-core\dist\engine.js:388:22
    at Engine.scopedRun (node_modules\@tensorflow\tfjs-core\dist\engine.js:398:23)
    at Engine.tidy (node_modules\@tensorflow\tfjs-core\dist\engine.js:387:21)
    at Object.tidy (node_modules\@tensorflow\tfjs-core\dist\globals.js:176:28)
    at BatchNormalization.call (node_modules\@tensorflow\tfjs-layers\dist\layers\normalization.js:212:28)
    at Object.nameScope (node_modules\@tensorflow\tfjs-layers\dist\common.js:41:19)
    at BatchNormalization.Layer.apply (node_modules\@tensorflow\tfjs-layers\dist\engine\topology.js:719:25)
    at Object.execute (node_modules\@tensorflow\tfjs-layers\dist\engine\executor.js:282:61)

After replace '_this.gamma.read()' with '_this.gamma == null ? null : _this.gamma.read()' in normalization.js:240, will get following error:

TypeError: Cannot read property 'dataId' of undefined
    at Engine.clone (node_modules\@tensorflow\tfjs-core\dist\engine.js:423:45)
    at node_modules\@tensorflow\tfjs-core\dist\engine.js:497:77
    at Array.map (<anonymous>)
    at saveFunc (node_modules\@tensorflow\tfjs-core\dist\engine.js:497:29)
    at engine_1.ENGINE.runKernelFunc.x (node_modules\@tensorflow\tfjs-core\dist\ops\batchnorm.js:281:9)
    at node_modules\@tensorflow\tfjs-core\dist\engine.js:528:55
    at node_modules\@tensorflow\tfjs-core\dist\engine.js:388:22
    at Engine.scopedRun (node_modules\@tensorflow\tfjs-core\dist\engine.js:398:23)
    at Engine.tidy (node_modules\@tensorflow\tfjs-core\dist\engine.js:387:21)
    at kernelFunc (node_modules\@tensorflow\tfjs-core\dist\engine.js:528:29)

Code to reproduce the bug

Add batch normalization layer with following config and train will reproduce the issue:

tf.layers.batchNormalization({
    epsilon: 1e-5,
    axis: 3,
    center: true,
    scale: false,
  })
rthadur commented 4 years ago

@Merlin-Zeng can you please provide reproduction code or an example in codepen.

1-bite-only commented 4 years ago

@rthadur here you go

const tf = require("@tensorflow/tfjs-node");

const model = tf.sequential({
  layers: [
    tf.layers.conv2d({filters: 1, kernelSize: 3, padding: 'same', inputShape: [15,15,3]}),
    tf.layers.batchNormalization({epsilon: 1e-5, axis: 3, center: true, scale: false}),
  ],
});

model.compile({
  optimizer: 'adam',
  loss: 'meanSquaredError',
  metrics: 'accuracy',
});

model.fit(tf.ones([1,15,15,3]), tf.ones([1,15,15,1])).then(console.log);
spazmodius commented 4 years ago

Same thing seems to happen when specifying { center: false }

Epoch 1 / 10
(node:5784) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'read' of undefined
    at node_modules\@tensorflow\tfjs-layers\dist\tf-layers.node.js:14682:85
    at node_modules\@tensorflow\tfjs-core\dist\tf-core.node.js:3075:22
    at Engine.scopedRun (node_modules\@tensorflow\tfjs-core\dist\tf-core.node.js:3085:23)
    at Engine.tidy (node_modules\@tensorflow\tfjs-core\dist\tf-core.node.js:3074:21)
    at Object.tidy (node_modules\@tensorflow\tfjs-core\dist\tf-core.node.js:15138:19)
    at BatchNormalization.call (node_modules\@tensorflow\tfjs-layers\dist\tf-layers.node.js:14654:20)
    at node_modules\@tensorflow\tfjs-layers\dist\tf-layers.node.js:3370:36
    at nameScope (node_modules\@tensorflow\tfjs-layers\dist\tf-layers.node.js:931:19)
    at BatchNormalization.Layer.apply (node_modules\@tensorflow\tfjs-layers\dist\tf-layers.node.js:3334:16)
    at execute (node_modules\@tensorflow\tfjs-layers\dist\tf-layers.node.js:6031:45)
shmishra99 commented 1 year ago

Hi @1-bite-only , Apology for the late response. I've reproduced the issue with the latest version of @tensorflow/tfjs@4.4.0 and i am getting the same error. Find the below attached screenshots. Code:

image

Error:

image

CC: @pyu10055 Do you have any update on this issue? Thank you !

johannpayer commented 1 year ago

Any update on this?