1. Implement the classification class, the LinearClassifier class and the MLPClassifier class in models.py import torch import...
Question:
1. Implement the classification class, the LinearClassifier class and the MLPClassifier class in models.py
import torch import torch.nn.functional as F
class ClassificationLoss(torch.nn.Module): def forward(self, input, target): """ Your code here
Compute mean(-log(softmax(input)_label))
@input: torch.Tensor((B,C)) @target: torch.Tensor((B,), dtype=torch.int64)
@return: torch.Tensor((,))
Hint: Don't be too fancy, this is a one-liner """ raise NotImplementedError('ClassificationLoss.forward')
class LinearClassifier(torch.nn.Module): def __init__(self): super().__init__()
""" Your code here """ raise NotImplementedError('LinearClassifier.__init__')
def forward(self, x): """ Your code here
@x: torch.Tensor((B,3,64,64)) @return: torch.Tensor((B,6)) """ raise NotImplementedError('LinearClassifier.forward')
class MLPClassifier(torch.nn.Module): def __init__(self): super().__init__()
""" Your code here """ raise NotImplementedError('MLPClassifier.__init__')
def forward(self, x): """ Your code here
@x: torch.Tensor((B,3,64,64)) @return: torch.Tensor((B,6)) """ raise NotImplementedError('MLPClassifier.forward')
2. Implement the __init__, __len__, and __getitem__ methods of the SuperTuxDataset class in utils.py. The __len__ method should return the size of the dataset. The __getitem__ method should return a tuple of images and labels. The image should be a torch. Tensor of size (3, 64, 64) with a range of [0, 1], and the label should be an integer. Ensure that the labels correspond to the classes: background (0), kart (1), pickup (2), nitro (3), bomb (4), and projectile (5). Use the information from the labels.csv file, where the headers are the file and label.
class SuperTuxDataset(Dataset): def __init__(self, dataset_path): """ Your code here Hint: Use the python csv library to parse labels.csv
WARNING: Do not perform data normalization here. """ raise NotImplementedError('SuperTuxDataset.__init__')
def __len__(self): """ Your code here """ raise NotImplementedError('SuperTuxDataset.__len__')
def __getitem__(self, idx): """ Your code here return a tuple: img, label """ raise NotImplementedError('SuperTuxDataset.__getitem__')
3. Implement the MLPClassifier class in models.py. The inputs and outputs to the multi-layer perceptron are the same as the linear classifier. However, now you're learning a non-linear function.
from .models import ClassificationLoss, model_factory, save_model from .utils import accuracy, load_data
def train(args): model = model_factory[args.model]()
""" Your code here
""" raise NotImplementedError('train')
save_model(model)
Data Structures and Algorithms in Python
ISBN: 978-1118290279
1st edition
Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser