How to append to a torch tensor

This recipe helps you append to a torch tensor

Recipe Objective

How to append to a torch tensor?

This is achieved by using the expand function which will return a new view of the tensor with its dimensions expanded to larger size. It is important to do because at some time if we have two tensors one is of smaller dimension and another is of larger one. Then we need to match the dimension of Smaller tensor with the larger one, at that time this function plays a major role. Lets understand this with practical implementation.

Step 1 - Import library

import torch

Step 2 - Take Sample tensor

Sample = torch.tensor([[10], [15], [20], [25]])
print("This is a Sample tensor with its size:", "\n",Sample, Sample.size())

This is a Sample tensor with its size: 
 tensor([[10],
        [15],
        [20],
        [25]]) torch.Size([4, 1])

Step 3 - Expand the dimension

expanded = Sample.expand(4,3)
print("This is the expanded view of sample tensor with its size:", "\n",expanded, expanded.size())

This is the expanded view of sample tensor with its size: 
 tensor([[10, 10, 10],
        [15, 15, 15],
        [20, 20, 20],
        [25, 25, 25]]) torch.Size([4, 3])

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

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.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

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

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

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 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.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python