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.
The procedure is extremely simple, just create two vectors and perform operations on them like you are performing them on two regular variables.
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.
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.
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.
where start is the starting value and end is the ending value.
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.