-
-
Notifications
You must be signed in to change notification settings - Fork 426
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
Add support for models that have different behavior at training/test time #38
Comments
Can't you just set the learning phase like this before running your code: import keras
keras.backend.set_learning_phase(0) For me, this seems to work. You can also have a look at the example in the readme which actually uses Keras and sets the learning phase: https://github.com/bethgelab/foolbox#example If I misunderstood the problem or you have an idea how we can improve Foolbox to make it easier to use with Keras, let us know. |
Oh right, forgot you can set the learning phase manually. Thanks! |
Do you think it would be better to set it internally and add a parameter to KerasModel? |
That might be a useful feature, though I can't really imagine why a user would want to set the learning phase to 1 while using the KerasModel since it doesn't have any training methods. I think in native Keras, the predict function is implemented with the learning phase set to 0. Maybe you could add learning phase as a parameter with default value 0? |
Reopening this until I added code to set the learning phase to 0 by default if it has not been set before. This will also simplify the example in the README and apparently is more intuitive for Keras users. |
Looked into this in more detail. Unfortunately, keras does not make it as easy as I thought to set the learning phase from within foolbox because constant learning phase values must be set before creating the keras model. Instead, I would need to feed the learning phase value for every function call and handle several cases separately. I don't think it's worth adding this complexity at the moment, so the recommended way is to just set the learning phase after the keras import as done in the resnet example in the readme. |
I almost filed a similar issue for the TensorFlow model wrapper, but |
@AngusG Can you explain in more detail? |
It was just a comment that since |
In principle we could expose this as an argument during the initialisation of the TF model. @jonasrauber What do you think? |
@AngusG @wielandbrendel I am not sure I understand the use case. The |
From what I have seen, the KerasModel wrapper does not support models with different behaviors at training and test time, such as dropout or batch normalization. This is because there is no opportunity to pass in keras.backend.learning_phase() as a parameter to the prediction functions (defined in line 70, 72 of foolbox/models/keras.py). Trying to predict using a model that depends on the learning phase yields an error such as
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'dropout_1/keras_learning_phase' with dtype bool
Code to reproduce error:
The text was updated successfully, but these errors were encountered: