×

Multiple-Choice Questions

Web Technologies MCQs

Computer Science Subjects MCQs

Databases MCQs

Programming MCQs

Testing Software MCQs

Digital Marketing Subjects MCQs

Cloud Computing Softwares MCQs

AI/ML Subjects MCQs

Engineering Subjects MCQs

Office Related Programs MCQs

Management MCQs

More

PyTorch MCQs (Multiple-Choice Questions)

PyTorch is an open-source machine learning framework used for building and training deep learning models. It provides tensors, automatic differentiation, neural network modules, optimizers, data-loading utilities, and support for CPU and GPU computation.

PyTorch MCQs

These PyTorch MCQs cover important concepts such as tensors, autograd, neural networks, datasets, DataLoader, optimizers, loss functions, GPU acceleration, model training, and model evaluation.

List of PyTorch MCQs

The following PyTorch multiple-choice questions are designed to test your knowledge of the PyTorch framework and its commonly used features and APIs.

1. What is PyTorch?

  1. A deep learning and machine learning framework
  2. A database management system
  3. A web development framework
  4. A programming language

Answer: A) A deep learning and machine learning framework

Explanation:

PyTorch is an open-source machine learning framework widely used for tensor computation, automatic differentiation, and building neural networks.

2. What is a tensor in PyTorch?

  1. A multidimensional array used for numerical computation
  2. A Python package manager
  3. A neural network architecture
  4. A loss function

Answer: A) A multidimensional array used for numerical computation

Explanation:

A PyTorch tensor is a multidimensional data structure used to store and manipulate numerical data and can be operated on by CPUs or supported accelerators.

3. Which module is commonly imported to use PyTorch?

  1. import pytorch
  2. import torch
  3. import py_torch
  4. import deep_torch

Answer: B) import torch

Explanation:

The main PyTorch Python package is imported using import torch.

4. Which function creates a tensor from existing data?

  1. torch.tensor()
  2. torch.create()
  3. torch.array()
  4. torch.data()

Answer: A) torch.tensor()

Explanation:

The torch.tensor() function creates a tensor from data such as a list or another compatible data object.

5. Which function creates a tensor filled with zeros?

  1. torch.empty()
  2. torch.zeros()
  3. torch.null()
  4. torch.zero_tensor()

Answer: B) torch.zeros()

Explanation:

The torch.zeros() function creates a tensor whose elements are initialized to zero.

6. Which function creates a tensor filled with ones?

  1. torch.ones()
  2. torch.fullones()
  3. torch.one()
  4. torch.unit()

Answer: A) torch.ones()

Explanation:

The torch.ones() function creates a tensor with all elements initialized to one.

7. Which function creates a tensor containing random values sampled from a standard normal distribution?

  1. torch.rand()
  2. torch.randn()
  3. torch.random_normal()
  4. torch.normal_random()

Answer: B) torch.randn()

Explanation:

The torch.randn() function generates random values from a standard normal distribution.

8. Which function creates random values from a uniform distribution over the interval [0, 1)?

  1. torch.randn()
  2. torch.rand()
  3. torch.uniform_random()
  4. torch.random()

Answer: B) torch.rand()

Explanation:

The torch.rand() function generates random values from a uniform distribution over the interval from 0 inclusive to 1 exclusive.

9. What does the shape attribute of a tensor provide?

  1. The tensor's dimensions and their sizes
  2. The tensor's memory address
  3. The tensor's Python class only
  4. The tensor's gradient value

Answer: A) The tensor's dimensions and their sizes

Explanation:

The shape property describes the size of each dimension of a tensor.

10. Which property returns the data type of a PyTorch tensor?

  1. type
  2. dtype
  3. data_type
  4. tensor_type

Answer: B) dtype

Explanation:

The dtype property specifies the data type of the elements stored in a tensor.

11. Which function converts a NumPy array into a PyTorch tensor while sharing memory when possible?

  1. torch.from_numpy()
  2. torch.numpy_tensor()
  3. torch.to_numpy_tensor()
  4. torch.array_from_numpy()

Answer: A) torch.from_numpy()

Explanation:

torch.from_numpy() creates a tensor from a NumPy array and, under supported conditions, the tensor and array share the same underlying memory.

12. What is autograd in PyTorch?

  1. An automatic differentiation system
  2. A GPU installation utility
  3. A data-loading framework
  4. A model visualization tool

