GeeksforGeeks App
Open App
Browser
Continue

Two-Dimensional Tensors in Pytorch

PyTorch is a python library developed by Facebook to run and train machine learning and deep learning models. In PyTorch everything is based on tensor operations.

Two-dimensional tensors are nothing but matrices or vectors of two-dimension with specific datatype, of n rows and n columns.

Representation: A two-dimensional tensor has the below representation.

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

Creation of Two-Dimensional Tensors:

We can create a tensor by passing a list of data, or randomly generating values with randn and also with arrange function that takes values within certain intervals.

Example :

Python3

 `# importing library``import` `torch` ` ``# list of data in 1d form``y``=``torch.tensor([``2.5``,``5.6``,``8.1``,``4.6``,``3.2``,``6.7``])` `# reshaping it to 2d``x``=``y.view(``2``,``3``)``print``(``'First tensor is: {}'``.``format``(x),``'\nSize of it:{}'``.``format``(x.size()),``      ``'\ntype of tensor:{}\n'``.``format``(x.dtype))` `# random values of size 2X2``x2``=``torch.randn(``2``,``2``)``print``(``'Second tensor is: {}'``.``format``(x2),``'\nSize of it:{}'``.``format``(x2.size()),``      ``'\ntype of tensor:{}\n'``.``format``(x2.dtype))` `# integers within this range``y1``=``torch.arrange(``0``,``8``)``x1``=``y1.view(``4``,``2``)``print``(``'Third tensor is: {}'``.``format``(x1),``'\nSize of it:{}'``.``format``(x1.size()),``      ``'\ntype of tensor:{}'``.``format``(x1.dtype))`

Output:

```First tensor is: tensor([[2.5000, 5.6000, 8.1000],
[4.6000, 3.2000, 6.7000]])
Size of it:torch.Size([2, 3])
type of tensor:torch.float32

Second tensor is: tensor([[1.2532, 1.3558],
[0.5496, 1.7828]])
Size of it:torch.Size([2, 2])
type of tensor:torch.float32

Third tensor is: tensor([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
Size of it:torch.Size([4, 2])
type of tensor:torch.int64```

Multiplication of tensors

Multiplication of tensors can be either element-wise multiplication(multiplying each element by element) or metrics multiplication (multiplying the corresponding column with the corresponding row). In deep learning, we use the concept of metrics multiplication with the required size.

Example :

Python3

 `import` `torch` `a``=``torch.arrange(``0``,``9``)` `# reshaping data``a``=``mat_a.view(``3``,``3``)` `b``=``torch.arrange(``0``,``9``)` `# reshaping data``b``=``mat_b.view(``3``,``3``)` `mat_mul``=``torch.matmul(mat_a,mat_b)``elem_mul``=``torch.mul(mat_a,mat_b)``print``(``'Tensor after elementwise multiplication:{}'``.``format``(elem_mul),``      ``'\n Tensor after matrix multiplication: {}'``.``format``(mat_mul))`

Output:

```Tensor after elementwise multiplication:tensor([[ 0,  1,  4],
[ 9, 16, 25],
[36, 49, 64]])
Tensor after matrix multiplication: tensor([[ 15,  18,  21],
[ 42,  54,  66],
[ 69,  90, 111]])```

Accessing elements:

In the tensor, we can access any column or row values through slicing, and for the particular elements we use indexing. To obtain only the value in the tensor we use .item().

Example :

Python3

 `import` `torch` `# defining the tensor``x4``=``torch.arrange(``4``,``13``)``y4``=``x4.view(``3``,``3``)` `# slicing is performed``print``(``'First column has the values:{}'``.``format``(y4[:,``0``]))``print``(``'Second row has the values:{}'``.``format``(y4[``1``,:]))` `# indexing a  particular element``print``(``'Data at the index 1,2 :{}'``.``format``(y4[``1``][``2``]))`

Output:

```First column has the values:tensor([ 4,  7, 10])
Second row has the values:tensor([7, 8, 9])
Data at the index 1,2 :9```

Three-dimensional tensors:

Three-dimensional tensors are nothing but matrices or vectors of rank 3. A 3d tensor is created by adding another level with brackets to that of the two-dimensional vector. In image processing, we use RGB images that have 3 dimensions of color pixels.

Python3

 `import` `torch` `# tensor with 3 dimension``x``=``torch.tensor([[[``11``,``12``,``13``],[``14``,``15``,``16``],[``17``,``18``,``19``]]])` `# 1d tensor``x1``=``torch.arrange(``10``,``19``)` `# reshaping it to 3d tensor``x1``=``x1.view(``1``,``3``,``3``)``print``(x,``'\n'``,x1)`

Output:

```tensor([[[11, 12, 13],
[14, 15, 16],
[17, 18, 19]]])
tensor([[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]]])
```

My Personal Notes arrow_drop_up