Create and access a tensor in PyTorch

Super Kai (Kazuya Ito) - Jun 30 - - Dev Community

*Memos:

tensor() can get 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 is data(Required-Type:int, float, complex or bool or tuple of int, float, complex or bool or list of int, float, complex or bool). *The default type is float32.
  • There is dtype argument with torch(Optional-Type:dtype): *Memos:
    • If dtype is not given, dtype is inferred from data or dtype of set_default_dtype() is used for floating-point numbers.
    • dtype= must be used.
    • My post explains dtype argument.
  • There is device argument with torch (Optional-Type:str, int or device()): *Memos:
    • device= must be used.
    • My post explains device argument.
  • There is requires_grad argument with torch (Optional-Type:bool): *Memos:
    • requires_grad= must be used.
    • My post explains requires_grad argument.
  • 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]]])
Enter fullscreen mode Exit fullscreen mode

In addition, Tensor() can get the 1D or more D tensor of zero or more floating-point numbers as shown below:

*Memos:

  • tensor() can be used with torch but not with a tensor.
  • The 1st argument with torch is data(Required-Type:tuple of int, float or bool or list of int, float or bool).
  • 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.]]])
Enter fullscreen mode Exit fullscreen mode

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]]])
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .