One Dimensional TensorsAs we know, PyTorch has been embraced by Deep learning world for the ability to conveniently define neural network. Neural network is fundamentally structured to sensors, and PyTorch is also built around sensors. There tends to be a significant boost in performance. Vaguely a tensor is a generalization of matrices. 1DTensor is similar to 1D matrix. In one dimensional Tensor have only one row and one column which is known as vector. There is a zerodimensional tensor also which is known as a scalar. Now we will discuss operations which are performed on tensors. We can use Google Colab also to write the code of Tensor. Accessing Google Colab is very simple. For Google Colab, There is no setup required. It runs entirely on the cloud. Google Colab is similar to Jupyter Notebook. Many packages come preinstall for us when using Google Colab. Unfortunately, the torch is not one of them, so we have first to install torch using !pip3 install torch command. Now, we will perform the operation on onedimensional Tensor. Creating onedimensional TensorFor creating a onedimensional Tensor, we use the tensor property of torch library. To create a tensor, we use the torch.tensor() method. Syntax of creating one dimensional tensor is as follows: Here, n is a variable of tensor type and tensor elements can be any integer or floating point number following with (,). ExampleOutput: tensor([1, 2, 3, 4]) Checking data type of elements in TensorWe can check the data type of the element which contains in Tensor. We use dtype() of Tensor to find the data type. ExampleOutput: torch.float32 Accessing of Tensor's elementsWe can access the elements of Tensor with the help of the index of that element. If we want to print all the elements of Tensor, then we can print the tensor variable. Like the onedimensional metrics index, Tensor index also starts from 0. ExampleOutput: tensor(3.) Accessing of Tensor's elements with the specified rangeIt is quite simple to access elements of specified range by passing the starting index or ending index of elements separated with a colon (:). It will skip starting index element and print elements till ending index. ExampleOutput: tensor (2.0,3.0) We have another example which prints all elements by skipping the starting index, which is initialized by us. ExampleOutput: tensor (2.0,3.0) Creating of Floating Point Tensor using Integer elementsWe can create a floating point Tensor using integer element. In this, we use FloatTensor property of torch is used. ExampleOutput: tensor([1., 2., 3., 4., 5., 6., 7.]) Finding size of the TensorJust like one dimensional metrics, we can find the size of Tensor also. We use size() method of Tensor to get the size. ExampleOutput: torch.Size([7]) Change view of TensorTensor has the property by which we can change the view of the Tensor. Changing view means if a tensor is one dimensional (one row and one column) and we want to change its view by six rows and one column.Changes can be done with the help of view() of Tensor. It is similar to the reshape () of an array. ExampleOutput: tensor ([1., 2., 3., 4., 5., 6.]) tensor([[1.], [2.], [3.], [4.], [5.], [6.]]) Note: We can use other dimension also such as (3, 2) but it should be compatible with our original tensor elements.Tensor using numpy arrayWe can also create Tensor using numpy array. We have to convert the numpy array into Tensor with the help of from_numpy () of the torch. For this, we first have to initialize numpy and then create a numpy array. ExampleOutput: tensor([1, 2, 3, 4, 5, 6]) torch.LongTensor
Next TopicVector Operation
