What are contiguous tensors What is the use of narrow function

This recipe explains what are contiguous tensors This recipe explains what is the use of narrow function

Recipe Objective

What are contiguous tensors?

What is the use of narrow function? In PyTorch, there are some operations which will know how to convert indices in to tensor to the byte location, but it will not change the content of the tensor. The operations are:
"transpose()", "narrow()", "expand()", and "view()".
When we call the "contiguous" it will make a copy of the tensor because the order of the elements have to be the same in case of shape which are created from scratch. Here we are going to see the "narrow()" operation, which will return narrowed version of the input tensor which we can say a new tensor. The syntax for the function is as follows:
torch.narrow(input, dim, start, length) → Tensor
where,
-- input - it is the input tensor.
-- dim - This is the dimension along which to narrow.
-- start - This is the starting dimension.
-- length - This is the distance to the ending dimension.

List of Classification Algorithms in Machine Learning

Step 1 - Import library

import torch

Step 2 - Take Sample data

Sample = torch.tensor([[22,33,44], [66,77,88], [88,99,100]])

Step 3 - Apply narrow

print("This is the narrow function where we are keeping dim=0, start=0 and length=2:", "\n", torch.narrow(Sample, 0, 0, 2), "\n") print("This is the narrow function where we are keeping dim=1, start=1 and length=2:", "\n",torch.narrow(Sample, 1, 1, 2))

This is the narrow function where we are keeping dim=0, start=0 and length=2: 
 tensor([[-1.3090, -1.9248,  1.0949,  0.0182,  0.4671],
        [-1.2347,  0.4116,  0.4400,  0.4498,  0.3900]]) 

This is the narrow function where we are keeping dim=1, start=1 and length=2: 
 tensor([[-1.9248,  1.0949],
        [ 0.4116,  0.4400],
        [-1.0873, -0.4315],
        [ 0.8892,  0.8937]])

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.