PyTorch View Tutorial [With 11 Examples]

This PyTorch tutorial will explain the usage of the PyTorch View in Python. The PyTorch view() function returns a new tensor with a similar number of data and should have a similar number of elements.

And additionally, we will also cover different examples related to PyTorch View. And we will cover these topics.

  • PyTorch view
  • PyTorch view example
  • PyTorch view batchsize
  • PyTorch view contiguous
  • PyTorch view flatten
  • PyTorch view in NumPy
  • PyTorch view size
  • PyTorch view transpose
  • PyTorch view reshape
  • PyTorch view unsqueeze
  • PyTorch to create a tensor with 8 elements and view with 2 rows and 4 columns
  • PyTorch to change the view of a tensor into 8 rows and 1 column

PyTorch view

In this section, we will learn about the PyTorch view in python.

The PyTorch view() function is used to convert the tensor into a 2D format which is rows and columns. And we should have a definite number of rows and columns to view.

Syntax:

The syntax of the PyTorch view is :

tensor.view(no_of_rows, no_of_columns)

Parameters:

  • no_of_rows: There are several rows that we can view.
  • no_of_columns: There are some columns that we can view.

This is how we understood the PyTorch view function.

Also, check: Introduction to PyTorch Lenet

PyTorch view example

In this section, we will learn about how to implement the PyTorch view with the help of an example.

The PyTorch view() function returns a new tensor with a similar number of data and should have a similar number of elements.

Code:

In the following code, we will import the torch library such as import torch.

  • a = torch.randn(6, 6): Here we are declaring a variable by using torch.randn() function.
  • b = a.view(36): Here we are describing a variable by using view() function.
  • c = a.view(-1, 12): Here the size -1 is deduced from other dimension.
 # Import library
import torch
# Describe a variable
a = torch.randn(6, 6)
torch.Size([6, 6])
# Using view() function
b = a.view(36)
# Print output
print(b)
b.size()
torch.Size([36])
# The size -1 is deduced from other dimensions
c = a.view(-1, 12)  
print(c)

Output:

After running the above code, we get the following output in which we can see that after using the view() function the new tensor is returned and the values of a new tensor are printed on the screen.

PyTorch view example
PyTorch view example

So, with this, we understood about the PyTorch view with the help of an example.

Read: PyTorch Add Dimension

PyTorch view Batchsize

In this section, we will learn about the PyTorch view batch size in python.

Before moving forward we should have a piece of knowledge about the batchsize.

The Batch size is defined as a process in which the number of samples prepared previous to the model is updated.

The PyTorch view batchsize is used to return the new tensor with a similar number of data.

Code:

In the following code, firstly we will import the torch library such as import torch.

  • pertens = torch.tensor([1,3,4]): Here we are declaring the Pertens variable by using torch.tensor() function.
  • pertens.view(3): Here we are using the view() function which returns the new tensor with a similar number of data.
# Import library
import torch

# Describe a variable
pertens = torch.tensor([1,3,4])

# Using view() function
pertens.view(3)

Output:

After running the above code, we get the following output in which we can see that the PyTorch batch size value is printed on the screen.

PyTorch view batchsize
PyTorch view batchsize

This is how we understood about the PyTorch view batch size.

Read: PyTorch Hyperparameter Tuning

PyTorch view contiguous

In this section, we will learn about the PyTorch view contiguous in python.

Before moving forward we should have a piece of knowledge about contiguous.

The contiguous is defined as next or together in sequence or we can say that sharing a common border.

Here we are proceeding with a view of a contiguous tensor that produces a non-contiguous tensor.

Code:

In the following code, we will import the torch module such as import torch.

  • c = torch.rand(6, 6): Here we are describing a variable by using torch.rand() function.
  • c.storage().data_ptr() == b.storage().data_ptr() is used to share the same underlying data.
  • s = torch.tensor([[1, 2],[3, 4]]): Here we are declaring the s variable by using torch.tensor() function.
  • c = s.transpose(0, 1): Here c is a view of s.
  • m = c.contiguous(): Here we get a contiguous tensor by calling contiguous() function and copying the data when c is not contiguous.
