Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimizer.minimize does not work with maxPooling layers #1189

Closed
generic-github-user opened this issue Feb 2, 2019 · 5 comments
Closed

optimizer.minimize does not work with maxPooling layers #1189

generic-github-user opened this issue Feb 2, 2019 · 5 comments
Assignees
Labels
comp:layers type:support user support questions

Comments

@generic-github-user
Copy link

generic-github-user commented Feb 2, 2019

TensorFlow.js version

Latest / 0.14.2

Browser version

Google Chrome
Version 71.0.3578.98 (Official Build) (64-bit)

Describe the problem

When using optimizer.minimize() with model.predict() to train a tf.model with a loss function, I encounter an issue. This only occurs when I use a maxPooling2D layer in a convolutional neural network with code similar to the code below. It produces this error: Cannot read property 'backend' of undefined. I'm not sure what is causing this or how to resolve it. The error does not occur when using a convolutional layer (tf.layers.conv2d()) without any pooling layers. I have not had this issue with tf.layers.averagePooling2d(), leading me to believe that this could be a possible bug.

Code to reproduce the error

loss = (pred, label) => pred.sub(label).square().mean();
optimizer = tf.train.sgd(0.001);

const input = tf.input({shape: [100, 100, 4]});
const conv = tf.layers.conv2d({
	kernelSize: 5,
	filters: 8,
	strides: 1,
	activation: 'relu',
	kernelInitializer: 'VarianceScaling'
});
const pool = tf.layers.maxPooling2d({
	poolSize: [2, 2],
	strides: [2, 2]
});
const flat = tf.layers.flatten();
const dense = tf.layers.dense({units: 10});
const output = dense.apply(flat.apply(pool.apply(conv.apply(input))));
const model = tf.model({inputs: input, outputs: output});

for (var i = 0; i < 10; i++) {
	optimizer.minimize(() =>
		loss(model.predict([tf.ones([1, 100, 100, 4])]), tf.ones([1, 10]))
	);
}
generic-github-user added a commit to quantuminformation/youtube-space-invaders that referenced this issue Feb 2, 2019
Used averagePooling2d pooling layers instead of maxPooling2d due to this bug: tensorflow/tfjs#1189. Once I have more information about this issue I can update the model with different pooling layer types. The input image is downsampled three times from 100 by 100 to 9 by 9. Adding a convolutional element to the network dramatically increased the performance of the training algorithm after several hundred training iterations.
@rthadur rthadur added comp:layers type:support user support questions labels Feb 2, 2019
@generic-github-user
Copy link
Author

It appears that this is in fact a bug in TensorFlow.js version 0.14 (https://stackoverflow.com/a/54495577/10940584). No error appears when the above code is run in version 0.13.3 and earlier.

@caisq
Copy link
Contributor

caisq commented Feb 2, 2019

@generic-github-user

In 0.14+, there is a change that disables backpropagation support in the Model.predict() method. You can use the Model.apply() method with the {training: true} flag to fix your code.

I.e., change

        optimizer.minimize(() =>
		loss(model.predict([tf.ones([1, 100, 100, 4])]), tf.ones([1, 10]))
	);

to

       optimizer.minimize(() =>
		loss(model.apply([tf.ones([1, 100, 100, 4])], {training: true}), tf.ones([1, 10]))
	);

@caisq caisq closed this as completed Feb 2, 2019
@generic-github-user
Copy link
Author

Thank you! This solved my problem, I appreciate the help.

@quantuminformation
Copy link

Initially, apply confused me as its similar name to the apply method on functions prototype.

@ichko
Copy link

ichko commented Apr 28, 2021

This doesn't seem to work for me. Has the API been changed?
What I am trying to do is do an optim update using the optim.minimize function and a model defined with the layers API.
The optim step is not erroring out, but my loss is not going down.
I could not find a documentation or an example of using apply with this training: true parameter. Neither were I able to find an example of using a model defined with layers being updated with the core API (optimizer.minimize(...))

Can anyone help me out?
(maybe this is not a question for this thread, but this was the only issue I was able to find that contains the setup that I am trying to recreate)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:layers type:support user support questions
Projects
None yet
Development

No branches or pull requests

5 participants