torch.Tensor — PyTorch 2.3 documentation

Make a Tensor

torch.tensor([1, 2, 3])
torch.rand(row, col, ...)
torch.randint(lower, upper, (row, col, ...))
torch.emtpy(row, col, ...) # Uninitialized data

Other parameters they share:

Get Tensor Properties

tensor.shape / tensor.size() # returns shape as `torch.Size([...])`
tensor.dim # returns scalar - number of dimensions

About dim :

**# More Examples:** 
x = torch.tensor([[1, 2, 3],
                  [4, 5, 6]])
                  
torch.cumsum(x, dim=-1) # Result [1, 3, 6], [4, 9, 15]
torch.cumsum(x, dim=0) # Result [1, 2, 3], [5, 7, 9]
torch.mean(x.float(), dim=-1) # Result [2.0, 5.0]
torch.mean(x.float(), dim=0) # Result [2.5, 3.5, 4.5]                  

Tensor Indexing

Basic Indexing

Single Element: Access a single element by specifying indices in each dimension.

import torch
x = torch.arange(10).reshape(2, 5)
print(x[1, 3])  # Access element at row 1, column 3

Slicing: Use slice notation [start:stop:step] to access a range of elements.

print(x[0, :])  # Access the first row
print(x[:, 1:4])  # Access columns 1 to 3 of all rows