EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials PyTorch Tutorial PyTorch expand
Secondary Sidebar
PyTorch Tutorial
  • PyTorch
    • PyTorch Image Classification
    • PyTorch Random
    • PyTorch Variable
    • PyTorch Activation Function
    • Python Formatted String
    • PyTorch GPU
    • PyTorch CUDA
    • PyTorch DataLoader
    • PyTorch LSTM
    • PyTorch Pad
    • PyTorch OpenCL
    • PyTorch Lightning
    • PyTorch SoftMax
    • PyTorch Flatten
    • PyTorch gan
    • PyTorch max
    • PyTorch pip
    • PyTorch Parameter
    • PyTorch Load Model
    • PyTorch Distributed
    • PyTorch BERT
    • PyTorch interpolate
    • PyTorch JIT
    • PyTorch expand
    • PyTorch AMD
    • PyTorch GRU
    • PyTorch rnn
    • PyTorch permute
    • PyTorch argmax
    • PyTorch SGD
    • PyTorch nn
    • PyTorch One Hot Encoding
    • PyTorch Tensors
    • What is PyTorch?
    • PyTorch MSELoss()
    • PyTorch NLLLOSS
    • PyTorch MaxPool2d
    • PyTorch Pretrained Models
    • PyTorch Squeeze
    • PyTorch Reinforcement Learning
    • PyTorch zero_grad
    • PyTorch norm
    • PyTorch VAE
    • PyTorch Early Stopping
    • PyTorch requires_grad
    • PyTorch MNIST
    • PyTorch Conv2d
    • Dataset Pytorch
    • PyTorch tanh
    • PyTorch bmm
    • PyTorch profiler
    • PyTorch unsqueeze
    • PyTorch adam
    • PyTorch backward
    • PyTorch concatenate
    • PyTorch Embedding
    • PyTorch Tensor to NumPy
    • PyTorch Normalize
    • PyTorch ReLU
    • PyTorch Autograd
    • PyTorch Transpose
    • PyTorch Object Detection
    • PyTorch Autoencoder
    • PyTorch Loss
    • PyTorch repeat
    • PyTorch gather
    • PyTorch sequential
    • PyTorch U-NET
    • PyTorch Sigmoid
    • PyTorch Neural Network
    • PyTorch Quantization
    • PyTorch Ignite
    • PyTorch Versions
    • PyTorch TensorBoard
    • PyTorch Dropout
    • PyTorch Model
    • PyTorch optimizer
    • PyTorch ResNet
    • PyTorch CNN
    • PyTorch Detach
    • Single Layer Perceptron
    • PyTorch vs Keras
    • torch.nn Module

PyTorch expand

PyTorch expand

Introduction to PyTorch expand

PyTorch expand is the attribute available in the tensor library that can be used to execute the operation of expansion. This attribute expands the specified tensor object as input to a new dimension. The new dimension where the tensor is expanded is a singleton one.

In this article, we will try to understand what is PyTorch expands, how to use PyTorch expand, how to perform PyTorch expand; for example, PyTorch expands and conclusion for this from our side.

One more thing to note here is that as a new view is created and not an object after expansion, none of the memory allocations are done to expand the operation. Instead, what happens internally is the dimension of value size 1 is expanded and turned to a bigger size by setting the value of the stride attribute to zero.

The only parameter that needs to be transferred is the sizes that specify the size of the desired expansion of the tensor object.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

What is PyTorch expand?

PyTorch expand is the attribute available in the tensor library that allows the user to extend the supplied tensor input in a particular new singleton dimension. The resultant and the effects of doing all this are as follows –

  • When we use the expand attribute to expand the specified tensor object, a new copy is not created, and what we can see the new view for the same original tensor value that we passed.
  • When the value of the particular dimension is set to -1, then the expansion of the source tensor does not happen along that dimension.
  • Let us consider one example to understand this behavior. When we have a particular tensor object (5,1), we can perform the expansion using the tensor.expand () attribute along with the dimension value of 1 size.

How to use PyTorch expand?

