tf.keras.FloatDTypePolicy
Stay organized with collections
Save and categorize content based on your preferences.
A dtype policy for a Keras layer.
Inherits From: DTypePolicy
tf.keras.FloatDTypePolicy(
name
)
A dtype policy determines a layer's computation and variable dtypes. Each
layer has a policy. Policies can be passed to the dtype
argument of layer
constructors, or a global policy can be set with
keras.config.set_dtype_policy
.
Args |
name
|
The policy name, which determines the compute and variable dtypes.
Can be any dtype name, such as "float32" or "float64" ,
which causes both the compute and variable dtypes
will be that dtype.
Can also be the string "mixed_float16" or "mixed_bfloat16" ,
which causes the compute dtype to be float16 or bfloat16
and the variable dtype to be float32 .
|
Typically you only need to interact with dtype policies when using mixed
precision, which is the use of float16 or bfloat16 for computations and
float32 for variables. This is why the term mixed_precision
appears in the
API name. Mixed precision can be enabled by passing "mixed_float16"
or
"mixed_bfloat16"
to keras.mixed_precision.set_dtype_policy()
.
keras.config.set_dtype_policy("mixed_float16")
layer1 = keras.layers.Dense(10)
layer1.dtype_policy # layer1 will automatically use mixed precision
<DTypePolicy "mixed_float16">
# Can optionally override layer to use float32
# instead of mixed precision.
layer2 = keras.layers.Dense(10, dtype="float32")
layer2.dtype_policy
<DTypePolicy "float32">
# Set policy back to initial float32.
keras.config.set_dtype_policy('float32')
In the example above, passing dtype="float32"
to the layer is
equivalent to passing
dtype=keras.config.DTypePolicy("float32")
.
In general, passing a dtype policy name to a layer is equivalent
to passing the corresponding policy, so it is never necessary
to explicitly construct a DTypePolicy
object.
Attributes |
compute_dtype
|
The compute dtype of this policy.
This is the dtype layers will do their computations in. Typically layers
output tensors with the compute dtype as well.
Note that even if the compute dtype is float16 or bfloat16, hardware
devices may not do individual adds, multiplies, and other fundamental
operations in float16 or bfloat16, but instead may do some of them in
float32 for numeric stability. The compute dtype is the dtype of the
inputs and outputs of the ops that the layer executes.
Internally, many ops will do certain internal calculations in
float32 or some other device-internal intermediate format with higher
precision than float16/bfloat16, to increase numeric stability.
|
name
|
Returns the name of this policy.
|
variable_dtype
|
The variable dtype of this policy.
This is the dtype layers will create their variables in, unless a layer
explicitly chooses a different dtype. If this is different than
DTypePolicy.compute_dtype , Layers will cast variables to
the compute dtype to avoid type errors.
Variable regularizers are run in the variable dtype, not the compute
dtype.
|
Methods
View source
convert_input(
x, autocast, dtype
)
from_config
View source
@classmethod
from_config(
config
)
get_config
View source
get_config()
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-06-07 UTC.
[null,null,["Last updated 2024-06-07 UTC."],[],[],null,["# tf.keras.FloatDTypePolicy\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/dtype_policies/dtype_policy.py#L167-L194) |\n\nA dtype policy for a Keras layer.\n\nInherits From: [`DTypePolicy`](../../tf/keras/DTypePolicy)\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.keras.dtype_policies.FloatDTypePolicy`](https://www.tensorflow.org/api_docs/python/tf/keras/FloatDTypePolicy)\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.keras.FloatDTypePolicy`](https://www.tensorflow.org/api_docs/python/tf/keras/FloatDTypePolicy)\n\n\u003cbr /\u003e\n\n tf.keras.FloatDTypePolicy(\n name\n )\n\nA dtype policy determines a layer's computation and variable dtypes. Each\nlayer has a policy. Policies can be passed to the `dtype` argument of layer\nconstructors, or a global policy can be set with\n[`keras.config.set_dtype_policy`](../../tf/keras/config/set_dtype_policy).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `name` | The policy name, which determines the compute and variable dtypes. Can be any dtype name, such as `\"float32\"` or `\"float64\"`, which causes both the compute and variable dtypes will be that dtype. Can also be the string `\"mixed_float16\"` or `\"mixed_bfloat16\"`, which causes the compute dtype to be `float16` or `bfloat16` and the variable dtype to be `float32`. |\n\n\u003cbr /\u003e\n\nTypically you only need to interact with dtype policies when using mixed\nprecision, which is the use of float16 or bfloat16 for computations and\nfloat32 for variables. This is why the term `mixed_precision` appears in the\nAPI name. Mixed precision can be enabled by passing `\"mixed_float16\"` or\n`\"mixed_bfloat16\"` to `keras.mixed_precision.set_dtype_policy()`. \n\n keras.config.set_dtype_policy(\"mixed_float16\")\n layer1 = keras.layers.Dense(10)\n layer1.dtype_policy # layer1 will automatically use mixed precision\n \u003cDTypePolicy \"mixed_float16\"\u003e\n # Can optionally override layer to use float32\n # instead of mixed precision.\n layer2 = keras.layers.Dense(10, dtype=\"float32\")\n layer2.dtype_policy\n \u003cDTypePolicy \"float32\"\u003e\n # Set policy back to initial float32.\n keras.config.set_dtype_policy('float32')\n\nIn the example above, passing `dtype=\"float32\"` to the layer is\nequivalent to passing\n`dtype=keras.config.DTypePolicy(\"float32\")`.\nIn general, passing a dtype policy name to a layer is equivalent\nto passing the corresponding policy, so it is never necessary\nto explicitly construct a `DTypePolicy` object.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ---------- ||\n|------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `compute_dtype` | The compute dtype of this policy. \u003cbr /\u003e This is the dtype layers will do their computations in. Typically layers output tensors with the compute dtype as well. Note that even if the compute dtype is float16 or bfloat16, hardware devices may not do individual adds, multiplies, and other fundamental operations in float16 or bfloat16, but instead may do some of them in float32 for numeric stability. The compute dtype is the dtype of the inputs and outputs of the ops that the layer executes. Internally, many ops will do certain internal calculations in float32 or some other device-internal intermediate format with higher precision than float16/bfloat16, to increase numeric stability. |\n| `name` | Returns the name of this policy. |\n| `variable_dtype` | The variable dtype of this policy. \u003cbr /\u003e This is the dtype layers will create their variables in, unless a layer explicitly chooses a different dtype. If this is different than [`DTypePolicy.compute_dtype`](../../tf/keras/DTypePolicy#compute_dtype), Layers will cast variables to the compute dtype to avoid type errors. Variable regularizers are run in the variable dtype, not the compute dtype. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `convert_input`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/dtype_policies/dtype_policy.py#L137-L157) \n\n convert_input(\n x, autocast, dtype\n )\n\n### `from_config`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/dtype_policies/dtype_policy.py#L162-L164) \n\n @classmethod\n from_config(\n config\n )\n\n### `get_config`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/dtype_policies/dtype_policy.py#L159-L160) \n\n get_config()"]]