**Tensors**: **Tensors are the fundamental data structur**e used in deep learning frameworks like PyTorch. A tensor is a multi-dimensional array that can store and manipulate numerical data efficiently. Tensors provide the foundation for performing computations in deep learning models. They can represent various types of data, including input features, model parameters, and intermediate results during computations.

**torch**: In the context of NLP, "torch" refers to the PyTorch library, which is a popular open-source deep learning framework. PyTorch provides a wide range of functionalities for building and training deep learning models, including NLP models. It offers various modules, functions, and classes for tensor operations, neural network architecture, optimization algorithms, loss functions, and more.

**Tensors are multidimensional arrays** that can store and manipulate numerical data efficiently. They are the fundamental data structure used for computations in deep learning models. However, Django can be used in combination with these deep learning frameworks to create web applications that utilize tensors for various tasks, such as data preprocessing, model training, and inference.

Here are some common functionalities and operations associated with tensors in deep learning frameworks, along with examples and their corresponding output:

**Creation**: Tensors can be created from various sources such as Python lists, NumPy arrays, or directly using built-in functions.

```
import torch
Create a tensor from a Python list
tensor1 = torch.tensor([1, 2, 3, 4, 5])
print(tensor1)
```

```
Output: tensor([1, 2, 3, 4, 5])
```

**Create a tensor from a NumPy array**

```
import numpy as np
array = np.array([1, 2, 3, 4, 5])
tensor2 = torch.tensor(array)
print(tensor2)
```

```
Output: tensor([1, 2, 3, 4, 5])
```

**Create a tensor of zeros**

```
tensor3 = torch.zeros(3, 2)
print(tensor3)
```

```
Output: tensor([[0., 0.],
[0., 0.],
[0., 0.]])
```

**Create a tensor of random values**

```
tensor4 = torch.randn(2, 2)
print(tensor4)
```

```
Output: tensor([[-0.5863, 0.2836],
[-0.4969, -0.4520]])
```

**Operations**: Tensors support a wide range of mathematical operations such as addition, subtraction, multiplication, division, and more.

```
import torch
tensor1 = torch.tensor([1, 2, 3])
tensor2 = torch.tensor([4, 5, 6])
```

**Element-wise addition**

```
result1 = tensor1 + tensor2
print(result1)
```

```
Output: tensor([5, 7, 9])
```

**Element-wise multiplication**

```
result2 = tensor1 * tensor2
print(result2)
```

```
Output: tensor([ 4, 10, 18])
```

**Matrix multiplicatio**n

```
tensor3 = torch.tensor([[1, 2], [3, 4]])
tensor4 = torch.tensor([[5, 6], [7, 8]])
result3 = torch.matmul(tensor3, tensor4)
print(result3)
```

```
Output: tensor([[19, 22],
[43, 50]])
```

**Indexing and Slicing**: Tensors allow indexing and slicing operations to access specific elements or subsets of elements.

python

```
import torch
tensor = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
```

**Access a specific element**

```
element = tensor[0, 1]
print(element)
```

```
Output: tensor(2)
```

**Slice a row**

```
row_slice = tensor[1, :]
print(row_slice)
```

```
Output: tensor([4, 5, 6])
```

**Slice a column**

```
column_slice = tensor[:, 2]
print(column_slice)
```

```
Output: tensor([3, 6, 9])
```

**Slice a submatrix**

```
submatrix = tensor[:2, :2]
print(submatrix)
```

```
Output: tensor([[1, 2],
[4, 5]])
```

```
import torch
tensor = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Get the shape of a tensor
shape = tensor.shape
print(shape)
```

```
Output: torch.Size([3, 3])
```

**reshape a tensor**

```
reshaped_tensor = tensor.view(9)
print(reshaped_tensor)
Output: tensor([1, 2, 3, 4, 5, 6, 7, 8, 9])
```

```
Transpose a tensor
transposed_tensor = tensor.transpose(0, 1)
print(transposed_tensor)
```

```
Output: tensor([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
```

Mathematical Functions: Tensors provide various mathematical functions for element-wise operations, reduction operations, and more.

```
import torch
tensor = torch.tensor([1.5, 2.7, 3.1, 4.9])
```

# Element-wise square root

```
sqrt_tensor = torch.sqrt(tensor)
print(sqrt_tensor)
```

```
Output: tensor([1.2247, 1.6432, 1.7607, 2.2136])
```

**Sum of all elements**

```
sum_tensor = torch.sum(tensor)
print(sum_tensor)
```

```
Output: tensor(12.2)
```

**Mean of all elements**

```
mean_tensor = torch.mean(tensor)
print(mean_tensor)
```

```
Output: tensor(3.05)
```

## Top comments (0)