*Memos:
- My post explains type conversion with type(), to() and a tensor.
- My post explains type promotion, result_type(), promote_types() and can_cast().
- My post explains device conversion with to(), from_numpy() and numpy().
- My post explains how to check PyTorch version, CPU and GPU(CUDA).
- My post explains dim(), size() , item() and tolist().
- My post explains is_tensor(), numel() and device().
- My post explains set_default_dtype(), set_default_device() and set_printoptions().
- My post explains manual_seed(), initial_seed() and seed().
tensor() can create the 0D or more D tensor of zero or more elements as shown below:
*Memos:
-
tensor()
can be used with torch but not with a tensor. - The 1st argument with
torch
isdata
(Required-Type:int
,float
,complex
orbool
ortuple
ofint
,float
,complex
orbool
orlist
ofint
,float
,complex
orbool
). *The default type isfloat32
. - There is
dtype
argument withtorch
(Optional-Default:None
-Type:dtype): *Memos:- If it's
None
, it's inferred fromdata
, then for floating-point numbers, get_default_dtype() is used. *My post explainsget_default_dtype()
and set_default_dtype(). -
dtype=
must be used. -
My post explains
dtype
argument.
- If it's
- There is
device
argument withtorch
(Optional-Default:None
-Type:str
,int
or device()): *Memos:- If it's
None
, get_default_device() is used. *My post explainsget_default_device()
and set_default_device(). -
device=
must be used. -
My post explains
device
argument.
- If it's
- There is
requires_grad
argument withtorch
(Optional-Default:False
-Type:bool
): *Memos:-
requires_grad=
must be used. - My post explains requires_grad.
-
- The one or more floating-point numbers or complex numbers of a tensor are rounded to 4 decimal places by default.
import torch
""" 0D tensor """
my_tensor = torch.tensor(data=-3)
my_tensor
# tensor(-3)
""" 1D tensor """
torch.tensor(data=[3, 7, -5])
# tensor([3, 7, -5])
torch.tensor(data=[3.635251, 7.270649, -5.164872])
# tensor([3.6353, 7.2706, -5.1649])
torch.tensor(data=[3.635251+4.634852, 7.27+2.586449j, -5.164872-3.45])
# tensor([0.9996+0.0000j, 7.2700+2.5864j, -8.6149+0.0000j])
torch.tensor(data=[True, False, True])
# tensor([True, False, True])
""" 2D tensor """
torch.tensor(data=[[3, 7, -5], [-9, 6, 2]])
# tensor([[3, 7, -5], [-9, 6, 2]])
""" 3D tensor """
torch.tensor(data=[[[3, 7, -5], [-9, 6, 2]],
[[8, 0, -1], [4, 9, -6]]])
# tensor([[[3, 7, -5], [-9, 6, 2]],
# [[8, 0, -1], [4, 9, -6]]])
In addition, Tensor() can create the 1D or more D tensor of zero or more floating-point numbers as shown below:
*Memos:
-
Tensor()
can be used withtorch
but not with a tensor. - The 1st argument with
torch
isdata
(Required-Type:tuple
ofint
,float
orbool
orlist
ofint
,float
orbool
). - The one or more floating-point numbers or complex numbers of a tensor are rounded to 4 decimal places by default.
import torch
torch.Tensor(data=[3., 7., -5.]) # 1D tensor
# tensor([3., 7., -5.])
torch.Tensor(data=[[3., 7., -5.], [-9., 6., 2.]]) # 2D tensor
# tensor([[-3., 7., -5.], [-9., 6., 2.]])
torch.Tensor(data=[[[-3., 7., -5.], [-9., 6., 2.]], # 3D tensor
[[8., 0., -1.], [4., 9., -6.]]])
# tensor([[[-3., 7., -5.], [-9., 6., 2.]],
# [[8., 0., 1.], [4., 9., -6.]]])
torch.Tensor(data=[[[-3., 7., -5.], [-9., 6., 2.]], # 3D tensor
[[8., 0., -1], [4., 9., -6.]]])
# tensor([[[-3., 7., -5.], [-9., 6., 2.]],
# [[8., 0., -1.], [4., 9., -6.]]])
torch.Tensor(data=[[[-3, 7, -5], [-9, 6, 2]], # 3D tensor
[[8, 0, -1], [4, 9, -6]]])
# tensor([[[-3., 7., -5.], [-9., 6., 2.]],
# [[8., 0., -1.], [4., 9., -6.]]])
torch.Tensor(data=[[[True, False, True], [True, False, True]], # 3D tensor
[[False, True, False], [False, True, False]]])
# tensor([[[1., 0., 1.], [1., 0., 1.]],
# [[0., 1., 0.], [0., 1., 0.]]])
You can access a 0D or more D tensor with these ways as shown below. *I give much more ways to access a 1D tensor than a 0D, 2D and 3D tensor:
import torch
my_tensor = torch.tensor(3) # 0D tensor
my_tensor
# tensor(3)
my_tensor = torch.tensor([3]) # 1D tensor
my_tensor
# tensor([3])
my_tensor = torch.tensor([3, 7, -5, -9, 6, 2, 8, 0, -1, 4, 9, -6])
# 1D tensor
my_tensor[4]
my_tensor[4,]
my_tensor[-10]
my_tensor[-10,]
my_tensor[4:5]
my_tensor[4:5,]
my_tensor[-8:5]
my_tensor[-8:5,]
my_tensor[4:-7]
my_tensor[4:-7,]
my_tensor[-8:-7]
my_tensor[-8:-7,]
# tensor(6)
my_tensor[4:8]
my_tensor[4:8,]
my_tensor[-8:8]
my_tensor[-8:8,]
my_tensor[4:-4]
my_tensor[4:-4,]
my_tensor[-8:-4]
my_tensor[-8:-4,]
# tensor([6, 2, 8, 0])
my_tensor[:7]
my_tensor[:7,]
my_tensor[:-5]
my_tensor[:-5,]
my_tensor[0:7]
my_tensor[0:7,]
my_tensor[-12:7]
my_tensor[-12:7,]
my_tensor[0:-5]
my_tensor[0:-5,]
my_tensor[-12:-5]
my_tensor[-12:-5,]
# tensor([3, 7, -5, -9, 6, 2, 8])
my_tensor[5:]
my_tensor[5:,]
my_tensor[-7:]
my_tensor[-7:,]
my_tensor[5:12]
my_tensor[5:12,]
my_tensor[-7:12]
my_tensor[-7:12,]
# tensor([2, 8, 0, -1, 4, 9, -6])
my_tensor[:]
my_tensor[:,]
my_tensor[0:12]
my_tensor[0:12,]
# tensor([3, 7, -5, -9, 6, 2, 8, 0, -1, 4, 9, -6])
my_tensor = torch.tensor([[3, 7, -5, -9, 6, 2],
[8, 0, -1, 4, 9, -6]])
my_tensor[1] # 2D tensor
my_tensor[:][1]
my_tensor[1, :]
# tensor([8, 0, -1, 4, 9, -6])
my_tensor[1][3]
my_tensor[1, 3]
# tensor(4)
my_tensor[1][:4]
my_tensor[1, :4]
# tensor([8, 0, -1, 4])
my_tensor[1][2:]
my_tensor[1, 2:]
# tensor([-1, 4, 9, -6])
my_tensor[:, 3]
# tensor([-9, 4])
my_tensor[:]
# tensor([[3, 7, -5, -9, 6, 2],
# [8, 0, -1, 4, 9, -6]])
my_tensor = torch.tensor([[[-3, 7, -5], [-9, 6, 2]],
[[8, 0, -1], [4, 9, -6]]])
my_tensor[1] # 3D tensor
my_tensor[:][1]
my_tensor[1, :]
my_tensor[1][:2]
my_tensor[1, :2]
my_tensor[1][0:]
my_tensor[1, 0:]
# tensor([[8, 0, -1], [4, 9, -6]])
my_tensor[1][0]
# tensor([8, 0, -1])
my_tensor[1][0][2]
my_tensor[1, 0, 2]
# tensor(-1)
my_tensor[1][0][:2]
my_tensor[1, 0, :2]
# tensor([8, 0])
my_tensor[1][0][1:]
my_tensor[1, 0, 1:]
# tensor([0, -1])
my_tensor[:, :, 1]
# tensor([[7, 6], [0, 9]])
my_tensor[:]
# tensor([[[-3, 7, -5], [-9, 6, 2]],
# [[8, 0, -1], [4, 9, -6]]])
Top comments (0)