-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathatt_paper_utils.py
45 lines (30 loc) · 1.15 KB
/
att_paper_utils.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
import os
import pickle
import torch
from torch.utils.data import Dataset
def check_extension(filename):
if os.path.splitext(filename)[1] != ".pkl":
return filename + ".pkl"
return filename
def load_dataset(filename):
with open(check_extension(filename), 'rb') as f:
return pickle.load(f)
class TSPDataset(Dataset):
def __init__(self, filename=None, size=50, num_samples=1000000, offset=0, distribution=None):
super(TSPDataset, self).__init__()
self.data_set = []
if filename is not None:
assert os.path.splitext(filename)[1] == '.pkl'
with open(filename, 'rb') as f:
data = pickle.load(f)
self.data = [torch.FloatTensor(row) for row in (data[offset:offset+num_samples])]
else:
# Sample points randomly in [0, 1] square
self.data = [torch.FloatTensor(size, 2).uniform_(0, 1) for i in range(num_samples)]
self.size = len(self.data)
def __len__(self):
return self.size
def __getitem__(self, idx):
return self.data[idx]
def make_dataset(*args, **kwargs):
return TSPDataset(*args, **kwargs)