We can use PyTorch expand attribute available in the tensor library by simply importing the tensor at the top of your program. We can extend the tensor along the dimension, which is a singleton in value. Though expanding the tensor to a bigger number of dimensions is also allowed, the resultant tensor will contain all the new values appended at the front side. Also, for doing so, we cannot set the value of the size of dimension at -1.

How to perform PyTorch expand?

Let us discuss how we can perform PyTorch expand using tensor.expand() attribute in PyTorch by using the following bullet pointers –

  • The first step will be importing the required library of torch where the definition of tensor and expand lies. Before you import the library, give a check on whether you have installed it priorly or not. The import statement looks like – import torch.
  • Now, we will have to define the object of a tensor which will act as a source for expansion, and its value should contain at least one dimension having a singleton value. For example, it can be defined as – sampleEducbaTensor = torch.tensor ( [[1],[2],[3]])
  • Now, it’s time to perform the expand operation on over tensor value along the singleton dimension. Note one thing here: expanding the tensor object along the non-singleton dimension will result in a runtime error. We will soon be seeing one example related to this in the examples section. Expansion can be done by using the statement educbaResultantExpandedTensor = sampleEducbaTensor. expand (3,2)
  • The last step is to display the resultant output to the user, which includes our expanded tensor –

Print (“The expanded tensor is,” educbaResultantExpandedTensor)

Example PyTorch expand

Let us now understand the implementation of the PyTorch expand and how it works along with the help of certain examples –

Example #1

In this example, we will try to expand the tensor having the size (3,1) to (3,2). While doing so, expansion happens only in the single dimension of value 1, and the other dimension of size 3 remains untouched and unchanged.

# We will import the torch library that will be required to create tensor object
import torch
# define and initiate the tensor object
sampleEducbaTensor = torch.tensor([[1],[2],[3]])
# Display the value of tensor in the output screen
print("Input tensor value :\n", sampleEducbaTensor )
print("Size of input tensor :\n", sampleEducbaTensor.size())
# Perform the expansion of the original tensor
sampleExpandedValue = sampleEducbaTensor.expand(3,2)
print("Value of tensor after expanding :\n", sampleExpandedValue )

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,560 ratings)

The output of the execution of the above program gives the following resultant –

PyTorch expand output 1

Example #2

Now, we will expand the tensor having the size (1,3) to (3,3). Even in this case, the expansion of the tensor will be carried out along the 1 size of the dimension.

# We will import the torch library that will be required to create tensor object
import torch
# define and initiate the tensor object
sampleEducbaTensor2 = torch.tensor([[1,2,3]])
# Display the value of tensor in the output screen
print("Value of input tensor object :\n", sampleEducbaTensor2 )
# size of tensor is [1,3] print("Size of input tensor :\n", sampleEducbaTensor2.size())
# Perform the expansion of the original tensor
sampleEducbaExpandedTensor = sampleEducbaTensor2.expand(3,-1)
print("Value of the resultant expanded tensor:\n", sampleEducbaExpandedTensor )
print("Size of output tensor :\n", sampleEducbaExpandedTensor.size())

The execution of the above program gives the following output as resultant –

PyTorch expand output 2

Example #3

When we attempt on expanding the tensor along the non-singleton dimension, then it will result in the runtime error as demonstrated in this example –

# We will import the torch library that will be required to create tensor object
import torch
# define and initiate the tensor object
sampleEducbaTensor3 = torch.tensor([[1,2,3]])
# Display the value of tensor in the output screen
print("Value of input tensor :\n", sampleEducbaTensor3 )
# size of tensor is [1,3] print("Size of the input tensor object :\n", sampleEducbaTensor3.size())
sampleEducbaTensor3.expand(3,4)

The execution of the above program gives the following output along with the runtime error as we are expanding the tensor along the dimension of a larger number –

output 3

Conclusion

PyTorch expand used for expanding the tensor object along the dimension having the singleton size. While doing so, only a new view of the expanded tensor is created and not a new object.

Recommended Articles

This is a guide to PyTorch expand. Here we discuss the implementation of the PyTorch expand and how it works along with the help of certain examples. You may also have a look at the following articles to learn more –

  1. PyTorch Conv2d
  2. PyTorch TensorBoard
  3. What is PyTorch?
  4. PyTorch Versions
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more