Skip to content

Using mixed precision causes incorrect loss, metric values #45568

Closed

Description

@Crispy13

Tensorflow version : 2.4.0-rc3, compiled from source
GPU : RTX 3080 10GB
CUDA / CUDNN : 11.1 / 8
bazel version : 3.1.0
Windows 10

I decided to mixed precision to speed up the training, but some issues were.
I used this code:

policy = keras.mixed_precision.Policy('mixed_float16')
keras.mixed_precision.set_global_policy(policy)

...

model.compile(optimizer = keras.mixed_precision.LossScaleOptimizer(optimizers.Adam(learning_rate=1e-2))
                        loss = dice_loss
                        metric = [dice_coefficient]
                                                )

Training didn't show any nan loss and metric. But when evaluating the model or each epoch ended, val loss and metric were incorrect(they were fixed over epochs). The colab notebook is here

etc:

  • The values of model.predict(x) and model(x) are different. When the former was nan, the latter seemed normal.

Activity

  1. changed the title [-]Using mixed precision cause incorrect loss, metric values[/-] [+]Using mixed precision causes incorrect loss, metric values[/+] on Dec 10, 2020
  2. google-ml-butler commented on Dec 10, 2020

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

  3. amahendrakar commented on Dec 10, 2020

    @amahendrakar
    Contributor

    On running the code with TF v2.2, the val_loss value is 1.0001.

    Was able to reproduce the issue with TF v2.3, with which the val_loss is nan.

    Whereas with TF v2.4.0-rc3 and TF-nightly, the code runs indefinitely. Please check the linked gist for reference. Thanks!

  4. 25 remaining items

  5. added 15 commits that reference this issue on Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

TF 2.3Issues related to TF 2.3comp:kerasKeras related issuesstaleThis label marks the issue/pr stale - to be closed automatically if no activitystat:awaiting responseStatus - Awaiting response from authortype:bugBug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions