- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Change the View of Tensor in PyTorch

PyTorch is an open−source machine learning library developed by Facebook's AI Research team. It is primarily used for deep learning applications, including natural language processing, computer vision, and reinforcement learning. PyTorch provides two main features: a multidimensional Tensor object and a collection of functions that operate on these Tensors.

## Torch

The Tensor object in PyTorch is similar to NumPy's ndarray, but with added capabilities for utilizing GPUs and building dynamic computational graphs. These computational graphs are constructed on−the−fly during the execution of the program, allowing for efficient memory usage and more flexible model architectures. Additionally, PyTorch has a modular design that makes it easy to build and experiment with different neural network models.

The torch module in PyTorch is the primary library for building neural networks. It provides a wide range of building blocks for constructing various types of neural networks, including fully connected layers, convolutional layers, recurrent layers, and many more. These building blocks can be combined to create complex models with custom architectures.

The torch module also includes many functions for performing common operations in neural networks, such as activation functions (e.g., ReLU, sigmoid), loss functions (e.g., cross−entropy, mean squared error), and optimization algorithms (e.g., stochastic gradient descent, Adam).

Furthermore, the torch module in PyTorch includes tools for data loading and preprocessing, as well as utilities for evaluating and visualizing the performance of models. It also provides a simple and flexible interface for training and deploying models on different devices, including CPUs, GPUs, and distributed computing environments.

This PyTorch tutorial will demonstrate how to alter the shape of a tensor using PyTorch, an open−source framework compatible with the Python programming language. Tensors are multidimensional arrays utilized for data storage, and the torch module must be imported to use them. The tensor() method is used to create a tensor.

### Syntax

torch.tensor(data)

Where data is a multi-dimensional array.

The first step that we need is to install the torch module and we can do that with the help of the command shown below.

### Command

pip3 install torch torchvision

Once we run the above command, the torch module will be installed on our machine.

## tensor.view()

In PyTorch, the view method of a Tensor is used to reshape the dimensions of a tensor without changing the underlying data. The view method returns a new tensor with the same number of elements as the original tensor but with a different shape.

The view method for a Tensor object in PyTorch takes two arguments, r and c, to reshape the tensor into a matrix with r rows and c columns. The first argument specifies the desired number of rows and the second argument specifies the desired number of columns.

The view method takes in one or more integers as arguments, which specify the desired shape of the tensor. The arguments can be a single integer, in which case the tensor is reshaped into a 1−dimensional tensor with the specified length. Alternatively, the arguments can be a tuple of integers, in which case the tensor is reshaped into a tensor with the specified shape.

For example, suppose we have a tensor with shape (2, 3, 4), which represents a 3−dimensional tensor with 2 matrices, each with 3 rows and 4 columns. We can reshape this tensor into a 2−dimensional tensor with shape (2, 12) using the view method as follows:

### Example

import torch # create a tensor with shape (2, 3, 4) x = torch.randn(2, 3, 4) # reshape the tensor into a 2-dimensional tensor with shape (2, 12) y = x.view(2, 12) print(x.shape) # prints (2, 3, 4) print(y.shape) # prints (2, 12)

To run the above code we need to run the command shown below.

**Command**

python3 main.py

Once we run the above command, we can expect the output to the same as the one shown below.

### Output

torch.Size([2, 3, 4]) torch.Size([2, 12])

Note that the number of elements in the original tensor must be the same as the number of elements in the reshaped tensor. In other words, the product of the dimensions of the original tensor must be equal to the product of the dimensions of the reshaped tensor.

Furthermore, the view method returns a new tensor that shares the same underlying data as the original tensor. This means that if the original tensor is modified, the reshaped tensor will also be affected. However, if the reshaped tensor is modified, the original tensor will not be affected.

Now let's create a tensor and have different variations of columns and rows with the help of the view module.

Consider the code shown below.

### Example

# import torch module import torch # create a 1D tensor with Float data type that holds 6 elements data1 = torch.FloatTensor([23, 45, 54, 32, 23, 78]) # display the original tensor print("Original Tensor: ", data1) # reshape the tensor into a 3x2 matrix and display print("Reshaped Tensor: ", data1.view(3, 2))

### Explanation

The code uses the PyTorch library to create a 1−dimensional tensor data1 with 6 floating−point values. The print() function is then used to display the original tensor.

