Skip to content

Commit 10c6091

Browse files
authored
Fix/add mydataset class (#487)
* fix training name in code_structure docs * add missing MyDataset class to data_solution.py template tests expect to import MyDataset from data.py, but the solution only had functions. this caused uvx invoke test to fail.
1 parent 6bc1f40 commit 10c6091

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

s2_organisation_and_version_control/exercise_files/data_solution.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
1+
from pathlib import Path
2+
13
import torch
24
import typer
5+
from torch.utils.data import Dataset
6+
7+
8+
class MyDataset(Dataset):
9+
"""Custom dataset for corrupt MNIST."""
10+
11+
def __init__(self, data_dir: str | Path, train: bool = True) -> None:
12+
super().__init__()
13+
self.data_dir = Path(data_dir)
14+
if train:
15+
imgs, targets = [], []
16+
for i in range(6):
17+
imgs.append(torch.load(self.data_dir / f"train_images_{i}.pt"))
18+
targets.append(torch.load(self.data_dir / f"train_target_{i}.pt"))
19+
self.images = torch.cat(imgs).unsqueeze(1).float()
20+
self.targets = torch.cat(targets).long()
21+
else:
22+
self.images = torch.load(self.data_dir / "test_images.pt").unsqueeze(1).float()
23+
self.targets = torch.load(self.data_dir / "test_target.pt").long()
24+
self.images = (self.images - self.images.mean()) / self.images.std()
25+
26+
def __len__(self) -> int:
27+
"""Return the length of the dataset."""
28+
return len(self.images)
29+
30+
def __getitem__(self, idx: int):
31+
"""Return a given sample from the dataset."""
32+
return self.images[idx], self.targets[idx]
333

434

535
def normalize(images: torch.Tensor) -> torch.Tensor:

0 commit comments

Comments
 (0)