torch.Tensor — PyTorch 2.3 documentation
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:
dtype = None Data Types: https://pytorch.org/docs/stable/tensors.html#data-typesdevice = None - Move to GPU: torch.device(’cuda’)requires_grad = Falsetensor.shape / tensor.size() # returns shape as `torch.Size([...])`
tensor.dim # returns scalar - number of dimensions
dim :dim values: count dimensions from the end of the tensor (-1 is last dimension)dim values: count dimension from the start of the tensor (0 is first dimension)dimtorch.max along dim=-1 will reduce the tensor the shape (2, 3)**# 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]
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