forked from BUPT-GAMMA/OpenHGNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meirec_dataset.py
60 lines (48 loc) · 1.63 KB
/
meirec_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
59
from pathlib import Path
import numpy as np
import torch
from torch.utils.data import DataLoader, Dataset
FOLD = Path(__file__).resolve().parent / "meirec"
class MeiRECDataset(Dataset):
def __init__(self, phase):
assert phase in ['train', 'test']
if phase == 'train':
self.data_path = FOLD / "train_data.txt"
else:
self.data_path = FOLD / "test_data.txt"
self.load_data(self.data_path)
def load_data(self, path):
self.features_list = []
self.label_list = []
with open(path, 'r', encoding='utf-8') as f:
for line in f.readlines():
feature, label = line.split()
self.features_list.append(feature)
self.label_list.append(float(label))
self.data_len = len(self.features_list)
# print(self.data_len)
def __getitem__(self, item):
index = item
feature_list = []
feature = self.features_list[index]
features_split = feature.split(',')
for item in features_split[:81]:
feature_list.append(float(item))
for item in features_split[81:]:
feature_list.append(int(item))
return {
'data': torch.from_numpy(np.array(feature_list)),
'labels': self.label_list[index]
}
def __len__(self):
return self.data_len
def get_data_loader(type, batch_size=64, num_workers=0):
dataset = MeiRECDataset(type)
dataloader = DataLoader(
dataset,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers,
drop_last=True,
)
return dataloader