Answer: A) An automatic differentiation system

Explanation:

PyTorch autograd automatically records operations on tensors that require gradients and uses the recorded computation graph to calculate derivatives.

13. What does requires_grad=True indicate for a tensor?

  1. Gradient computation should be tracked for the tensor
  2. The tensor must be stored on a GPU
  3. The tensor must contain integers
  4. The tensor cannot be modified

Answer: A) Gradient computation should be tracked for the tensor

Explanation:

Setting requires_grad=True tells autograd to track operations involving the tensor for gradient computation.

14. Which method is commonly used to compute gradients through a computation graph?

  1. forward()
  2. backward()
  3. gradient()
  4. calculate()

Answer: B) backward()

Explanation:

The backward() method performs backpropagation through the computation graph and computes gradients for relevant leaf tensors.

15. What does tensor.grad generally contain after backward()?

  1. The tensor's gradient when gradients have been accumulated
  2. The tensor's original value
  3. The tensor's shape
  4. The tensor's device name only

Answer: A) The tensor's gradient when gradients have been accumulated

Explanation:

For an appropriate leaf tensor requiring gradients, grad contains the gradient accumulated by autograd after backward computation.

16. Which method detaches a tensor from the current computation graph?

  1. detach()
  2. remove_graph()
  3. disconnect()
  4. detach_graph()

Answer: A) detach()

Explanation:

The detach() method returns a new tensor detached from the computation graph that created it.

17. What is the purpose of torch.no_grad()?

  1. To disable gradient tracking within a context
  2. To disable the CPU
  3. To delete model parameters
  4. To disable tensor operations

Answer: A) To disable gradient tracking within a context

Explanation:

torch.no_grad() is a context manager that disables gradient calculation for operations performed within the context.

18. Which module provides building blocks for constructing neural networks?

  1. torch.nn
  2. torch.network
  3. torch.layers
  4. torch.deep

Answer: A) torch.nn

Explanation:

The torch.nn module provides classes and functions for constructing neural network models, including layers and loss functions.

19. What is torch.nn.Module?

  1. The base class for neural network modules
  2. A tensor data type
  3. An optimizer
  4. A dataset format

Answer: A) The base class for neural network modules

Explanation:

torch.nn.Module is the base class used to define neural network modules and models in PyTorch.

20. Which method is typically overridden to define the computation performed by a custom PyTorch module?

  1. run()
  2. forward()
  3. compute()
  4. predict()

Answer: B) forward()

Explanation:

A custom nn.Module normally implements its forward computation in the forward() method.

21. What is the purpose of nn.Linear?

  1. To create a fully connected linear layer
  2. To create a convolutional layer
  3. To load training data
  4. To calculate model accuracy

Answer: A) To create a fully connected linear layer

Explanation:

nn.Linear applies a linear transformation to its input and is commonly used as a fully connected layer.

22. What is the purpose of nn.Sequential?

  1. To create a container that applies modules in sequence
  2. To load data sequentially from a database
  3. To execute optimizers sequentially
  4. To create sequential tensors only

Answer: A) To create a container that applies modules in sequence

Explanation:

nn.Sequential provides a container in which modules are applied in the order in which they are supplied.

23. Which activation function is commonly represented by nn.ReLU?

  1. Rectified Linear Unit
  2. Random Linear Unit
  3. Recursive Learning Unit
  4. Regularized Logistic Unit

Answer: A) Rectified Linear Unit

Explanation:

ReLU stands for Rectified Linear Unit and is commonly used to introduce non-linearity into neural networks.

24. Which layer is commonly used for two-dimensional image convolution?

  1. nn.Conv2d
  2. nn.Image2d
  3. nn.Dense2d
  4. nn.Filter2d

Answer: A) nn.Conv2d

Explanation:

nn.Conv2d applies a two-dimensional convolution over an input signal and is commonly used in convolutional neural networks for images.

25. Which PyTorch layer is commonly used to reduce spatial dimensions in convolutional neural networks?

  1. nn.MaxPool2d
  2. nn.LinearPool
  3. nn.Reduce2d
  4. nn.SpacePool

Answer: A) nn.MaxPool2d

Explanation:

nn.MaxPool2d performs two-dimensional max pooling and can reduce the spatial dimensions of feature maps.

26. Which class is commonly used to represent a dataset in PyTorch?

  1. torch.utils.data.Dataset
  2. torch.data.Database
  3. torch.dataset.Data
  4. torch.utils.DatasetLoader

Answer: A) torch.utils.data.Dataset

Explanation:

torch.utils.data.Dataset provides an interface for representing datasets and can be subclassed to implement custom datasets.

27. What is the primary purpose of DataLoader?

  1. To provide iterable access to a dataset with batching and other loading options
  2. To define neural network layers
  3. To calculate gradients
  4. To save model weights

Answer: A) To provide iterable access to a dataset with batching and other loading options

Explanation:

DataLoader wraps a dataset in an iterable and provides features such as batching, shuffling, and configurable data loading.

28. Which DataLoader argument is commonly used to specify the number of samples in each batch?

  1. batch_size
  2. batch_count
  3. sample_size
  4. group_size

Answer: A) batch_size

Explanation:

The batch_size argument determines how many samples are generally included in each batch produced by a DataLoader.

29. Which DataLoader argument can randomly reorder the data each epoch?

  1. randomize
  2. shuffle
  3. reorder
  4. mix

Answer: B) shuffle

Explanation:

Setting shuffle=True causes the data-loading process to sample the dataset in a shuffled order rather than maintaining the default sequential order.

30. What is the purpose of a loss function in model training?

  1. To measure the difference between model predictions and target values
  2. To load training data
  3. To create GPU memory
  4. To define the Python version

Answer: A) To measure the difference between model predictions and target values

Explanation:

A loss function measures the error or dissimilarity between model predictions and target values. The optimizer then uses gradients of the loss to update model parameters.

31. Which loss function is commonly used for regression problems?

  1. nn.MSELoss
  2. nn.CrossEntropyLoss
  3. nn.NLLLoss
  4. nn.SoftmaxLoss

Answer: A) nn.MSELoss

Explanation:

nn.MSELoss computes mean squared error and is commonly used for regression tasks.

32. Which loss function is commonly used for multiclass classification with class-index targets?

  1. nn.MSELoss
  2. nn.CrossEntropyLoss
  3. nn.L1Loss
  4. nn.SmoothL1Loss

Answer: B) nn.CrossEntropyLoss

Explanation:

nn.CrossEntropyLoss is commonly used for classification with multiple classes and expects unnormalized logits as input.

33. Which loss function is commonly associated with binary classification when using probabilities as input?

  1. nn.BCELoss
  2. nn.MSELoss
  3. nn.NLLLoss
  4. nn.L1Loss

Answer: A) nn.BCELoss

Explanation:

nn.BCELoss computes binary cross-entropy between target values and input probabilities. In many binary-classification models, BCEWithLogitsLoss is preferred when the model outputs logits directly.

34. Which module provides optimization algorithms such as SGD and Adam?

  1. torch.optim
  2. torch.optimize
  3. torch.training
  4. torch.gradient

Answer: A) torch.optim

Explanation:

The torch.optim module provides implementations of optimization algorithms used to update model parameters during training.

35. Which optimizer is an adaptive optimization algorithm commonly used in deep learning?

  1. Adam
  2. LinearSearchOnly
  3. SimpleGD
  4. RandomOptimizer

Answer: A) Adam

Explanation:

torch.optim.Adam is a widely used adaptive optimization algorithm that maintains running estimates used to update parameters.

36. What does optimizer.zero_grad() generally do?

  1. Resets accumulated gradients of optimized parameters
  2. Deletes the model
  3. Resets model weights to zero
  4. Stops the optimizer permanently

Answer: A) Resets accumulated gradients of optimized parameters

Explanation:

PyTorch gradients accumulate by default, so optimizer.zero_grad() is commonly called before a new backward pass to clear previously accumulated gradients.

37. What does optimizer.step() do?

  1. Updates parameters using the computed gradients
  2. Computes the model output
  3. Creates a new dataset
  4. Clears all tensors

Answer: A) Updates parameters using the computed gradients

Explanation:

After gradients have been computed during backpropagation, optimizer.step() updates the parameters according to the selected optimization algorithm.

38. Which method sets a model into training mode?

  1. model.train()
  2. model.training()
  3. model.start_train()
  4. model.enable_training()

Answer: A) model.train()

Explanation:

The train() method puts a module into training mode. This affects modules such as Dropout and BatchNorm that have training-specific behavior.

39. Which method sets a model into evaluation mode?

  1. model.test()
  2. model.eval()
  3. model.evaluate()
  4. model.testing()

