-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
59 lines (45 loc) · 1.7 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import torch
from torch.utils.data import Dataset
class SequenceDataset(Dataset):
def __init__(self, data_path):
self.data = self.read_data_file(data_path)
@staticmethod
def read_data_file(data_path):
data = []
with open(data_path, 'r') as f:
for line in f:
temp_list = list(map(int, line.strip().split(' ')))
data.append(temp_list)
return data
def __getitem__(self, index):
label = self.data[index]
data = [0]
data.extend(label[:-1])
return torch.Tensor(data).long(), torch.Tensor(label).long()
def __len__(self):
return len(self.data)
class ClassDataset(Dataset):
def __init__(self, real_path, fake_path):
real_data = self.read_data_file(real_path)
fake_data = self.read_data_file(fake_path)
self.data = real_data + fake_data
self.target = [1 for _ in range(len(real_data))] + [0 for _ in range(len(fake_data))]
@staticmethod
def read_data_file(data_path):
data = []
with open(data_path, 'r') as f:
for line in f:
temp_list = list(map(int, line.strip().split(' ')))
data.append(temp_list)
return data
def __getitem__(self, index):
return torch.Tensor(self.data[index]).long(), torch.Tensor([self.target[index]]).long()
def __len__(self):
return len(self.data)
if __name__ == '__main__':
from torch.utils.data import DataLoader
data = ClassDataset('./real_data.txt', './fake_data.txt')
trainloader = DataLoader(dataset=data, batch_size=2, shuffle=False)
for index, (x, y) in enumerate(trainloader):
print(index)
print(x)