Home » PyTorch One Dimensional Tensor | 1D Tensor

PyTorch One Dimensional Tensor | 1D Tensor

by Online Tutorials Library

One Dimensional Tensors

As 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.

One Dimensional Tensors

1D-Tensor is similar to 1D- matrix. In one dimensional Tensor have only one row and one column which is known as vector. There is a zero-dimensional 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.

One Dimensional Tensors

Google Colab is similar to Jupyter Notebook. Many packages come pre-install 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 one-dimensional Tensor.

Creating one-dimensional Tensor

For creating a one-dimensional 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 (,).

Example

Output:

tensor([1, 2, 3, 4])  

One Dimensional Tensors

Checking data type of elements in Tensor

We can check the data type of the element which contains in Tensor. We use dtype() of Tensor to find the data type.

Example

Output:

torch.float32  

One Dimensional Tensors

Accessing of Tensor’s elements

We 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 one-dimensional metrics index, Tensor index also starts from 0.

Example

Output:

tensor(3.)  

One Dimensional Tensors

Accessing of Tensor’s elements with the specified range

It 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.

Example

Output:

tensor (2.0,3.0)  

One Dimensional Tensors

We have another example which prints all elements by skipping the starting index, which is initialized by us.

Example

Output:

tensor (2.0,3.0)  

One Dimensional Tensors

Creating of Floating Point Tensor using Integer elements

We can create a floating point Tensor using integer element. In this, we use FloatTensor property of torch is used.

Example

Output:

tensor([1., 2., 3., 4., 5., 6., 7.])  

One Dimensional Tensors

Finding size of the Tensor

Just like one dimensional metrics, we can find the size of Tensor also. We use size() method of Tensor to get the size.

Example

Output:

torch.Size([7])  

One Dimensional Tensors

Change view of Tensor

Tensor 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.

Example

Output:

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.

One Dimensional Tensors

Tensor using numpy array

We 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.

Example

Output:

tensor([1, 2, 3, 4, 5, 6])  torch.LongTensor  

One Dimensional Tensors


Next TopicVector Operation

You may also like