-
Notifications
You must be signed in to change notification settings - Fork 19.5k
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
Update trainer.py #20473
base: master
Are you sure you want to change the base?
Update trainer.py #20473
Conversation
Cleaning each time you compile the model the previous trainer metrics also erased the compilation of other models with are a cominbation of that model. Given problems in archiquectures shuch as GAN. As explain in issue:
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
The issue is: #20474 |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #20473 +/- ##
===========================================
- Coverage 82.07% 59.94% -22.14%
===========================================
Files 515 515
Lines 47416 47415 -1
Branches 7439 7439
===========================================
- Hits 38919 28421 -10498
- Misses 6691 17246 +10555
+ Partials 1806 1748 -58
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Thanks for the PR! @hertschuh, @jeffcarp, do you remember why this line was inserted? I believe it was intended to fix a bug, however removing it does not seem to break any test. If we do remove the line, then we need a test for the use case it was breaking. |
I don't have any context on this. This is the PR that added this line to fix some bug: #20197 |
@fchollet @hertschuh |
@fchollet Previously, that line was necessary to prevent an error caused by the nested import keras
train_images = keras.random.uniform(shape=(32, 784))
train_labels = keras.random.randint(shape=(32, 1), minval=0, maxval=9)
trainer1 = keras.Sequential(
[keras.layers.Input(shape=(784,)), keras.layers.Dense(1, activation="relu")]
)
trainer1.compile(loss="mse", metrics=["mse"]) # Not calling `build` for `trainer1`'s `CompileLoss`
# Create another model.
inputs = keras.Input(shape=(784,))
x = trainer1(inputs)
outputs = keras.layers.Dense(10)(x)
trainer2 = keras.Model(inputs=inputs, outputs=outputs)
trainer2.compile(loss="binary_crossentropy", metrics=["accuracy"])
trainer2.fit(
train_images, keras.utils.to_categorical(train_labels, 10), epochs=2
)
# `fit` might fail because `trainer1`'s `CompileLoss` is not built However, there is logic that skips the metrics for the sub-trainer: keras/keras/src/trainers/trainer.py Lines 264 to 267 in 8deee17
This allows us to keep the metrics in the sub-trainer without encountering the unbuilt issue. We might want to consider adding a similar test as shown above to prevent future breakages. |
Thank you, @james77777778 and @hertschuh ! @TheMGGdev are you able to add a basic, minimal unit test for your use case, so we avoid breaking it in the future? We should also include a test based on @james77777778's snippet above. |
I don't know if this is exactly what you are asking for. Here is a more simplified example of the issue 20474. In Keras 3.6 it gives error but in Keras 3 it works. In the issue I explain better the error and where I think the error is. This is the code with the prints for debugging:
And this is the code without the prints for the test:
|
I think that the solution for both PR is to clean only the layers of the models that don´t have compiled == True, so the one that have not been compiled already. This way it should work for both cases |
Cleaning each time you compile the model the previous trainer metrics also erased the compilation of other models with are a cominbation of that model. Given problems in archiquectures shuch as GAN. As explain in issue: