How to create a n dimensional torch tensor

This recipe helps you create a n dimensional torch tensor

Recipe Objective

How to create a n dimensional torch tensor?

This is achieved by using the torch.tensor(input, dim) function, in which the input is nothing but the values that we are going to specify and dim is nothing but dimensions, in which dimensions we want the output like 1-dimensional, 2-dimensional. Lets understand this with practical implementation.

Get Access to Plant Species Identification Project using Machine Learning

Step 1 - Import library

import torch

Step 2 - Make n dimensional torch tensor

Sample_tensor_0 = torch.tensor([99,22,44])
Sample_tensor_1 = torch.tensor([[55,66,77],[88,99,00]])
Sample_tensor = torch.tensor([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
print("This is a 0 dimensional torch tensor:","\n",torch.sum(Sample_tensor_0, dim=0), Sample_tensor_0.shape,"\n")
print("This is a 1 dimensional torch tensor:","\n",torch.sum(Sample_tensor_1, dim=1), Sample_tensor_1.shape, "\n")
print("This is a 2 dimensional torch tensor:","\n",torch.sum(Sample_tensor, dim=2), Sample_tensor.shape)

This is a 0 dimensional torch tensor: 
 tensor(165) torch.Size([3]) 

This is a 1 dimensional torch tensor: 
 tensor([198, 187]) torch.Size([2, 3]) 

This is a 2 dimensional torch tensor: 
 tensor([[ 6, 15],
        [24, 33]]) torch.Size([2, 2, 3])

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

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

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

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.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

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.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

AWS MLOps Project to Deploy Multiple Linear Regression Model
Build and Deploy a Multiple Linear Regression Model in Python on AWS

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

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

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.