What is the use of tensor view

This recipe explains what is the use of tensor view

Recipe Objective

What is the use of tensor.view?

The torch.view allows us to be a View of a existing tensor. With its base tensor the View shares the same underlying data. Explicitly data copying is avoided by supporting the view which will allow us for memory efficient and fast data reshaping, slicing and the element wise operation. Lets understand with an example.

Get Access to Plant Species Identification Project using Machine Learning

Step 1 - Import library

import torch

Step 2 - Take Sample data

data = torch.randn(2,6)
print("This is the Sample data:","\n",data)

This is the Sample data: 
 tensor([[-0.2411, -1.5348,  0.6823,  0.7178, -0.2301,  0.5801],
        [ 1.5759, -0.1746,  1.5680,  0.4865,  0.5272, -0.0037]])

Step 3 - Make a view

view = data.view(4,3)
print("This is the new view data:", "\n",view, "\n")
print("The tensor 'data' and the tensor 'view' will share the same underlying data:")
print(data.storage().data_ptr() == view.storage().data_ptr())

This is the new view data: 
 tensor([[-0.2411, -1.5348,  0.6823],
        [ 0.7178, -0.2301,  0.5801],
        [ 1.5759, -0.1746,  1.5680],
        [ 0.4865,  0.5272, -0.0037]])

The tensor 'data' and the tensor 'view' will share the same underlying data:
True

Step 4 - Modify the view

view[0][1] = 2.169
print("Modifying the view tensor will change the base tensor as well which is data", "\n", view, "\n","\n", data)

Modifying the view tensor will change the base tensor as well which is data 
 tensor([[-0.2411,  2.1690,  0.6823],
        [ 0.7178, -0.2301,  0.5801],
        [ 1.5759, -0.1746,  1.5680],
        [ 0.4865,  0.5272, -0.0037]]) 
 
 tensor([[-0.2411,  2.1690,  0.6823,  0.7178, -0.2301,  0.5801],
        [ 1.5759, -0.1746,  1.5680,  0.4865,  0.5272, -0.0037]])

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

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

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

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 .

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.