What is a Torch Tensor?

This beginner-friendly Pytorch code introduces you to the concept of a torch tensor with the help of a simple example.

Objective: What is a Torch Tensor?

This PyTorch example introduces you to the fundamentals of a torch tensor, highlighting the various torch tensor types and an easy-to-understand torch tensor code example.

What is a Torch Tensor?

The Torch tensor is a multi-dimensional matrix containing a single data type element. Ten tensor types are defined by the torch with CPU and GPU variants. The different tensor types are:

  • Integer  (Should I change these into H3?)

  1. Data type - 8-bit integer (unsigned)
    dtype - torch.uint8
    CPU tensor - torch.ByteTensor
    GPU tensor- torch.cuda.ByteTensor

  2. Data type - 8-bit integer (signed)
    dtype - torch.int8
    CPU tensor - torch.CharTensor
    GPU tensor - torch.cuda.CharTensor

  3. Data type - 16-bit integer (signed)
    dtype - torch.int16 or torch.short
    CPU tensor - torch.ShortTensor
    GPU tensor - torch.cuda.ShortTensor

  4. Data type -32-bit integer (signed)
    dtype - torch.int32 or torch.int
    CPU tensor - torch.IntTensor
    GPU tensor - torch.cuda.IntTensor

  5. Data type - 64-bit integer (signed)
    dtype - torch.int64 or torch.long
    CPU tensor - torch.LongTensor
    GPU tensor - torch.cuda.LongTensor

  • Complex

  1. Data type - 32-bit complex
    dtype - torch.complex32

  2. Data type - 64-bit complex
    dtype - torch.complex64

  3. Data type - 128-bit complex
    dtype - torch.complex128 or torch.cdouble

  • Floating

  1. Data type - 32-bit floating point
    dtype - torch.float32 or torch.float
    CPU tensor - torch.FloatTensor
    GPU tensor - torch.cuda.FloatTensor

  2. Data type - 64-bit floating point
    dtype - torch.float64 or torch.double
    CPU tensor - torch.DoubleTensor
    GPU tensor - torch.cuda.DoubleTensor

  3. Data type - 16-bit floating point 1
    dtype - torch.float16 or torch.half
    CPU tensor - torch.HalfTensor
    GPU tensor - torch.cuda.HalfTensor

  4. Data type - 16-bit floating point 2
    dtype - torch.bfloat16
    CPU tensor - torch.BFloat16Tensor
    GPU tensor - torch.cuda.BFloat16Tensor

  • Boolean

  1. Data type - Boolean
    dtype - torch.bool
    CPU tensor - torch.BoolTensor
    GPU tensor - torch.cuda.BoolTensor

You can easily convert a PyTorch tensor from one data type to another. For example, you can convert a torch tensor to int using the .int() method and a torch tensor to float using the .float() method.

Steps Showing How To Use A Torch Tensor

The following steps will help you understand how to use a torch tensor with the help of an easy-to-understand torch tensor example.

Step 1 - Import Libraries

First, you must import the required libraries.

import torch import numpy as np

Step 2 - Create Torch Tensor

The next is to create sample torch tensors.

Sample_tensor_1 = torch.tensor([[-2., 2.],[8., -9.]]) Sample_tensor_2 = torch.tensor(np.array([[30, 40, 56], [84, 35, 26]]))

Step 3 - Print Torch Tensor Results

The final step is to print the two sample torch tensors.

print("This is first Sample torche tensor with their dtype", "\n",Sample_tensor_1, Sample_tensor_1.dtype) print("-------------------------------------------------------------------------------------------------") print("This is Second Sample torche tensor with their dtype", "\n",Sample_tensor_2, Sample_tensor_2.dtype)

The output of the above code is-

This is first Sample torche tensor with their dtype 

 tensor([[-2.,  2.],

        [ 8., -9.]]) torch.float32

-------------------------------------------------------------------------------------------------

This is Second Sample torche tensor with their dtype 

 tensor([[30, 40, 56],

        [84, 35, 26]]) torch.int64

{"mode":"full","isActive":false}

How To Convert Torch Tensor To Numpy Array?

There are two ways to convert torch tensor to NumPy array-

  1. Using The .numpy() Method

This method returns a NumPy array that shares the same underlying data as the PyTorch tensor. Any changes made to the NumPy array will also be reflected in the PyTorch tensor and vice versa.

  1. Using The torch.from_numpy() Function

This function creates a new PyTorch tensor from a NumPy array. The new PyTorch tensor will have the same data type and dimensions as the NumPy array.

How To Convert Numpy Array To Torch Tensor?

There are two ways to convert NumPy to torch tensor-

  1. Using The torch.from_numpy() Function

This function creates a new PyTorch tensor from a NumPy array. The new PyTorch tensor will have the same data type and dimensions as the NumPy array.

  1. Using The .as_tensor() Method

This method converts a NumPy array to a PyTorch tensor by replacing the original NumPy array with the new PyTorch tensor.

Get Access To 70+ Enterprise-Grade Solved End-to-End ML Projects And Become A Data Science Pro

How To Convert Torch Tensor To List?

There are two ways to convert a PyTorch tensor to a list:

  • The .tolist() method returns a nested Python list containing the same elements as the PyTorch tensor.

  • Using a ‘for’ loop iterates over the PyTorch tensor and appends each element to a Python list.

Some Commonly-Used Torch Tensor Functions

Here are some of the commonly used torch tensor functions are-

  1. Torch Tensor- ‘append’

The torch.cat() function is used to append torch tensors. It takes a sequence of tensors and a dimension as input and returns a new tensor that concatenates the input tensors along the specified dimension.

  1. Torch Tensor- ‘transpose’

The torch.transpose() function is used to transpose torch tensors. It takes two dimensions as input and returns a new tensor that transposes the input tensor along the specified dimensions.

  1. Torch Tensor- ‘max’

The torch.max() function is used to compute the maximum value of a torch tensor. It takes two arguments: the tensor to compute the maximum of and the dimension along which to compute the maximum.

  1. Torch Tensor- ‘shape’

The torch.shape() function is used to get the shape of a torch tensor. It returns a tuple that contains the dimensions of the tensor.

  1. Torch Tensor- ‘reshape’

The torch.reshape() function is used to reshape a torch tensor. It takes two arguments: the tensor to reshape and the desired shape of the tensor.

  1. Torch Tensor- ‘view’

The torch.view() function is similar to the torch.reshape() function, but it does not change the underlying data of the original tensor. This means that any changes made to the new view of the tensor will also be reflected in the original tensor.

Learn Real-World Implementations Of Torch Tensor With ProjectPro

This Torch Tensor example has helped you gain a comprehensive understanding of working with Torch Tensors in PyTorch, covering key topics such as torch tensor basics, conversion to NumPy arrays, and common tensor functions. Understanding Torch Tensors is crucial for effectively working with PyTorch in various machine learning and deep learning tasks. To master PyTorch and its applications in real-world data science and machine learning solutions, check out the enterprise-grade end-to-end solved projects by ProjectPro. These hands-on projects, expert guidance, and practical experience help you develop the skills and confidence to tackle complex data science challenges.

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.