PyTorch uses a dynamic computation graph, also called define-by-run. Why is this feature especially helpful for researchers?
Think about how easy it is to try new ideas if you can change the model on the fly.
Dynamic graphs let you change the model during execution, so researchers can quickly test new ideas without rebuilding the whole graph.
PyTorch offers tools like TorchScript and ONNX export. How do these help in production?
Think about how to make a model run fast and on different devices after training.
TorchScript and ONNX let you save models in formats that run efficiently and can be used outside Python, which is great for production.
You train a PyTorch model to classify images into categories. Which metric helps best to understand model performance during training?
Think about a simple way to measure how many predictions are right.
Accuracy directly measures the fraction of correct predictions, making it suitable for classification tasks.
Consider this PyTorch code snippet:
for data, target in dataloader:
optimizer.zero_grad()
output = model(data)
loss = loss_fn(output, target)
loss.backward(retain_graph=True)
optimizer.step()Why might this cause a RuntimeError about graph retention?
retain_graph=True is only needed if you call backward multiple times on the same graph.
retain_graph=True keeps the computation graph in memory after backward, which is unnecessary here and can cause errors.
You want to build a PyTorch model to predict the next word in a sentence, capturing long-term context. Which architecture is best?
Think about which model remembers information over many steps.
LSTM networks are designed to capture long-term dependencies in sequences better than simple RNNs or CNNs.