# Import library
import torch
c = torch.rand(6, 6)
d = c.view(4, 9)
# c and d share the same underlying data.
c.storage().data_ptr() == b.storage().data_ptr()  
# Modifying view tensor changes base tensor as well.
d[0][0] = 3.14
c[0][0]

s = torch.tensor([[1, 2],[3, 4]])
s.is_contiguous()

 # c is a view of s. 
c = s.transpose(0, 1) 
# View tensors might be non-contiguous.
c.is_contiguous()

# Here we get a contiguous tensor by calling contiguous() function
# And copying the data when c is not contiguous.
m = c.contiguous()
print(m)

Output:

After running the above code, we get the following output in which we can see that the PyTorch view contiguous values are printed on the screen.

PyTorch view contiguous
PyTorch view contiguous

This is how we can understand about the PyTorch view contiguous.

Read: PyTorch Linear Regression 

PyTorch view flatten

In this section, we will learn about the PyTorch view flatten in python.

The flatten is defined as a process that is used to flatten an n-dimensional tensor to a one-dimensional tensor.

Code:

In the following code, we will import the torch library such as import torch.

  • a = torch.tensor([[2,4,6,8,10,12], [2,4,6,8,10,12]]): Here we are creating an two dimensional tensor with six elements by using torch.tensor() function.
  • print(a) is used to display an actual tensor.
  • print(torch.flatten(a)) is used to flatten a tensor with flatten() function.
  • a.view(2,6): Here we are using a view() function that return a new tensor.
# import torch module
import torch
 
# create an two dimensional tensor with 6 elements each
a = torch.tensor([[2,4,6,8,10,12],
                  [2,4,6,8,10,12]])
 
# Here we are displaying actual tensor
print(a)
 
# flatten a tensor with flatten() function
print(torch.flatten(a))

# Using view() function
a.view(2,6)

Output:

After running the above code, we get the following output in which we can see that the PyTorch view flattening values are printed on the screen.

PyTorch view flatten
PyTorch view flatten

So, with this, we understood about the PyTorch view flatten.

Read: PyTorch Numpy to Tensor

PyTorch view in numpy

In this section, we will learn about the PyTorch view in numpy in python.

The Pytorch view is used to register on a torch tensor to convert its shape. And convert the shape of ndarray with the help of reshaping that is a numpy() function.

Code:

In the following code, firstly we import the torch library such as import torch.

  • m = torch.ones((4, 6, 8)): Here we are declaring a variable by using torch.ones() function.
  • m.view(-1, 12).size(): Here we are using the view() function that is used to return the new tensor.
  • torch.Size([2, 12]): Here we are declaring the size by using torch.size() function.
# Import library
import torch
# Describe a variable
m = torch.ones((4, 6, 8))
m.size()
torch.Size([4, 6, 8])
# Using view() function
m.view(-1, 12).size()
# Declaring the size
torch.Size([2, 12])

Output:

After running the above code, we get the following output in which we can see that the PyTorch view in numpy value is printed on the screen.

PyTorch view in numpy
PyTorch view in NumPy

So, with this, we understood about the PyTorch view in numpy.

Read: PyTorch RNN – Detailed Guide

PyTorch view size

In this section, we will learn about the PyTorch view size in python.

As we know the PyTorch view() function returns the new tensor with similar data and should have a similar number of elements but have different sizes. The view size can be consistent with its real size.

Code:

In the following code, firstly we will import the torch library such as import torch.

  • x = torch.randn(2, 4, 6, 8): Here we are describing a variable by using torch.randn() function.
  • torch.Size([2, 4, 6, 8]) is used to define the size of a tensor.
  • y = x.view(2, 6, 4, 8): Here we are using a view() function that does not change the tensor layout in memory.
  • y.size(): Here we are defining the size of the view.
# Import Library
import torch
# Describing a variable
x = torch.randn(2, 4, 6, 8)
# Define the size of the tensor
x.size()
torch.Size([2, 4, 6, 8])
# Using view() function that does not change tensor layout in a memory
y = x.view(2, 6, 4, 8) 
# Define the size of the view 
y.size()

Output:

After running the above code, we get the following output in which you can see that the PyTorch view size value is painted on the screen.

PyTorch view size
PyTorch view size

So, with this, we understood about the PyTorch view size.

Read: PyTorch MNIST Tutorial

PyTorch view transpose

In this section, we will learn about the PyTorch view transpose in python.

The PyTorch view transpose is defined as a process that transfers the tensor to a different context or a place or we can say that changing the place of two or more things.

Code:

In the following code, we will import the torch library such as import torch.

  • x = torch.randn(4,8,16,24): Here we are describing a variable by using a torch.randn() function.
  • torch.Size([4,8,16,24]): Here we are defining the size of the tensor.
  • y = x.transpose(2, 3): Here we are swapping the third and fourth dimensions.
  • z = x.view(4,8,24,16): Here we are using the view() function that does not change the tensor layout in memory.
# Import Library
import torch
# Describing a variable
x = torch.randn(4,8,16,24)
# Define the size of the tensor
x.size()
torch.Size([4,8,16,24])
# Here we are swapping the third and fourth dimension
y = x.transpose(2, 3)  
y.size()
torch.Size([4,8,16,24])
# Using view() function that does not change tensor layout in a memory
z = x.view(4,8,24,16)  
z.size()
torch.Size([4,8,24,16])
torch.equal(y, z)

Output:

In the below output, you can see that the PyTorch view transpose value is printed on the screen.

PyTorch view transpose
PyTorch view transpose

So, with this, we understood about the PyTorch view transpose.

Read: PyTorch Binary Cross Entropy

PyTorch view reshape

In this section, we will learn about the PyTorch view reshape in python.

The PyTorch view reshape function is used to reshape or change the dimensions into a given shape. The tensor is defined as an input tensor.

Code:

In the following code, we will import the torch library such as import torch.

  • r = torch.tensor([2, 4, 6, 8, 10, 12]): Here we are creating a one-dimensional tensor with size elements by using torch.tensor() function.
  • print(r.shape) is used to print the tensor shape.
  • print(r) is used to show the actual shape.
  • print(r.reshape([3, 2])) is used to reshape the tensor into three rows and two columns.
  • print(r.shape) is used to show the shape of the reshape tensor.
  • r.view(3,2): Here we are using a view() function that returns the new tensor.
# Import linrary
import torch
 
# Here we are creating  a one-dimensional tensor with 6 elements
r = torch.tensor([2, 4, 6, 8, 10, 12])
 
# Showing tensor shape
print(r.shape)
 
# Showing  actual tensor
print(r)
 
# Reshape tensor into 3 rows and 2 columns
print(r.reshape([3, 2]))
 
# Showing the shape of reshaped tensor
print(r.shape)

# Using view() function
r.view(3,2)

Output:

After running the above code, we get the following output in which we can see that the PyTorch view reshape value is printed on the screen.

PyTorch view reshape
PyTorch view reshape

So, with this, we understood about the PyTorch view reshape.

Read: PyTorch Dataloader + Examples

PyTorch view unsqueeze

In this section, we will learn about the PyTorch view unsqueeze in python.

Before moving forward we should have a piece of knowledge about the unsqueeze.

The unsqueeze() function returns a new tensor with a dimension of size one inserted at the defined position.

Code:

In the following code, we will import the torch library such as import torch.

  • a = torch.tensor([2, 3, 4, 5]): Here we are delaring the variable by using torch.tensor() function.
  • m=torch.unsqueeze(a, 0): Here we are using the unsqueeze() function to add dimension.
  • print(m) is used to print the output with the help of the print() function.
  • m.view(2,2): Here we are using the view() function that returns the new tensor.
# Import library
import torch

# Declare a variable
a = torch.tensor([2, 3, 4, 5])
# Using unsqueeze() method to add dimension
m=torch.unsqueeze(a, 0)
# Print output
print(m)

# Using view() function
m.view(2,2)

Output:

After running the above code, we get the following output in which we can see that the PyTorch view unsqueeze value is printed on the screen.

PyTorch view unsqueeze
PyTorch view unsqueeze

This is how we can understand about the PyTorch view squeeze.

Read: PyTorch Early Stopping + Examples

PyTorch to create a tensor with 8 elements and view with 2 rows and 4 columns

In this section, we will learn about how to create a tensor with 8 elements and a view with 2 rows and 4 columns in PyTorch.

Here we are using the view() function. The view() function is used to convert the tensor into two-dimensional format rows and columns and here we create a tensor with 8 elements and a view with 2 rows and 4 columns.

Code:

In the following code, firstly we import the torch library such as import torch.

  • a=torch.FloatTensor([25, 30, 35, 40, 45,50,55,60]): Here we are creating one dimensional tensor with 8 elements.
  • print(a.view(2, 4)) is used to view tensor in 2 rows and 4 columns.
  • print(a.view(2, 4)) is used to view tensor in 3 rows and 4 columns.
# import torch library
import torch
 
# Here we are creating one dimensional tensor with 8 elements
a=torch.FloatTensor([25, 30, 35, 40,
                     45,50,55,60]) 
 
# view tensor in 2 rows and 4 columns
print(a.view(2, 4))
  
# view tensor in 3 rows and 4 columns
print(a.view(2, 4))

Output:

After running the above code, we get the following output in which we can see that the PyTorch one-dimensional tensor with 8 elements and view with 2 rows and 4 columns values are printed on the screen.

PyTorch to create a tensor with 8 elements and view with 2 rows and 4 columns
PyTorch to create a tensor with 8 elements and view with 2 rows and 4 columns

So, with this, we understood about how to create a tensor with 8 elements and view with 2 rows and 4 columns.

Read: PyTorch Pretrained Model

PyTorch to change the view of a tensor into 8 rows and 1 column

In this section, we will learn about how to change the view of a tensor into eight rows and one column in PyTorch.

As we know the PyTorch view function is used to convert the tensor into a 2D format which is rows and columns. And we should have a definite number of rows and columns to view.

Code:

In the following code, we will import the torch module such as import torch.

  • a = torch.FloatTensor([25, 30, 35, 40, 45,50,55,60]): Here we are creating one dimensional tensor with eight elements.
  • print(a.view(8, 1)): Here we are viewing tensor in 8 rows and 1 column.
  • print(a.view(1, 8)): Here we are viewing tensor in 1 row and 8 columns.
# import torch
import torch
 
# Here we are creating one dimensional tensor 8 elements
a = torch.FloatTensor([25, 30, 35, 40,
                     45,50,55,60]) 
 
# Here we are viewing tensor in 8 rows and 1 column
print(a.view(8, 1))
  
# Here we are viewing tensor in 1 row and 8 columns
print(a.view(1, 8))

Output:

After running the above code, we get the following output in which we can see that the changing view tensor into 8 rows and 1 column values are printed on the screen.

PyTorch to change the view of a tensor into 8 rows and 1 column
PyTorch to change the view of a tensor into 8 rows and 1 column

This is how we can understand about how to change the view of a tensor into 8 rows.

You may also like to read the following Python PyTorch tutorials.

So, in this tutorial, we discussed the PyTorch view and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • PyTorch view
  • PyTorch view example
  • PyTorch view batchsize
  • PyTorch view contiguous
  • PyTorch view flatten
  • PyTorch view in numpy
  • PyTorch view size
  • PyTorch view transpose
  • PyTorch view reshape
  • PyTorch view unsqueeze
  • PyTorch to create a tensor with 8 elements and view with 2 rows and 4 columns
  • PyTorch to change the view of a tensor into 8 rows and 1 column