Pytorch Tensor
A tensor is a multidimensional array, which can be a scalar, vector, matrix, or higher-dimensional data structure.
In PyTorch, Tensor is the core representation of data, similar to NumPy's multidimensional arrays, but with more powerful features such as GPU acceleration support and automatic gradient computation.
Tensors support multiple data types (integer, float, boolean, etc.).
Tensors can be stored on CPU or GPU, and GPU tensors can significantly accelerate computation.
The following image shows how tensors of different dimensions are represented in PyTorch:
!(#)
**Description:**
* **1D Tensor / Vector:** The most basic form of tensor, which can be considered as an array. The example in the figure is a vector containing 10 elements.
* **2D Tensor / Matrix:** A two-dimensional array, typically used to represent matrices. The example in the figure is a 4x5 matrix containing 20 elements.
* **3D Tensor / Cube:** A three-dimensional array, which can be considered as a cube formed by stacking multiple matrices. The example in the figure shows a 3x4x5 cube, where each 5x5 matrix represents a "layer" of the cube.
* **4D Tensor / Vector of Cubes:** A four-dimensional array, which can be considered as a vector composed of multiple cubes. The example in the figure does not have specific values, but can be understood as a collection containing multiple 3D tensors.
* **5D Tensor / Matrix of Cubes:** A five-dimensional array, which can be considered as a matrix composed of multiple 4D tensors. The example in the figure also does not have specific values, but can be understood as a collection containing multiple 4D tensors.
* * *
## Creating Tensors
The ways to create tensors are:
| **Method** | **Description** | **Example Code** |
| --- | --- | --- |
| `torch.tensor(data)` | Create a tensor from a Python list or NumPy array. | `x = torch.tensor([[1, 2], [3, 4]])` |
| `torch.zeros(size)` | Create a tensor filled with zeros. | `x = torch.zeros((2, 3))` |
| `torch.ones(size)` | Create a tensor filled with ones. | `x = torch.ones((2, 3))` |
| `torch.empty(size)` | Create an uninitialized tensor. | `x = torch.empty((2, 3))` |
| `torch.rand(size)` | Create a tensor with random values from uniform distribution, values in `[0, 1)`. | `x = torch.rand((2, 3))` |
| `torch.randn(size)` | Create a tensor with random values from normal distribution, mean 0, standard deviation 1. | `x = torch.randn((2, 3))` |
| `torch.arange(start, end, step)` | Create a 1D sequence tensor, similar to Python's `range`. | `x = torch.arange(0, 10, 2)` |
| `torch.linspace(start, end, steps)` | Create a tensor with evenly spaced values in a specified range. | `x = torch.linspace(0, 1, 5)` |
| `torch.eye(size)` | Create an identity matrix (1 on diagonal, 0 elsewhere). | `x = torch.eye(3)` |
| `torch.from_numpy(ndarray)` | Convert a NumPy array to a tensor. | `x = torch.from_numpy(np.array([1, 2, 3]))` |
Using the `torch.tensor()` function, you can convert a list or array to a tensor:
## Example
import torch
tensor = torch.tensor([1,2,3])
print(tensor)
Output:
tensor([1, 2, 3])
If you have a NumPy array, you can use `torch.from_numpy()` to convert it to a tensor:
## Example
import numpy as np
np_array = np.array([1,2,3])
tensor = torch.from_numpy(np_array)
print(tensor)
Output:
tensor([1, 2, 3])
Creating a 2D tensor (matrix):
## Example
import torch
tensor_2d = torch.tensor([
[-9,4,2,5,7],
[3,0,12,8,6],
[1,23, -6,45,2],
[22,3, -1,72,6]
])
print("2D Tensor (Matrix):n", tensor_2d)
print("Shape:", tensor_2d.shape)# shape
Output:
2D Tensor (Matrix): tensor([[-9, 4, 2, 5, 7], [ 3, 0, 12, 8, 6], [ 1, 23, -6, 45, 2], [22, 3, -1, 72, 6]])Shape: torch.Size([4, 5])
Creating other dimensions:
# Create 3D tensor (cube) tensor_3d = torch.stack([tensor_2d, tensor_2d + 10, tensor_2d - 5]) # stack 3 2D tensorsprint("3D Tensor (Cube):n", tensor_3d)print("Shape:", tensor_3d.shape) # shape# Create 4D tensor (vector of cubes) tensor_4d = torch.stack([tensor_3d, tensor_3d + 100]) # stack 2 3D tensorsprint("4D Tensor (Vector of Cubes):n", tensor_4d)print("Shape:", tensor_4d.shape) # shape# Create 5D tensor (matrix of cubes) tensor_5d = torch.stack([tensor_4d, tensor_4d + 1000]) # stack 2 4D tensorsprint("5D Tensor (Matrix of Cubes):n", tensor_5d)print("Shape:", tensor_5d.shape) # shape
* * *
## Tensor Attributes
The tensor attributes are as follows:
| **Attribute** | **Description** | **Example** |
| --- | --- | --- |
| `.shape` | Get the shape of the tensor | `tensor.shape` |
| `.size()` | Get the shape of the tensor | `tensor.size()` |
| `.dtype` | Get the data type of the tensor | `tensor.dtype` |
| `.device` | Check where the tensor is located (CPU/GPU) | `tensor.device` |
| `.dim()` | Get the number of dimensions of the tensor | `tensor.dim()` |
| `.requires_grad` | Whether gradient computation is enabled | `tensor.requires_grad` |
| `.numel()` | Get the total number of elements in the tensor | `tensor.numel()` |
| `.is_cuda` | Check if the tensor is on GPU | `tensor.is_cuda` |
| `.T` | Get the transpose of the tensor (for 2D tensors) | `tensor.T` |
| `.item()` | Get the value of a single-element tensor | `tensor.item()` |
| `.is_contiguous()` | Check if the tensor is stored contiguously | `tensor.is_contiguous()` |
## Example
import torch
# Create a 2D tensor
tensor = torch.tensor([[1,2,3],[4,5,6]], dtype=torch.float32)
# Tensor attributes
print("Tensor:n", tensor)
print("Shape:", tensor.shape)# get shape
print("Size:", tensor.size())# get shape (alternative method)
print("Data Type:", tensor.dtype)# data type
print("Device:", tensor.device)# device
print("Dimensions:", tensor.dim())# number of dimensions
print("Total Elements:", tensor.numel())# total elements
print("Requires Grad:", tensor.requires_grad)# whether gradient is enabled
print("Is CUDA:", tensor.is_cuda)# whether on GPU
print("Is Contiguous:", tensor.is_contiguous())# whether contiguous
# Get single element value
single_value = torch.tensor(42)
print("Single Element Value:", single_value.item())
# Transpose tensor
YouTip