DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Updated on

eye() in PyTorch

*Memos:

eye() can create the 2D tensor with 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 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(int) with torch is n(Required) which is the number of rows.
  • The 2nd argument(int) with torch is m(Optional-Default:n) which is the number of columns.
  • There is dtype argument(torch.dtype) (Optional-Default:None) with torch. *Memos:
  • There is device argument(str, int or torch.device) (Optional-Default:cpu) with torch. *Memos:
    • cpu, cuda, ipu, xpu, mkldnn, opengl, opencl, ideep, hip, ve, fpga, ort, xla, lazy, vulkan, mps, meta, hpu, mtia or privateuseone can be set to device.
    • Setting 0 uses GPU(CUDA).
    • device= must be used.
import torch

torch.eye(n=0)
torch.eye(n=0, device='cpu')
torch.eye(n=0, device=torch.device(device='cpu'))
torch.eye(n=0, device=torch.device(type='cpu'))
# 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.float64, device='cuda:0')
torch.eye(n=4, m=6, dtype=torch.float64, device='cuda')
torch.eye(n=4, m=6, dtype=torch.float64, device=0)
torch.eye(n=4, m=6, dtype=float, device=torch.device('cuda:0'))
torch.eye(n=4, m=6, dtype=float, device=torch.device(type='cuda'))
torch.eye(n=4, m=6, dtype=float, device=torch.device(type='cuda', index=0))
# 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.]], device='cuda:0', dtype=torch.float64)

torch.eye(n=4, m=6, dtype=torch.int64)
torch.eye(n=4, m=6, dtype=int)
# 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]], dtype=torch.int32)

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)
torch.eye(n=4, m=6, dtype=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]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)