*Memos:
- My post explains diag() and diagflat().
- My post explains diagonal().
- My post explains diag_embed().
- My post explains device().
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
) withtorch
isn
(Required) which is the number of rows. - The 2nd argument(
int
) withtorch
ism
(Optional-Default:n
) which is the number of columns. - There is
dtype
argument(torch.dtype) (Optional-Default:None
) withtorch
. *Memos:- If
None
, set_default_dtype() is used. -
dtype
can also accept int(), float() and bool() but not complex() which are python built-in functions. -
dtype=
must be used.
- If
- There is
device
argument(str
,int
or torch.device) (Optional-Default:cpu
) withtorch
. *Memos:-
cpu
,cuda
,ipu
,xpu
,mkldnn
,opengl
,opencl
,ideep
,hip
,ve
,fpga
,ort
,xla
,lazy
,vulkan
,mps
,meta
,hpu
,mtia
orprivateuseone
can be set todevice
. - 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]])
Top comments (0)