Answer: B) model.eval()

Explanation:

The eval() method switches a module to evaluation mode, affecting layers such as Dropout and BatchNorm that behave differently during training and evaluation.

40. Which statement correctly describes model.eval() and torch.no_grad()?

  1. They are exactly the same mechanism
  2. eval() changes module behavior, while no_grad() disables gradient tracking
  3. eval() disables the GPU, while no_grad() enables it
  4. Both permanently delete gradients

Answer: B) eval() changes module behavior, while no_grad() disables gradient tracking

Explanation:

model.eval() changes the module's training/evaluation behavior, whereas torch.no_grad() controls gradient tracking. They serve different purposes.

41. Which function checks whether a CUDA-enabled GPU is available?

  1. torch.cuda.is_available()
  2. torch.gpu.exists()
  3. torch.cuda.check()
  4. torch.is_gpu()

Answer: A) torch.cuda.is_available()

Explanation:

torch.cuda.is_available() returns a Boolean indicating whether CUDA is available for use in the current PyTorch environment.

42. Which method can be used to move a tensor to a specified device?

  1. tensor.to()
  2. tensor.move()
  3. tensor.device()
  4. tensor.transfer()

Answer: A) tensor.to()

Explanation:

The to() method can return a tensor converted to a specified device or data type, depending on the arguments provided.

43. Which function concatenates tensors along an existing dimension?

  1. torch.stack()
  2. torch.cat()
  3. torch.join()
  4. torch.merge()

Answer: B) torch.cat()

Explanation:

torch.cat() concatenates a sequence of tensors along an existing dimension.

44. What is the difference between torch.cat() and torch.stack()?

  1. cat concatenates along an existing dimension, while stack creates a new dimension
  2. cat creates a new dimension, while stack removes a dimension
  3. Both always perform exactly the same operation
  4. Neither function works with tensors

Answer: A) cat concatenates along an existing dimension, while stack creates a new dimension

Explanation:

torch.cat() joins tensors along an existing dimension, whereas torch.stack() concatenates tensors along a newly created dimension.

45. What does torch.squeeze() generally do?

  1. Removes dimensions of size 1
  2. Removes all tensor values
  3. Combines multiple tensors
  4. Converts a tensor to a Python list

Answer: A) Removes dimensions of size 1

Explanation:

torch.squeeze() removes dimensions of size one from a tensor, either all such dimensions or a specified dimension.

46. What is the purpose of torch.save()?

  1. To serialize and save PyTorch objects
  2. To calculate gradients
  3. To create a neural network
  4. To move tensors to a GPU

Answer: A) To serialize and save PyTorch objects

Explanation:

torch.save() serializes and saves PyTorch objects. A common model-saving practice is to save a model's state_dict().

47. What is commonly stored in a PyTorch model's state_dict?

  1. Learned parameters and persistent buffers
  2. The entire Python interpreter
  3. Only the model's source code
  4. Only the training dataset

Answer: A) Learned parameters and persistent buffers

Explanation:

A module's state_dict contains the state needed to represent its parameters and persistent buffers. Saving it is a recommended way to save learned model parameters.

48. Which method loads parameters from a state dictionary into a PyTorch model?

  1. load_state_dict()
  2. load_parameters()
  3. restore_state()
  4. import_weights()

Answer: A) load_state_dict()

Explanation:

The load_state_dict() method loads parameter and buffer values from a state dictionary into the corresponding module.

49. Which technique is commonly used to reduce overfitting in neural networks?

  1. Dropout
  2. Increasing training error intentionally
  3. Removing all activation functions
  4. Disabling the loss function

Answer: A) Dropout

Explanation:

Dropout randomly disables units during training and is commonly used as a regularization technique to help reduce overfitting.

50. Which sequence correctly represents a common PyTorch training step?

  1. Compute prediction, calculate loss, call backward(), then call optimizer.step()
  2. Call optimizer.step(), calculate loss, then create the model
  3. Call eval(), delete gradients, then calculate the loss
  4. Save the model, call backward(), then load the dataset

Answer: A) Compute prediction, calculate loss, call backward(), then call optimizer.step()

Explanation:

A typical training iteration computes the model output, calculates the loss, clears accumulated gradients, performs backpropagation with backward(), and updates parameters with optimizer.step().

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2026 www.includehelp.com. All rights reserved.