-
Notifications
You must be signed in to change notification settings - Fork 15
/
create_dataset.py
41 lines (31 loc) · 1.09 KB
/
create_dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Create 2D points dataset for visualization.
Created by: Xu Ma (Email: ma.xu1@northeastern.edu)
Modified Date: Mar/29/2024
"""
from sklearn.datasets import make_moons
import torch
from torch.utils.data import Dataset
class MoonDataset(Dataset):
def __init__(self, n_samples=100, noise=0.2, random_state=0):
dataset = make_moons(n_samples=n_samples, noise=noise, random_state=random_state)
self.data = dataset[0]
self.labels = dataset[1]
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
x = torch.Tensor(self.data[idx])
y = torch.LongTensor([self.labels[idx]])
return x, y
class ValDataset(Dataset):
def __init__(self):
range_w = torch.arange(-2, 3, step=0.02)
range_h = torch.arange(-2, 2.5, step=0.02)
data = torch.stack(torch.meshgrid(range_w, range_h, indexing='ij'), dim=-1).float()
data = data.reshape(-1, 2)
self.data = data
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
x = torch.Tensor(self.data[idx])
return x