Question 4
You are writing a PyTorch training script on a machine with a CUDA-enabled GPU. You have correctly identified the device and moved your model to it. However, when you run your script, it crashes on the forward pass with a RuntimeException, indicating that the model's weights and the input data are on different devices.
Review the following code snippet and determine the single line of code that must be added to the ---BLANK --- to fix this specific error.
import torchimport torch.nn as nn
# 1. Setup device and move the model to itdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model = nn.Linear(in_features=64, out_features=10).to(device)optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
# 2. Create a batch of input data (defaults to CPU)# This simulates data coming from a DataLoaderinput_batch = torch.randn(32, 64) # [batch_size, feature_size]
# 3. Perform a training stepoptimizer.zero_grad()
# --- BLANK ---
# The following line causes the error:# "RuntimeException: Expected all tensors to be on the same device,# but found at least two devices, cuda:0 and cpu!"outputs = model(input_batch)# ... loss calculation and backward pass would followWhat line of code should be placed in the --- BLANK --- to resolve this device mismatch?
model.cuda()
input_batch.cuda()
input_batch = input_batch.requires_grad_(True)
input_batch = input_batch.to(device)
torch.set_default_tensor_type('torch.cuda.FloatTensor')