PyTorch Basics

PyTorch Tensors: Creation, Manipulation, and Operations

The basic building block of PyTorch are the Tensors which are data structures similar to the NumPy Arrays. They are similar to the arrays and matrices that we can use to encode and decode inputs and outputs of a model as well as the model’s parameters. The main difference between the NumPy array and tensors is that tensors can run on tensors can run on GPUs or other hardware accelerators.

Tensor_name = torch.tensor([value 1 , value 2 , ….. Value n ])

Where the ‘torch.tensor() is the method to create the tensors.

The below code snippets show the creation of Tensors and their manipulation through the operations. In this example, we are creating tensor1 and tensor2 to store the data and perform the operations.

Python




import torch
 
# Create a tensor from a list
tensor1 = torch.tensor([1, 2, 3])
print("Tensor from list:", tensor1)
 
# Create a tensor of zeros with shape (2, 3)
tensor2 = torch.zeros(2, 3)
print("Tensor of zeros:", tensor2)
 
# Create a random tensor with shape (3, 2)
tensor3 = torch.rand(3, 2)
print("Random tensor:", tensor3)
 
# Performing operations on Tensors
 
# Addition
result_add = tensor1 + tensor2
print("Addition result:", result_add)
 
 
# Multiplication
result_mul = tensor2 * 5
print("Multiplication result:", result_mul)
 
 
# Matrix multiplication
result_matmul = torch.matmul(tensor2, tensor3)
print("Matrix multiplication result:", result_matmul)


Output:

Tensor from list: tensor([1, 2, 3])
Tensor of zeros: tensor([[0., 0., 0.],
        [0., 0., 0.]])
Random tensor: tensor([[0.9161, 0.3915],
        [0.7185, 0.7726],
        [0.4831, 0.0832]])
Addition result: tensor([[1., 2., 3.],
        [1., 2., 3.]])
Multiplication result: tensor([[0., 0., 0.],
        [0., 0., 0.]])
Matrix multiplication result: tensor([[0., 0.],
        [0., 0.]])

Start learning PyTorch for Beginners

Machine Learning helps us to extract meaningful insights from the data. But now, it is capable of mimicking the human brain. This is done using neural networks, which contain the various interconnected layers of nodes containing the data. This data is passed to forward layers. Subsequently, the model learns from the data and predicts output for the new data.

PyTorch helps us to create and train these neural networks that act like our brains and learn from the data.

Table of Content

  • What is Pytorch?
  • Why use PyTorch?
  • How to install Pytorch ?
  • PyTorch Basics
  • Autograd: Automatic Differentiation in PyTorch
  • Neural Networks in PyTorch
  • Working with Data in PyTorch
  • Intermediate Topics in PyTorch
  • Validation and Testing
  • Frequently Asked Questions

Similar Reads

What is Pytorch?

PyTorch is an open-source machine learning library for Python developed by Facebook’s AI Research Lab (FAIR). It is widely used for building deep learning models and conducting research in various fields like computer vision, natural language processing, and reinforcement learning. One of the key features of PyTorch is its dynamic computational graph, which allows for more flexible and intuitive model construction compared to static graph frameworks. PyTorch also offers seamless integration with other popular libraries like NumPy, making it easier to work with tensors and multidimensional arrays....

Why use PyTorch?

It supports tensor computation: Tensor is the data structure that is similar to the networks, array. It is an n-dimensional array that contains the data. We can perform arbitrary numeric computation on these arrays using the APIs. It provides Dynamic Graph Computation: This feature allows us to define the computational graphs dynamically during runtime. This makes it more flexible than the static computation graphs approach in which where the graph structure is fixed and defined before execution, It provides the Automatic Differentiation: The Autograd package automatically computes the gradients that are crucial for training the model using optimization algorithms. Thus, we can perform operations on tensors without manually calculating gradients. It has Support for Python: It has native support for the Python programming language. Thus, we can easily integrate with existing Python workflows and libraries. This is the reason why it is used by the machine learning and data science communities. It has its production environment: PyTorch has the TorchScript which is the high-performance environment for serializing and executing PyTorch models. You can easily compile PyTorch models into a portable intermediate representation (IR) format. Due to this, we can deploy the model on various platforms and devices without requiring the original Python code....

How to install Pytorch ?

To install PyTorch, you can use the pip package manager, which is the standard tool for installing Python packages. You can install PyTorch using the following command:...

PyTorch Basics

PyTorch Tensors: Creation, Manipulation, and Operations...

Autograd: Automatic Differentiation in PyTorch

...

Neural Networks in PyTorch

Now, we will shift our focus on Autograd which is one of the most important topics in the PyTorch basics. The Autograd Module of PyTorch provides the automatic calculation of the gradients. It means that we do not need to calculate the gradients explicitly. You might be thinking what gradient is. So, the gradient represents the rate of change of functions with respect to parameters. This helps us to identify the difference between the predicted outputs and actual labels....

Working with Data in PyTorch

...

Intermediate Topics in PyTorch

Basics of nn.Module and nn.Parameter...

Validation and Testing

...

Conclusion

...

Frequently Asked Questions

...

Contact Us