Next, the view() method is used to reshape the tensor into a 3x2 matrix, i.e., a matrix with 3 rows and 2 columns. The resulting reshaped tensor is then displayed using another print() statement.

In summary, the code demonstrates how to create and manipulate tensors in PyTorch using the view() method to reshape a tensor.

To run the above code we need to run the command shown below.

**Command**

python3 main.py

Once we run the above command, we can expect the output to the same as the one shown below.

### Output

Original Tensor: tensor([23., 45., 54., 32., 23., 78.]) Reshaped Tensor: tensor([[23., 45.], [54., 32.], [23., 78.]])

The tensor has been reshaped to have 3 rows and 2 columns.

Now let's change the view of the tensor to be of 2 rows and 3 columns.

Consider the code shown below.

### Example

# import torch module import torch # create a 1D tensor with Float data type that holds 6 elements data1 = torch.FloatTensor([23, 45, 54, 32, 23, 78]) # display the original tensor print("Original Tensor: ", data1) # reshape the tensor into a 2x3 matrix and display print("Reshaped Tensor: ", data1.view(2, 3))

### Explanation

This code imports the PyTorch library and creates a 1−dimensional tensor data1 with 6 floating−point values. The print() function is then used to display the original tensor.

The view() method is then used to reshape the tensor into a 2x3 matrix, i.e., a matrix with 2 rows and 3 columns. The resulting reshaped tensor is then displayed using another print() statement.

To run the above code we need to run the command shown below.

**Command**

python3 main.py

Once we run the above command, we can expect the output to the same as the one shown below.

### Output

Original Tensor: tensor([23., 45., 54., 32., 23., 78.]) Reshaped Tensor: tensor([[23., 45., 54.], [32., 23., 78.]])

In both the above examples, we changed the view of tensor using the view method, we can also change the data type as well if we want.

Consider the code shown below.

### Example

# import torch module import torch # create a 1D tensor with Float data type that holds 6 elements data1 = torch.FloatTensor([15.5, 18.7, 21.3, 26.1, 30.2, 35.9]) # display the data type of the original tensor print("Original Tensor data type: ", data1.dtype) # change the data type of data1 to int8 and display the new data type print("Converting to int8: ", data1.view(torch.int8).dtype) # change the data type of data1 to int16 and display the new data type print("Converting to int16: ", data1.view(torch.int16).dtype) # change the data type of data1 to int32 and display the new data type print("Converting to int32: ", data1.view(torch.int32).dtype) # change the data type of data1 to int64 and display the new data type print("Converting to int64: ", data1.view(torch.int64).dtype)

### Explanation

The code creates a 1−dimensional tensor named data1 with FloatTensor data type, which holds 6 elements: 23, 45, 54, 32, 23, and 78. The print() function is used to display the original tensor.

Next, the view() method is applied to data1 to change its shape into a 3x2 matrix. The view() method takes two arguments that specify the desired shape of the tensor. In this case, the tensor is reshaped into 3 rows and 2 columns. The reshaped tensor is then printed.

This code demonstrates how to use the view() method to change the shape of a tensor in PyTorch. By changing the shape of a tensor, we can manipulate how the underlying data is organized and accessed, which can be useful in a variety of applications.

To run the above code we need to run the command shown below.

**Command**

python3 main.py

Once we run the above command, we can expect the output to the same as the one shown below.

### Output

Original Tensor data type: torch.float32 Converting to int8: torch.int8 Converting to int16: torch.int16 Converting to int32: torch.int32 Converting to int64: torch.int64

## Conclusion

In conclusion, PyTorch is a powerful open−source framework for machine learning and deep learning tasks. It provides a wide range of tools and functions to help users manipulate and analyze data, including the ability to create and modify tensors. Tensors are multidimensional arrays that can hold a variety of data types, and they are a fundamental building block of PyTorch.

One important function of tensors is the ability to reshape them, which can be accomplished using the view() method. This method allows users to change the shape of a tensor without modifying its underlying data, making it a flexible and efficient tool for data manipulation. By using the view() method, users can easily change the shape of a tensor to fit their specific needs, whether that involves converting a 1D tensor to a 2D tensor or rearranging the dimensions of a tensor.

Overall, the ability to change the view of a tensor in PyTorch is a valuable tool for machine learning and deep learning applications, allowing users to efficiently manipulate and analyze data. With PyTorch, users have access to a powerful and flexible framework for building and training machine learning models, and the ability to reshape tensors is just one of the many tools available to help them do so.