tf.identity
Stay organized with collections
Save and categorize content based on your preferences.
Return a Tensor with the same shape and contents as input.
tf.identity(
input, name=None
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
The return value is not the same Tensor as the original, but contains the same
values. This operation is fast when used on the same device.
For example:
a = tf.constant([0.78])
a_identity = tf.identity(a)
a.numpy()
array([0.78], dtype=float32)
a_identity.numpy()
array([0.78], dtype=float32)
Calling tf.identity
on a variable will make a Tensor that represents the
value of that variable at the time it is called. This is equivalent to calling
<variable>.read_value()
.
a = tf.Variable(5)
a_identity = tf.identity(a)
a.assign_add(1)
<tf.Variable ... shape=() dtype=int32, numpy=6>
a.numpy()
6
a_identity.numpy()
5
This function can also be used to explicitly transfer tensors between devices.
For example, to transfer a tensor in GPU memory back to host memory, one can
use:
with tf.device("/gpu:0"):
x_on_gpu = tf.constant(1)
with tf.device("/cpu:0"):
x_on_cpu = tf.identity(x_on_gpu)
x_on_cpu.device
'/job:localhost/replica:0/task:0/device:CPU:0'
Args |
input
|
A Tensor , a Variable , a CompositeTensor or anything that can be
converted to a tensor using tf.convert_to_tensor .
|
name
|
A name for the operation (optional).
|
Returns |
A Tensor or CompositeTensor. Has the same type and contents as input .
|
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-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.identity\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/array_ops.py#L253-L314) |\n\nReturn a Tensor with the same shape and contents as input.\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.identity`](https://www.tensorflow.org/api_docs/python/tf/identity)\n\n\u003cbr /\u003e\n\n tf.identity(\n input, name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|---------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Migrate LoggingTensorHook and StopAtStepHook to Keras callbacks](https://www.tensorflow.org/guide/migrate/logging_stop_hook) | - [Substantial Undocumented Infection Facilitates the Rapid Dissemination of Novel Coronavirus (SARS-CoV2)](https://www.tensorflow.org/probability/examples/Undocumented_Infection_and_the_Dissemination_of_SARS-CoV2) |\n\nThe return value is not the same Tensor as the original, but contains the same\nvalues. This operation is fast when used on the same device.\n\n#### For example:\n\n a = tf.constant([0.78])\n a_identity = tf.identity(a)\n a.numpy()\n array([0.78], dtype=float32)\n a_identity.numpy()\n array([0.78], dtype=float32)\n\nCalling [`tf.identity`](../tf/identity) on a variable will make a Tensor that represents the\nvalue of that variable at the time it is called. This is equivalent to calling\n`\u003cvariable\u003e.read_value()`. \n\n a = tf.Variable(5)\n a_identity = tf.identity(a)\n a.assign_add(1)\n \u003ctf.Variable ... shape=() dtype=int32, numpy=6\u003e\n a.numpy()\n 6\n a_identity.numpy()\n 5\n\nThis function can also be used to explicitly transfer tensors between devices.\nFor example, to transfer a tensor in GPU memory back to host memory, one can\nuse: \n\n with tf.device(\"/gpu:0\"):\n x_on_gpu = tf.constant(1)\n with tf.device(\"/cpu:0\"):\n x_on_cpu = tf.identity(x_on_gpu)\n x_on_cpu.device\n '/job:localhost/replica:0/task:0/device:CPU:0'\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `input` | A `Tensor`, a `Variable`, a `CompositeTensor` or anything that can be converted to a tensor using [`tf.convert_to_tensor`](../tf/convert_to_tensor). |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` or CompositeTensor. Has the same type and contents as `input`. ||\n\n\u003cbr /\u003e"]]