
- Torch - Home
- Torch - Introduction
- Torch - Installation
- Torch - Torch Tensors
- Torch - Optimizers & Loss Functions
- Torch - Artificial Neural Networks
- Torch - Convolutional Neural Networks
- Torch - Recurrent Neural Networks
- Torch Useful Resources
- DeepSpeed - Quick Guide
- Torch - Useful Resources
- Discuss Torch
Torch - Torch Tensors
Tensors are multidimensional arrays that can store data of various types. They are similar to NumPy arrays with additional capabilities, such as GPUs for faster computation. Tensors can be used to represent data in various ways, i.e., from vectors to scalars or to matrices. Tensors are very flexible; this flexibility makes tensors more powerful for a wide range of applications in machine learning and deep learning.
For example, in image processing, a 3D tensor can represent an image with width, height, and the color channels. In natural language processing, a 2D tensor can represent each row redirected to a sentence, and each column redirects to a word embedding.
Tensors can be easily converted to and from other data structures, such as Python lists and NumPy arrays, providing integration with the existing code bases. This is combined with the hardware acceleration which makes tensors a basic concepts in the development and deployment of machine learning models.
Tensor attributes
Tensors have different attributes that specifies information about properties −
size(): The dimensions of tensor.
nDimension(): The number of dimensions of the tensor.
type(): The data type of the tensor.
We can access these methods attributes using the following methods −
print(tensor:size()) print(tensor:nDimension()) print(tensor:type())
Operations in Tensors
Following are the fundamental operations that can be performed on Torch Tensors−
Arithmetic Operations
Matrix Operations
Reduction Operations
Element-wise Operations
Torch Tensor Functions Overview
These operations in Torch Tensors specify element calculations for the difficult deep learning tasks.
Function | Description |
---|---|
torch.add() | Adds two tensors |
torch.sub() | Subtracts one tensor from another |
torch.mul() | Multiplies two numbers |
torch.div() | Divides one tensor by another. |
torch.mm() | Matrix multiplication is performed. |
torch.matmul() | Multiplication and supporting broadcast is performed in the matrix. |
torch.t() | A 2D Tensor is transposed. |
torch.sum() | Sum all the elements in the tensor. |
torch.mean() | Computes the mean of all elements. |
torch.max() | Maximum value in the tensor will be returned. |
torch.min() | Minimum value in the tensor will be returned. |
torch.pow() | The power of each element will be computed. |
torch.sqrt() | The square root of each element will be computed. |
torch.abs() | Returns the absolute value of each element. |
These operations are difficult for various deep learning tasks, as they allow for efficient and parallelized computations. Each operations in the Torch Tensor makes versatile and powerful for deep learning and numerical computations.
Creating Tensors
There are several ways to create tensors in Torch using these methods −
From Lua Tables
We can create tensors directly from the Lua table using torch.Tensor(). This is used for small datasets or we can specify the data manually.
local data = {{2, 3}, {1, 2}} local tensor = torch.Tensor(data)
Using Factory Methods
Torch provides different factory methods to create tensors with specific values or shapes.Some of the common methods are −
torch.Tensor(): Creates an uninitialized tensor.
torch.zeros(): Creates a tensor filled with zero.
torch.ones() Creates a tensor filled with ones.
torch.rand(): creates a tensor with random values.
Following lines create different 4*4 tensors in Torch −
local empty_tensor = torch.Tensor(4, 4) local zeros_tensors = torch.zeros(4, 4) local ones_tensor = torch.ones(4, 4) local ransom_tensor = torch.rand(4, 4)