tf.keras.backend.cast
Stay organized with collections
Save and categorize content based on your preferences.
Casts a tensor to a different dtype and returns it.
tf.keras.backend.cast(
x, dtype
)
You can cast a Keras variable but it still returns a Keras tensor.
Arguments |
x
|
Keras tensor (or variable).
|
dtype
|
String, either ('float16' , 'float32' , or 'float64' ).
|
Returns |
Keras tensor with dtype dtype .
|
Examples:
Cast a float32 variable to a float64 tensor
input = tf.keras.backend.ones(shape=(1,3))
print(input)
<tf.Variable 'Variable:0' shape=(1, 3) dtype=float32,
numpy=array([[1., 1., 1.]], dtype=float32)>
cast_input = tf.keras.backend.cast(input, dtype='float64')
print(cast_input)
tf.Tensor([[1. 1. 1.]], shape=(1, 3), dtype=float64)
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.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.keras.backend.cast\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/keras/backend/cast) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.2.0/tensorflow/python/keras/backend.py#L1555-L1580) |\n\nCasts a tensor to a different dtype and returns it.\n\n#### View aliases\n\n\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.backend.cast`](/api_docs/python/tf/keras/backend/cast)\n\n\u003cbr /\u003e\n\n tf.keras.backend.cast(\n x, dtype\n )\n\nYou can cast a Keras variable but it still returns a Keras tensor.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Arguments --------- ||\n|---------|------------------------------------------------------------|\n| `x` | Keras tensor (or variable). |\n| `dtype` | String, either (`'float16'`, `'float32'`, or `'float64'`). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Keras tensor with dtype `dtype`. ||\n\n\u003cbr /\u003e\n\n#### Examples:\n\nCast a float32 variable to a float64 tensor \n\n input = tf.keras.backend.ones(shape=(1,3))\n print(input)\n \u003ctf.Variable 'Variable:0' shape=(1, 3) dtype=float32,\n numpy=array([[1., 1., 1.]], dtype=float32)\u003e\n cast_input = tf.keras.backend.cast(input, dtype='float64')\n print(cast_input)\n tf.Tensor([[1. 1. 1.]], shape=(1, 3), dtype=float64)"]]