Casts a tensor to a different dtype and returns it. 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`. E
(x, dtype)
| 1535 | |
| 1536 | @keras_export('keras.backend.cast') |
| 1537 | def cast(x, dtype): |
| 1538 | """Casts a tensor to a different dtype and returns it. |
| 1539 | |
| 1540 | You can cast a Keras variable but it still returns a Keras tensor. |
| 1541 | |
| 1542 | Arguments: |
| 1543 | x: Keras tensor (or variable). |
| 1544 | dtype: String, either (`'float16'`, `'float32'`, or `'float64'`). |
| 1545 | |
| 1546 | Returns: |
| 1547 | Keras tensor with dtype `dtype`. |
| 1548 | |
| 1549 | Examples: |
| 1550 | Cast a float32 variable to a float64 tensor |
| 1551 | |
| 1552 | ```python |
| 1553 | >>> import tensorflow as tf |
| 1554 | >>> from tensorflow.keras import backend as K |
| 1555 | >>> input = K.ones(shape=(1,3)) |
| 1556 | >>> print(input) |
| 1557 | >>> cast_input = K.cast(input, dtype='float64') |
| 1558 | >>> print(cast_input) |
| 1559 | |
| 1560 | <tf.Variable 'Variable:0' shape=(1, 3) dtype=float32, |
| 1561 | numpy=array([[1., 1., 1.]], dtype=float32)> |
| 1562 | tf.Tensor([[1. 1. 1.]], shape=(1, 3), dtype=float64) |
| 1563 | ``` |
| 1564 | """ |
| 1565 | return math_ops.cast(x, dtype) |
| 1566 | |
| 1567 | |
| 1568 | # UPDATES OPS |