Vector Operations in Pytorch

22 Apr, 2022
In this article, we are going to discuss vector operations in PyTorch. Vectors are a one-dimensional tensor, which is used to manipulate the data. Vector operations are of different types such as mathematical operation, dot product, and linspace.
PyTorch is an optimized tensor library majorly used for Deep Learning applications using GPUs and CPUs. It is one of the widely used Machine learning libraries, others being TensorFlow and Keras.
We can create a vector by using the torch.tensor() function
Syntax:
torch.tensor([value1,value2,.value n])
where values are the input values that take input as a list
Example:
Python3
# importing pytorch moduleimport torch # create an vectorA = torch.tensor([7058, 7059, 7060, 7061, 7062]) # displayprint(A)
Output:
tensor([7058, 7059, 7060, 7061, 7062])
Now let us discuss each vector operation supported under tensor. 
The procedure is extremely simple, just create two vectors and perform operations on them like you are performing them on two regular variables.
Example:
Python3
# importing pytorch moduleimport torch # create an vector AA = torch.tensor([58, 59, 60, 61, 62]) # create an vector BB = torch.tensor([100, 120, 140, 160, 180]) # add two vectorsprint("Addition of two vectors:", A+B) # subtract two vectorsprint("subtraction of two vectors:", A-B) # multiply two vectorsprint("multiplication of two vectors:", A*B) # multiply two vectorsprint("multiplication of two vectors:", A*B) # divide two vectorsprint("division of two vectors:", A/B) # floor divide two vectorsprint("floor division of two vectors:", A//B) # modulus of two vectorsprint("modulus operation of two vectors:", A % B) # power of two vectorsprint("power operation of two vectors:", A**B)
Output:
Addition of two vectors: tensor([158, 179, 200, 221, 242])
subtraction of two vectors: tensor([ -42,  -61,  -80,  -99, -118])
multiplication of two vectors: tensor([ 5800,  7080,  8400,  9760, 11160])
multiplication of two vectors: tensor([ 5800,  7080,  8400,  9760, 11160])
division of two vectors: tensor([0.5800, 0.4917, 0.4286, 0.3812, 0.3444])
floor division of two vectors: tensor([0, 0, 0, 0, 0])
modulus operation of two vectors: tensor([58, 59, 60, 61, 62])
power operation of two vectors: tensor([  0, -4166911448072485343,  0,8747520307384418433,  0])
It is similar to arithmetic operations except that the other vector part is replaced by a constant.
Example:
Python3
# importing pytorch moduleimport torch # create an vector AA = torch.tensor([58, 59, 60, 61, 62]) # divide vector by 2print(A/2) # multiply vector by 2print(A*2) # subtract  vector by 2print(A-2)
Output:
tensor([29.0000, 29.5000, 30.0000, 30.5000, 31.0000])
tensor([116, 118, 120, 122, 124])
tensor([56, 57, 58, 59, 60])
dot() is used to get the dot product. The vectors in consideration just need to be passed to it.
Syntax:
torch.dot(vector1,vector2)  
Example:
Python3
# importing pytorch moduleimport torch # create an vector AA = torch.tensor([58, 59, 60, 61, 62]) # create an vector BB = torch.tensor([8, 9, 6, 1, 2]) # dot product of the two vectorsprint(torch.dot(A, B))
Output:
tensor(1540)
linspace is used to arrange data linearly in the given space. It is available in the torch package and using linspace() function with values for start and end are enough.
Syntax:
torch.linspace(start,end)  
where start is the starting value and end is the ending value.
Example
Python3
# importing pytorch moduleimport torch # arrange the elements from 2 to 10print(torch.linspace(2, 10))
Output:
The linspace function is used to plot a function on two-dimensional coordinate systems. For the x-axis, we create a land space from 0 to 10 in an interval of 2.5, and Y will be the function of each x value. 
Example 1: sin function
Python3
#import pytorchimport torch #import numpyimport numpy as np #import matplotlibimport matplotlib.pyplot as plt # create lin space from 1 to 12x = torch.linspace(1, 12) # sin functiony = torch.sin(x) # plotplt.plot(x.numpy(), y.numpy()) # displayplt.show()
Output:
Example 2: cos function
Python3
#import pytorchimport torch #import numpyimport numpy as np #import matplotlibimport matplotlib.pyplot as plt # create lin space from 1 to 12x = torch.linspace(1, 12) # cos functiony = torch.cos(x) # plotplt.plot(x.numpy(), y.numpy()) # displayplt.show()
Output:
Example 3: tan() function
Python3
#import pytorchimport torch #import numpyimport numpy as np #import matplotlibimport matplotlib.pyplot as plt # create lin space from 1 to 12x = torch.linspace(1, 12) # tan functiony = torch.tan(x) # plotplt.plot(x.numpy(), y.numpy()) # displayplt.show()
Output:
surindertarika1234
sumitgumber28
simranarora5sos
Picked
Python-PyTorch
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.