DEV Community

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

Posted on • Edited on

Create and access a tensor in PyTorch

Buy Me a Coffee

*Memos:

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 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-Default:None-Type:dtype): *Memos:
  • There is device argument with torch (Optional-Default:None-Type:str, int or device()): *Memos:
  • There is requires_grad argument with torch (Optional-Default:False-Type:bool): *Memos:
  • 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 create 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

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay