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:

  1. n= torch.tensor([Tensor elements])  

Here, n is a variable of tensor type and tensor elements can be any integer or floating point number following with (,).

Example

import torch  

n=torch.tensor([1,2,3,4])  

print(n)

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

import torch  

n=torch.tensor ([1.0, 2.0, 3.0])  

print (n.dtype)

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

import torch  

n=torch.tensor([1.0, 2.0, 3.0])  

print(n[2])

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


  1. import torch  
  2. n=torch.tensor([1.0, 2.0, 3.0])  
  3. print(n[0:2]) 

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

import torch  

n=torch.tensor ([1.0, 2.0, 3.0])  

print(n[0:])

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

import torch  

n=torch.FloatTensor([1,2,3,4,5,6,7])  

print(n)

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


  1. import torch  
  2. n=torch.FloatTensor([1,2,3,4,5,6,7])  
  3. print(n.size())  

Output:

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


  1. import torch  
  2. n=torch.FloatTensor([1,2,3,4,5,6])  
  3. print(n)  
  4. n.view(6,1)     

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

import torch  

import numpy as np  

a=np.array([1,2,3,4,5,6])  

tensorcon=torch.from_numpy(a)  

print(tensorcon)  

print(tensorcon.type())

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


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *