Javatpoint Logo
Javatpoint Logo

Vector Operations

We know Tensor have different types of dimensions such as zero dimension, one dimension, and multi-dimensional. Vectors are a one-dimensional tensor, and to manipulate them several operations available. Vector operations are of different types such as mathematical operation, dot product, and linspace. Vectors play a vital role in deep learning.

In deep learning neural network, we generate random point with the help of vectors or one-dimensional tensor. There are the following operations which are performed on the vector.

Mathematical Operations

We can add, subtract, multiply, and divide the tensor from another tensor. Following is the table of all mathematical operations which are performed on vectors with the expected output.

S. No. Operation Tensor A Tensor B Number Syntax Output
1 + [1, 2, 3] [4, 5, 6] 2 A+B [5, 7, 9]
2 [1, 2, 3] [4, 5, 6] 2 A+2 [3, 4, 5]
3 - [1, 2, 3] [4, 5, 6] 2 A-B [-3, -3, -3]
4 [1, 2, 3] [4, 5, 6] 2 B-2 [2, 3, 4]
5 * [1, 2, 3] [4, 5, 6] 2 A*B [4, 10,18]
6 [1, 2, 3] [4, 5, 6] 2 A*2 [2, 4, 6]
7 / [1, 2, 3] [4, 5, 6] 2 B/A [4, 2, 2]
8 [1, 2, 3] [4, 5, 6] 2 B/2 [2, 2, 3]

Output:

tensor([5, 7, 9])
tensor([3, 4, 5])
tensor([-3, -3, -3])
tensor([2, 3, 4])
tensor([ 4, 10, 18])
tensor([2, 4, 6])
tensor([4, 2, 2])
tensor([2, 2, 3])

Vector Operation

Dot Product and linspace

We can perform the dot product of two tensors also. We use the dot() method of the torch to calculate which provide the accurate or expected result. There is another vector operation, i.e., linspace. For linspace, we use the method linspace (). This method contains two parameters first is the starting number, and the second is the ending number.

The output of this method is, it prints one hundred of equally spaced number from starting number to ending number.

Note: We can explicitly specify a step size rather than resorting to the default by passing one more argument, which is step size at the end of the parameter list.

Example

Output:

tensor(32)
tensor([2.0000, 2.0707, 2.1414, 2.2121, 2.2828, 2.3535, 2.4242, 2.4949, 2.5657,
        2.6364, 2.7071, 2.7778, 2.8485, 2.9192, 2.9899, 3.0606, 3.1313, 3.2020,
        3.2727, 3.3434, 3.4141, 3.4848, 3.5556, 3.6263, 3.6970, 3.7677, 3.8384,
        3.9091, 3.9798, 4.0505, 4.1212, 4.1919, 4.2626, 4.3333, 4.4040, 4.4747,
        4.5455, 4.6162, 4.6869, 4.7576, 4.8283, 4.8990, 4.9697, 5.0404, 5.1111,
        5.1818, 5.2525, 5.3232, 5.3939, 5.4646, 5.5354, 5.6061, 5.6768, 5.7475,
        5.8182, 5.8889, 5.9596, 6.0303, 6.1010, 6.1717, 6.2424, 6.3131, 6.3838,
        6.4545, 6.5253, 6.5960, 6.6667, 6.7374, 6.8081, 6.8788, 6.9495, 7.0202,
        7.0909, 7.1616, 7.2323, 7.3030, 7.3737, 7.4444, 7.5152, 7.5859, 7.6566,
        7.7273, 7.7980, 7.8687, 7.9394, 8.0101, 8.0808, 8.1515, 8.2222, 8.2929,
        8.3636, 8.4343, 8.5051, 8.5758, 8.6465, 8.7172, 8.7879, 8.8586, 8.9293,
        9.0000])

Vector Operation

Plotting a function on the two-dimensional coordinate system

The linspace function can come in use when plotting 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. For example, we can find the exponential of each x value for y.

Now, we are plotting x and y data using Map plot lib library, which is a visualization library for data analysis.

Example

Output:

Vector Operation

Note: For much smoother exponential we have to increase data in linspace. If it is 100 rather than 5, then the output will be smoother like that.

Vector Operation

Note: We can plot the sin value of x rather than exponential value. So it will create a sine called a curve.

Example

Output:

Vector Operation




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA