Updated April 5, 2023

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

### 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 )
```

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

#### 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 –

#### 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 –

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

We hope that this EDUCBA information on “PyTorch expand” was beneficial to you. You can view EDUCBA’s recommended articles for more information.