DEV Community

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

Posted on • Updated on

Create a tensor in PyTorch

*Memos:

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 with torch but not with a tensor.
  • The 1st argument(int, float, complex or bool or tuple of int, float, complex or bool or list of int, float, complex or bool) with torch is data(Required). *The default type is float32.
  • There is dtype argument(torch.dtype) (Optional-Default:None) with torch. *Memos:
  • There is device argument(int, str 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).
  • 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 = torch.tensor(data=-3,
                         dtype=torch.int64,
                         device='cpu')
my_tensor = torch.tensor(data=-3,
                         dtype=torch.int64, 
                         device=torch.device(device='cpu'))
my_tensor = torch.tensor(data=-3,
                         dtype=torch.int64, 
                         device=torch.device(type='cpu'))
my_tensor
# tensor(-3)

my_tensor.dtype
# torch.int64

my_tensor.type()
# torch.LongTensor

my_tensor.device
# device(type='cpu')

my_tensor = torch.tensor(data=-3,
                         dtype=torch.int64,
                         device='cuda:0')
my_tensor = torch.tensor(data=-3,
                         dtype=torch.int64,
                         device='cuda')
my_tensor = torch.tensor(data=-3,
                         dtype=torch.int64,
                         device=0)
my_tensor = torch.tensor(data=-3,
                         dtype=torch.int64, 
                         device=torch.device(device='cuda:0'))
my_tensor = torch.tensor(data=-3,
                         dtype=torch.int64, 
                         device=torch.device(type='cuda'))
my_tensor = torch.tensor(data=-3,
                         dtype=torch.int64, 
                         device=torch.device(type='cuda', index=0))
my_tensor
# tensor(-3, device='cuda:0')

my_tensor.type()
# torch.cuda.LongTensor

my_tensor.device
# device(type='cuda', index=0)

torch.tensor(data=-3, dtype=float32)
# tensor(-3.)

torch.tensor(data=-3, dtype=torch.complex64)
# tensor(-3.+0.j)

torch.tensor(data=-3, dtype=torch.bool)
# tensor(True)

torch.tensor(data=-3, dtype=int)
# tensor(-3)

torch.tensor(data=-3, dtype=float)
# tensor(-3., dtype=torch.float64)

torch.tensor(data=-3, dtype=bool)
# tensor(True)

""" 1D tensor """

torch.tensor(data=[-3.635251, 7.270649, -5.164872])
# tensor([-3.6353, 7.2706, -5.1649])

torch.tensor(data=[-3.635251, 7.270649, -5.164872], 
             dtype=torch.int64)
# tensor([-3, 7, -5])

torch.tensor(data=[-3.635251, 7.270649, -5.164872], 
             dtype=torch.complex64)
# tensor([-3.6353+0.j,  7.2706+0.j, -5.1649+0.j])

torch.tensor(data=[-3.635251, 7.270649, -5.164872], 
             dtype=torch.bool)
# tensor([True, True, True])

""" 2D tensor """

torch.tensor(data=[[-3.635251+4.634852j,
                    7.270649+2.586449j,
                    -5.164872-3.450984j],
                   [8.75+9.66j,
                    0.49-3.87j,
                    -1.64-8.45j]])
# tensor([[-3.6353+4.6349j, 7.2706+2.5864j, -5.1649-3.4510j],
#         [8.7500+9.6600j, 0.4900-3.8700j, -1.6400-8.4500j]])

torch.tensor(data=[[-3.635251+4.634852j,
                    7.270649+2.586449j,
                    -5.164872-3.450984j],
                   [8.75+9.66j,
                    0.49-3.87j,
                    -1.64-8.45j]],
             dtype=torch.bool)
# tensor([[True, True, True],
#         [True, True, True]])

""" 3D tensor """

torch.tensor(data=[[[True, False, True], [True, False, True]], 
                   [[False, True, False], [False, True, False]]])
# tensor([[[True, False, True], [True, False, True]],
#         [[False, True, False], [False, True, False]]])

torch.tensor(data=[[[True, False, True], [True, False, True]], 
                   [[False, True, False], [False, True, False]]], 
             dtype=torch.int)
# tensor([[[1, 0, 1], [1, 0, 1]],
#         [[0, 1, 0], [0, 1, 0]]], dtype=torch.int32)

torch.tensor(data=[[[True, False, True], [True, False, True]], 
                   [[False, True, False], [False, True, False]]], 
             dtype=torch.float32)
# tensor([[[1., 0., 1.], [1., 0., 1.]],
#         [[0., 1., 0.], [0., 1., 0.]]])

torch.tensor(data=[[[True, False, True], [True, False, True]], 
                   [[False, True, False], [False, True, False]]], 
             dtype=torch.complex64)
# tensor([[[1.+0.j, 0.+0.j, 1.+0.j], [1.+0.j, 0.+0.j, 1.+0.j]],
#         [[0.+0.j, 1.+0.j, 0.+0.j], [0.+0.j, 1.+0.j, 0.+0.j]]])
Enter fullscreen mode Exit fullscreen mode
  • Tensor() can only create the 1D or more D tensor whose type is float32 from zero or more integers, floating-point numbers or boolean values as shown below:

*Memos:

  • tensor() can be used with torch but not with a tensor.
  • The 1st argument(list of int, float or bool or tuple of int, float or bool) with torch is data(Required).
  • The one or more values of the tensor of float can already be rounded to 4 decimal places when creating the tensor, then be shown with 4 decimal.
import torch

torch.Tensor(data=[-3, 7, -5]) # 1D tensor
# tensor([-3., 7., -5.])

torch.Tensor(data=[[-3, 7, -5], [2, 6, 3]]) # 2D tensor
# tensor([[3., 7., 5.], [2., 6., 3.]])

torch.Tensor(data=[[[-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(data=[[[-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(data=[[[-3.635251, 7.270649, -5.164872], # 3D tensor
                    [2.750269, 6.811633, 3.042955]],
                   [[8.75, 0.49, -1.64],
                    [4.24, 9.99, -6.07]]])
# tensor([[[-3.6353, 7.2706, -5.1649],
#          [2.7503, 6.8116, 3.0430]],
#         [[8.7500, 0.4900, -1.6400],
#          [4.2400, 9.9900, -6.0700]]])

torch.Tensor(data=[[[True, False, True], # 3D tensor
                    [True, False, True]],
                   [[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

Top comments (0)