tf.eye
Stay organized with collections
Save and categorize content based on your preferences.
Construct an identity matrix, or a batch of matrices.
tf.eye(
num_rows,
num_columns=None,
batch_shape=None,
dtype=tf.dtypes.float32
,
name=None
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
See also tf.ones
, tf.zeros
, tf.fill
, tf.one_hot
.
# Construct one identity matrix.
tf.eye(2)
==> [[1., 0.],
[0., 1.]]
# Construct a batch of 3 identity matrices, each 2 x 2.
# batch_identity[i, :, :] is a 2 x 2 identity matrix, i = 0, 1, 2.
batch_identity = tf.eye(2, batch_shape=[3])
# Construct one 2 x 3 "identity" matrix
tf.eye(2, num_columns=3)
==> [[ 1., 0., 0.],
[ 0., 1., 0.]]
Args |
num_rows
|
Non-negative int32 scalar Tensor giving the number of rows
in each batch matrix.
|
num_columns
|
Optional non-negative int32 scalar Tensor giving the number
of columns in each batch matrix. Defaults to num_rows .
|
batch_shape
|
A list or tuple of Python integers or a 1-D int32 Tensor .
If provided, the returned Tensor will have leading batch dimensions of
this shape.
|
dtype
|
The type of an element in the resulting Tensor
|
name
|
A name for this Op . Defaults to "eye".
|
Returns |
A Tensor of shape batch_shape + [num_rows, num_columns]
|
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.eye\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/linalg_ops.py#L196-L241) |\n\nConstruct an identity matrix, or a batch of matrices.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.linalg.eye`](https://www.tensorflow.org/api_docs/python/tf/eye)\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.eye`](https://www.tensorflow.org/api_docs/python/tf/eye), [`tf.compat.v1.linalg.eye`](https://www.tensorflow.org/api_docs/python/tf/eye)\n\n\u003cbr /\u003e\n\n tf.eye(\n num_rows,\n num_columns=None,\n batch_shape=None,\n dtype=../tf/dtypes#float32,\n name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Advanced automatic differentiation](https://www.tensorflow.org/guide/advanced_autodiff) - [Introduction to graphs and tf.function](https://www.tensorflow.org/guide/intro_to_graphs) | - [TensorFlow Probability Case Study: Covariance Estimation](https://www.tensorflow.org/probability/examples/TensorFlow_Probability_Case_Study_Covariance_Estimation) - [Learnable Distributions Zoo](https://www.tensorflow.org/probability/examples/Learnable_Distributions_Zoo) - [A Tour of TensorFlow Probability](https://www.tensorflow.org/probability/examples/A_Tour_of_TensorFlow_Probability) - [Bayesian Gaussian Mixture Model and Hamiltonian MCMC](https://www.tensorflow.org/probability/examples/Bayesian_Gaussian_Mixture_Model) - [Bayesian Modeling with Joint Distribution](https://www.tensorflow.org/probability/examples/Modeling_with_JointDistribution) |\n\nSee also [`tf.ones`](../tf/ones), [`tf.zeros`](../tf/zeros), [`tf.fill`](../tf/fill), [`tf.one_hot`](../tf/one_hot). \n\n # Construct one identity matrix.\n tf.eye(2)\n ==\u003e [[1., 0.],\n [0., 1.]]\n\n # Construct a batch of 3 identity matrices, each 2 x 2.\n # batch_identity[i, :, :] is a 2 x 2 identity matrix, i = 0, 1, 2.\n batch_identity = tf.eye(2, batch_shape=[3])\n\n # Construct one 2 x 3 \"identity\" matrix\n tf.eye(2, num_columns=3)\n ==\u003e [[ 1., 0., 0.],\n [ 0., 1., 0.]]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------|\n| `num_rows` | Non-negative `int32` scalar `Tensor` giving the number of rows in each batch matrix. |\n| `num_columns` | Optional non-negative `int32` scalar `Tensor` giving the number of columns in each batch matrix. Defaults to `num_rows`. |\n| `batch_shape` | A list or tuple of Python integers or a 1-D `int32` `Tensor`. If provided, the returned `Tensor` will have leading batch dimensions of this shape. |\n| `dtype` | The type of an element in the resulting `Tensor` |\n| `name` | A name for this `Op`. Defaults to \"eye\". |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` of shape `batch_shape + [num_rows, num_columns]` ||\n\n\u003cbr /\u003e"]]