Download code/ampd/data/dataset.py from fryabcfry/cs4486-amp-pd-checkpoints: direct link, hf CLI and curl.
- Browser
- Download file 1.88 kB
-
https://huggingface.co/fryabcfry/cs4486-amp-pd-checkpoints/resolve/main/code/ampd/data/dataset.py
- Command line
-
hf download hf://fryabcfry/cs4486-amp-pd-checkpoints/code/ampd/data/dataset.py
-
curl -L -o dataset.py https://huggingface.co/fryabcfry/cs4486-amp-pd-checkpoints/resolve/main/code/ampd/data/dataset.py
1.88 kB
| """Torch dataset and dataloader helpers.""" | |
| from __future__ import annotations | |
| from typing import List, Sequence | |
| import numpy as np | |
| import pandas as pd | |
| import torch | |
| from torch.utils.data import DataLoader, Dataset | |
| class AMPDataset(Dataset): | |
| def __init__( | |
| self, | |
| X: np.ndarray, | |
| samples: pd.DataFrame, | |
| target_cols: Sequence[str], | |
| mask_cols: Sequence[str], | |
| n_classes: int | None = None, | |
| ): | |
| self.X = np.array(X, dtype=np.float32, copy=True) | |
| self.samples = samples.reset_index(drop=True) | |
| self.target_cols = list(target_cols) | |
| self.mask_cols = list(mask_cols) | |
| self.n_classes = n_classes | |
| Y = np.array(self.samples[self.target_cols].to_numpy(dtype=np.float32), copy=True) | |
| M = np.array(self.samples[self.mask_cols].to_numpy(dtype=np.float32), copy=True) | |
| Y = np.nan_to_num(Y, nan=0.0) | |
| if n_classes is not None: | |
| Y = np.clip(Y, 0, n_classes - 1) | |
| self.Y = Y | |
| self.M = M | |
| def __len__(self) -> int: | |
| return len(self.samples) | |
| def __getitem__(self, idx: int): | |
| return { | |
| "x": torch.from_numpy(self.X[idx]), | |
| "y": torch.from_numpy(self.Y[idx]), | |
| "mask": torch.from_numpy(self.M[idx]), | |
| "idx": torch.tensor(idx, dtype=torch.long), | |
| } | |
| def make_loader( | |
| X: np.ndarray, | |
| samples: pd.DataFrame, | |
| target_cols: Sequence[str], | |
| mask_cols: Sequence[str], | |
| batch_size: int = 64, | |
| shuffle: bool = False, | |
| num_workers: int = 0, | |
| n_classes: int | None = None, | |
| ) -> DataLoader: | |
| ds = AMPDataset(X, samples, target_cols, mask_cols, n_classes=n_classes) | |
| return DataLoader( | |
| ds, | |
| batch_size=batch_size, | |
| shuffle=shuffle, | |
| num_workers=num_workers, | |
| pin_memory=torch.cuda.is_available(), | |
| drop_last=False, | |
| ) | |