![]() |
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
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
|
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]])
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 :
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 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.
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]]])
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 9 |