tensor() can create the 0D or more D tensor of zero or more integers, floating-point numbers, complex numbers or boolean values as shown below:
*Memos:
-
tensor()
can be used only from torch but not from a tensor. - The default type is
float32
. - You can also use one or more tuples to create a 1D or more D tensor.
import torch
torch.tensor(3) # 0D tensor
# tensor(3)
torch.tensor([3, 7, 5]) # 1D tensor
# tensor([3, 7, 5])
torch.tensor([[3, 7, 5], [2, 6, 3]]) # 2D tensor
# tensor([[3, 7, 5], [2, 6, 3]])
torch.tensor([[[3, 7, 5], [2, 6, 3]], # 3D tensor
[[8, 0, 1], [4, 9, 6]]])
# tensor([[[3, 7, 5], [2, 6, 3]],
# [[8, 0, 1], [4, 9, 6]]])
torch.tensor([[[3., 7., 5.], [2., 6., 3.]], # 3D tensor
[[8., 0., 1.], [4., 9., 6.]]])
# tensor([[[3., 7., 5.], [2., 6., 3.]],
# [[8., 0., 1.], [4., 9., 6.]]])
torch.tensor([[[3+4j, 7+2j, 5+3j], [2+5j, 6+1j, 3+9j]], # 3D tensor
[[8+9j, 0+3j, 1+8j], [4+3j, 9+4j, 6+2j]]])
# tensor([[[3.+4.j, 7.+2.j, 5.+3.j], [2.+5.j, 6.+1.j, 3.+9.j]],
# [[8.+9.j, 0.+3.j, 1.+8.j], [4.+3.j, 9.+4.j, 6.+2.j]]])
-
Tensor() can also create the 1D or more D tensor whose type is
float32
as shown below:
import torch
torch.Tensor([3., 7., 5.]) # 1D tensor
# tensor([3., 7., 5.])
torch.Tensor([[3., 7., 5.], [2., 6., 3.]]) # 2D tensor
# tensor([[3., 7., 5.], [2., 6., 3.]])
torch.Tensor([[[3., 7., 5.], [2., 6., 3.]], # 3D tensor
[[8., 0., 1.], [4., 9., 6.]]])
# tensor([[[3., 7., 5.], [2., 6., 3.]],
# [[8., 0., 1.], [4., 9., 6.]]])
torch.Tensor([[[3, 7, 5], [2, 6, 3]], # 3D tensor
[[8, 0, 1], [4, 9, 6]]])
# tensor([[[3., 7., 5.], [2., 6., 3.]],
# [[8., 0., 1.], [4., 9., 6.]]])
torch.Tensor([[[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 0D or more D 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
torch.tensor(4) # 0D tensor
# tensor(4)
my_tensor = torch.tensor([3, 7, 5, 2, 6, 3, 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, 3, 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, 2, 6, 3, 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([3, 8, 0, 1, 4, 9, 6])
my_tensor[:]
my_tensor[:,]
my_tensor[0:12]
my_tensor[0:12,]
# tensor([3, 7, 5, 2, 6, 3, 8, 0, 1, 4, 9, 6])
my_tensor = torch.tensor([[3, 7, 5, 2, 6, 3], [8, 0, 1, 4, 9, 6]])
# 2D tensor
my_tensor[1]
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([2, 4])
my_tensor[:]
# tensor([[3, 7, 5, 2, 6, 3], [8, 0, 1, 4, 9, 6]])
my_tensor = torch.tensor([[[3, 7, 5], [2, 6, 3]],
[[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], [2, 6, 3]],
# [[8, 0, 1], [4, 9, 6]]])
Top comments (0)