Mixed precision is the use of both float16 and float32 when training a model,
and is used to make the model run faster. This function will use mixed
precision to speed up the execution time of tf.functions when run on a GPU.
It does this by changing the dtype of certain operations in the function's
graph from float32 to float16.
This function additionally wraps an Optimizer with a LossScaleOptimizer, which
is required to prevent underflow in the float16 tensors during the backwards
pass. An optimizer must be passed to this function, which will then be wrapped
to use loss scaling.
When this function is used, gradients should only be computed and applied with
the returned optimizer through opt.minimize(), and not with a
tf.GradientTape. This is because the returned optimizer will apply loss
scaling, and tf.GradientTape will not. If you do use a tf.GradientTape,
your model may train to a worse quality.
Currently, mixed precision is only enabled on Volta GPUs and above. TPU
support is coming soon. CPUs are not supported, as CPUs do not run float16
operations faster than float32 operations.
Either an int/float, the string "dynamic", or an instance of a
tf.train.experimental.LossScale. The loss scale to use. It is
recommended to keep this as its default value of "dynamic".
Returns
A version of opt that will use loss scaling to prevent underflow.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2020-10-01 UTC."],[],[],null,["# tf.train.experimental.enable_mixed_precision_graph_rewrite\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/train/experimental/enable_mixed_precision_graph_rewrite) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/training/experimental/mixed_precision.py#L78-L119) |\n\nEnable mixed precision in [`tf.function`](../../../tf/function)s via a graph rewrite. \n\n tf.train.experimental.enable_mixed_precision_graph_rewrite(\n opt, loss_scale='dynamic'\n )\n\nMixed precision is the use of both float16 and float32 when training a model,\nand is used to make the model run faster. This function will use mixed\nprecision to speed up the execution time of [`tf.function`](../../../tf/function)s when run on a GPU.\nIt does this by changing the dtype of certain operations in the function's\ngraph from float32 to float16.\n\nThis function additionally wraps an Optimizer with a LossScaleOptimizer, which\nis required to prevent underflow in the float16 tensors during the backwards\npass. An optimizer must be passed to this function, which will then be wrapped\nto use loss scaling.\n\nWhen this function is used, gradients should only be computed and applied with\nthe returned optimizer through `opt.minimize()`, and not with a\n[`tf.GradientTape`](../../../tf/GradientTape). This is because the returned optimizer will apply loss\nscaling, and [`tf.GradientTape`](../../../tf/GradientTape) will not. If you do use a [`tf.GradientTape`](../../../tf/GradientTape),\nyour model may train to a worse quality.\n\nCurrently, mixed precision is only enabled on Volta GPUs and above. TPU\nsupport is coming soon. CPUs are not supported, as CPUs do not run float16\noperations faster than float32 operations.\n| **Warning:** This rewrite silently affects the entire model and can have unintended consequences. One example: If a NaN occurs during dynamic loss scaling, the data for the batch is silently dropped while the LossScaleOptimizer attempts to find the appropriate scaling value on the next batch.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `opt` | An instance of a [`tf.keras.optimizers.Optimizer`](../../../tf/keras/optimizers/Optimizer). |\n| `loss_scale` | Either an int/float, the string \"dynamic\", or an instance of a [`tf.train.experimental.LossScale`](../../../tf/train/experimental/LossScale). The loss scale to use. It is recommended to keep this as its default value of \"dynamic\". |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A version of `opt` that will use loss scaling to prevent underflow. ||\n\n\u003cbr /\u003e"]]