eye() in PyTorch
Tags: pytorch, eye, diagonal, tensor
Published: 2024-05-22 19:28:35

*Memos:
- [My post](https://dev.to/hyperkai/diag-and-diagflat-in-pytorch-22n9) explains [diag()](https://pytorch.org/docs/stable/generated/torch.diag.html) and [diagflat()](https://pytorch.org/docs/stable/generated/torch.diagflat.html).
- [My post](https://dev.to/hyperkai/diagonal-in-pytorch-2643) explains [diagonal()](https://pytorch.org/docs/stable/generated/torch.diagonal.html).
- [My post](https://dev.to/hyperkai/diagembed-in-pytorch-a2o) explains [diag_embed()](https://pytorch.org/docs/stable/generated/torch.diag_embed.html).

[eye()](https://pytorch.org/docs/stable/generated/torch.eye.html) can create the 2D tensor of zero or more `1.`, `1`, `1.+0.j` or `True` on the diagonal and zero or more `0.`, `0`, `0.+0.j` or `False` elsewhere as shown below:

*Memos:
- `eye()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) but not with a tensor.
- The returned tensor has zero or more floating-point numbers(Default), integers, complex numbers or boolean values.
- The 1st argument with `torch` is `n`(Required-Type:`int`) which is the number of rows.
- The 2nd argument with `torch` is `m`(Optional-Default:`n`-Type:`int`) which is the number of columns.
- There is `dtype` argument with `torch`(Optional-Type:[dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)):
  *Memos:
  - If `dtype` is not given, `dtype` is `float32` or `dtype` of [set_default_dtype()](https://pytorch.org/docs/stable/generated/torch.set_default_dtype.html) is used for floating-point numbers.
  - `dtype=` must be used.
  - [My post](https://dev.to/hyperkai/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch-13h2) explains `dtype` argument.
- There is `device` argument with `torch`(Optional-Type:`str`, `int` or [device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch.device)):
  *Memos:
  - `device=` must be used.
  - [My post](https://dev.to/hyperkai/set-device-with-device-argument-functions-and-get-it-in-pytorch-1o2p) explains `device` argument.
- There is `requires_grad` argument with `torch`(Optional-Type:`bool`):
  *Memos:
  - `requires_grad=` must be used.
  - [My post](https://dev.to/hyperkai/set-requiresgrad-with-requiresgrad-argument-functions-and-get-it-in-pytorch-39c3) explains `requires_grad` argument.
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
import torch

torch.eye(n=0)
# tensor([], size=(0, 0))

torch.eye(n=1)
# tensor([[1.]])

torch.eye(n=2)
# tensor([[1., 0.],
#         [0., 1.]])

torch.eye(n=3)
# tensor([[1., 0., 0.],
#         [0., 1., 0.],
#         [0., 0., 1.]])

torch.eye(n=4)
# tensor([[1., 0., 0., 0.],
#         [0., 1., 0., 0.],
#         [0., 0., 1., 0.],
#         [0., 0., 0., 1.]])

torch.eye(n=4, m=0)
# tensor([], size=(4, 0))

torch.eye(n=4, m=1)
# tensor([[1.],
#         [0.],
#         [0.],
#         [0.]])

torch.eye(n=4, m=2)
# tensor([[1., 0.],
#         [0., 1.],
#         [0., 0.],
#         [0., 0.]])

torch.eye(n=4, m=3)
# tensor([[1., 0., 0.],
#         [0., 1., 0.],
#         [0., 0., 1.],
#         [0., 0., 0.]])

torch.eye(n=4, m=4)
# tensor([[1., 0., 0., 0.],
#         [0., 1., 0., 0.],
#         [0., 0., 1., 0.],
#         [0., 0., 0., 1.]])

torch.eye(n=4, m=5)
# tensor([[1., 0., 0., 0., 0.],
#         [0., 1., 0., 0., 0.],
#         [0., 0., 1., 0., 0.],
#         [0., 0., 0., 1., 0.]])

torch.eye(n=4, m=6)
# tensor([[1., 0., 0., 0., 0., 0.],
#         [0., 1., 0., 0., 0., 0.],
#         [0., 0., 1., 0., 0., 0.],
#         [0., 0., 0., 1., 0., 0.]])

torch.eye(n=4, m=6, dtype=torch.int64)
# tensor([[1, 0, 0, 0, 0, 0],
#         [0, 1, 0, 0, 0, 0],
#         [0, 0, 1, 0, 0, 0],
#         [0, 0, 0, 1, 0, 0]])

torch.eye(n=4, m=6, dtype=torch.complex64)
# tensor([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
#         [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
#         [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
#         [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]])

torch.eye(n=4, m=6, dtype=torch.bool)
# tensor([[True, False, False, False, False, False],
#         [False, True, False, False, False, False],
#         [False, False, True, False, False, False],
#         [False, False, False, True, False, False]])
```