Complete the code to import the PyTorch data loader module.
from torch.utils.data import [1]
The DataLoader class is used to load data in batches for training in PyTorch.
Complete the code to define a custom dataset class inheriting from PyTorch's Dataset.
class CustomDataset([1]): def __init__(self, data): self.data = data def __len__(self): return len(self.data) def __getitem__(self, idx): return self.data[idx]
The custom dataset must inherit from Dataset to work with PyTorch data loaders.
Fix the error in the data loading loop to get batches from the data loader.
for [1] in data_loader: inputs, labels = batch print(inputs.shape, labels.shape)
The loop variable should be batch to unpack inputs and labels correctly.
Fill both blanks to create a data loader with batch size 32 and shuffling enabled.
data_loader = DataLoader(dataset, batch_size=[1], shuffle=[2])
Batch size should be 32 and shuffling should be True to randomize data order.
Fill all three blanks to transform raw data into tensors and normalize it in the custom dataset.
import torch from torchvision import transforms class CustomDataset(Dataset): def __init__(self, data): self.transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[1], std=[2]) ]) self.data = data def __getitem__(self, idx): sample = self.data[idx] sample = self.transform(sample) return sample def __len__(self): return len(self.data)
The mean and std values are standard for normalizing images in PyTorch. Mean is [0.485, 0.456, 0.406] and std is [0.229, 0.224, 0.225]. The first blank is mean, second is std.