# Top 30 PyTorch Interview Questions and Answers

**Q1: What is PyTorch?**
**Answer: **PyTorch is a machine learning library for the programming language Python, based on Torch library, used for application such as natural language processing. It is free and open source software release under one of the BSD licenses. It has been released in October 2016 written in Python, C++, and CUDA.
**Q2: Who is the founder of PyTorch?**
**Answer:** Soumith Chintala and Hugh parkins (AI researcher in Facebook) are the founders of PyTorch.
**Q3: What are the significant features of PyTorch?**
**Answer: **The features of PyTorch are as follows:

**Easy interface**: PyTorch offers easy to use API, and it is straightforward to operate and run on Python. The code execution is smooth.**Python usage:**This library is considered to be Pythonic, which smoothly integrates the Python data science stack.**Computational Graphs:**PyTorch provides an excellent platform which offers dynamic computational graphs. So that a user can change them during runtime, this is more useful when a developer has no idea of how much memory is required for creating a neural network model.**Imperative Programming:**PyTorch performs computations through each line of the written code. This is similar to Python program execution.

**Q4: What are the three levels of abstraction?**

**Answer:**Levels of abstraction are as follows:

- Tensor- Imperative n-dimensional Array which runs on GPU.
- Variable- Node in the computational graph. This stores data and gradient.
- Module- Neural network layer will store state otherwise learnable weights.

**Q5: What are the advantages of PyTorch?**

**Answer:**The following are the advantages of PyTorch:

- It is easy to debug.
- It includes many layers as Torch.
- It can be considered as NumPy extension to GPU (Graphical Processing Units).
- It allows building network whose structure is dependent on computation itself.
- It includes a lot of loss functions.

**Q6: What are the difference between Tensorflow and PyTorch?**

**Answer:**

PyTorch |
TensorFlow |

Pytorch is closely related to the Lua-based Torch framework, which is used on Facebook. | TensorFlow is developed by Google and actively used at Google. |

Pytorch is new compared to other competitive Technologies. | TensorFlow is not new and is a to-go tool by many researchers and industry professionals. |

Pytorch includes everything imperatively and dynamically. | TensorFlow has static and dynamic graphs as a combination. |

PyTorch includes Computation graph during runtime. | TensorFlow does not have any runtime option. |

PyTorch includes deployment highlight for mobile and embedded frameworks. | TensorFlow works better for embedded frameworks. |

**Q7: What is Artificial Intelligence?**

**Answer:**Artificial intelligence is an excellent area of computer science that highlighted the creation of intelligent machines that work and reacts like humans. It can perform the task typically requiring human knowledge, such as visual perception, speech recognition, decision-making, etc.

**Q8: What is Machine learning?**

**Answer:**Machine learning is an application of artificial intelligence (AI) that provides that systems automatically learn and improve from experience without being programmed. Machine learning points on the development of computer programs that can access data and use its trend for themselves.

**Q9: What is Deep learning?**

**Answer:**Deep learning is a critical subset of machine learning that imitates the working of the human brain in processing data and creating patterns for use in decision making. Deep learning has network learning capability of learning unsupervised from data that is unstructured or unlabeled. It uses the concept of neural networks to solve complex problems.

**Q10: What are the difference between AI, Machine learning, and Deep learning?**

**Answer:**

Artificial Intelligence |
Machine learning |
Deep Learning |

Artificial intelligence originated around 1950s. | Machine learning originated around 1960s. | Deep learning originated around 1970s. |

AI represents simulated intelligence in machines. | Machine learning is the practice of getting machines to make decisions without being programmed. | Deep learning is the process of using Artificial Neural Networks to solve complex problems. |

AI is a subset of Data Science. | Machine learning is a subset of AI & Data Science. | Deep learning is a subfield of machine learning. AI & Data Science. |

The aim is to build machines which are capable of thinking like humans. | The aim is to make machines learn through data so that they can solve problems. | The aim is to build neural networks that automatically discover patterns for feature detection. |

**Q11: Explain the following three variants of gradient descent: batch, stochastic, and mini-batch?**

**Answer:**

**Stochastic Gradient Descent**: Here, we use only a single training example for calculation of gradient and parameters.**Batch Gradient Descent**: We calculate the gradient for the whole dataset and perform the update at each iteration.**Mini-batch Gradient Descent:**It's a variant of Stochastic Gradient Descent and here instead of single training example, mini-batch of samples is used.

**Q12: What is Backpropagation?**

**Answer:**Backpropagation is a training algorithm used for multiple users for a many layer neural network. In this method, we move the error into the end of the net to all weights inside the system and allowing efficient calculation of the gradient.

**It is divided into several steps as follows:-**

- Forward propagation of training data to generate output.
- Then by using target value and output value error derivative can be computed concerning output activation.
- Then we back produce for computing derivative of the error concerning output activation on previous and continue this for all the hidden layers.
- Using previously solved derivatives for output and all the hidden layers, we calculate error derivatives.
- And then we update the weights.

**Q13: What are the benefits of mini-batch gradient descent?**

**Answer:**The benefits of mini-batch gradient descent are as follow:

- It is more efficient compared to stochastic gradient descent.
- The generalization is maintained by discovering the flat minima.
- Mini-batches allow help to approximate the gradient of the entire training set, which helps us to avoid local minima.

**Q14: What is auto-encoder?**

**Answer:**An auto-encoder is a self-government machine learning algorithm that uses the backpropagation principle, where the target values are equal to the inputs provided. Internally, it has a hidden layer that manages a code used to represent the input.

**Q15: What is gradient descent?**

**Answer:**Gradient descent is an optimization algorithm, which is used to learn the value of parameters that controls the cost function. It is a repetitive algorithm which moves in the direction of vertical descent as defined by the negative of the gradient.

**Q16: What are the essential elements of Pytorch?**

**Answer:**Main elements of PyTorch are as follows:

- PyTorch tensors
- PyTorch NumPy
- Mathematical operations
- Autograd Module
- Optim Module
- nn Module

**Q17: What are tensors in PyTorch?**

**Answer:**A Tensor is a multi-dimensional matrix containing elements of a single data type. Tensors in PyTorch are same as NumPy array. Its manually compute the forward pass, loss, and backward pass. The most significant difference between the PyTorch Tensors and NumPy Array is that Pytorch can run either in CPU or GPU. To run operations on GPU, just cast the sensor in the file system.

# import PyTorch import torch # define a tensor torch.floatTensor([2]) 2 Torch. float tensor of size 1

**Q18: What are the mathematical building blocks of Neural Networks?**

**Answer**: Mathematics is a vital role in any machine learning algorithm and includes many core concepts of mathematics to get the right algorithm.

**The essential elements of machine learning and data science are as follows:**

**Vectors:**Vector is considered to be an array of numbers which is continuous or discrete, and space which consists of vectors is called a vector space.

**Scalers:**Scalers are termed to have zero dimensions containing only one value. When it comes to PyTorch, it does not include a particular tensor with zero dimensions.

**Matrices:**In matrices, most of the structured data is usually represented in the form of tables or a specific model.

**Q19: What is Autograd module in PyTorch?**

**Answer:**There is an automatic differentiation technique used in PyTorch. This technique is more powerful when we are building a neural network. There is a recorder which records what operations we have performed, and then it replays it backs to compute our gradient.

**Q20: What is the Optim Module in PyTorch?**

**Answer:**Torch.optim is a module that implements various optimization algorithm used for building neural networks. Most of the commonly used syntax is already supported. Below is the code of Adam optimizer

Optimizer = torch.optim.Adam(mode1, parameters( ), lr=learning rate

**Q21: What is nn Module in PyTorch?**

**Answer:**

**nn module:**The nn package define a set of modules, which are thought of as a neural network layer that produce output from the input and have some trainable weights. It is a type of tensor that considers a module parameter. Parameters are tensors subclasses. A fully connected ReLU networks where one hidden layer, trained to predict y from x to minimizing the square distance.

**Example:**

Import torch # define mode1 model= torch.nn.Sequential( torch.nn.Linear(hidden_num_units, hidden_num_units), torch.nn.ReLU( ), torch.nn.Linear(hidden_num_units, output_num_units), ) loss_fn= torch.nn.crossEntropyLoss( )

**Q22: What are the advantages of PyTorch?**

**Answer: Advantages of PyTorch are:**

- It is easy to debug.
- It includes many layers as Torch.
- It includes many layers as Torch.
- It allows building networks whose network is dependent upon computation itself.
- It has a simple interface and easily usable API.

**Q23: Write the installation steps of PyTorch?**

**Answer:**

**Installing PyTorch with Anaconda and Conda**

- Download Anaconda and install (Go with the latest Python version).
- Go to the Getting Started section on the PyTorch website through
.*pytorch.org* - Generate the appropriate configuration options for your particular environment. For example:

- OS: Windows
- Package Manager: condo
- Python: 3.6
- CUDA: 9.0

- Run the below command in the terminal(CMD) to install PyTorch.

> conda install PyTorch -c PyTorch > pip3 install torchvision

**Q24: How to build a neural network through PyTorch?**

**Answer:**A PyTorch implementation of the neural network looks the same as a NumPy implementation. The motive of this section is to showcase the similar nature of PyTorch and NumPy. For example: create a three-layered network having five nodes in the input layer, three in the hidden layer, and one in the output layer.

Import torch n_input, n_hidden, n_output= 3, 4, 1

**Q25: Why use PyTorch for Deep learning?**

**Answer:**PyTorch is the essential part of deep learning tool. Deep learning is a subset of machine learning, which algorithm works on the human brain. These algorithms are known as artificial neural networks. That neural network is used for image classification, Artificial Neural Networks, and Recurrent Neural Networks. Unlike other libraries like TensorFlow where you have first to define an entire computation graph before you can run your model.

**Q26: What are the differences between an array and a linked-list?**

**Answer:**Collection of the object in a well-defined order is known as Array. And the linked list is also a set of objects but they are not in a well-defined form or remain in sequence, and also they have a pointer which is not in case of Array.

**Q27: Name the two standard regularizations in machine learning approach?**

**Answer:**Two common regularizations are L1 and L2. Both have their distinct functions. L1 contains many variables which are in binary numbers. L2 are meant for error handling, and both of them are related to the Gaussian concept.

**Q28: What do you mean by Supervised and Unsupervised learning?**

**Answer:**Supervised learning is a type of education where we teach or trained the machine using data which is well labeled, and some information is already tagged with the correct answer. So that supervised learning algorithm analyses the training data. Unsupervised learning is an essential type of machine learning where there is neither classified nor labeled and allow to act on that information without guidance.

**Q28: what is the difference between Type I and Type II error?**

**Answer:**Type I error is the false positive value. And Type 1 error is a false negative value. Type I error represent something is happening when. Type II errors are describing that there nothing is wrong where something is not right.

**Q29: What do you know about Recall and precision?**

**Answer:**Recall is known as an exact positive rate. Precision is generally a predictive value, which is positive.