forked from b06b01073/go_thesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoDataset.py
83 lines (57 loc) · 2.5 KB
/
GoDataset.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import torch
from torch.utils.data import Dataset, DataLoader
from config.dataset_config import dataset_config
from GameUnroller import GameUnroller
import gogame
import goutils
import numpy as np
from seed import set_seed
set_seed()
class GoTrainDataset(Dataset):
def __init__(self, games):
"""
Args:
games (list of string): A list of string, where each entry represents a single game.
"""
self.games = games
self.unroller = GameUnroller()
def __len__(self):
return len(self.games)
def __getitem__(self, idx):
game = self.games[idx]
inputs, labels = self.unroller.unroll(game) # we only return the raw feature inputs, things such as zero padding should be done by the user of this dataset
# in training set, random symmetric augmentation is performed
for i in range(len(inputs)):
inputs[i], labels[i] = gogame.random_symmetry(inputs[i], goutils.action1d_to_onehot(labels[i]))
return np.array(inputs, dtype=np.float32), np.array(labels, dtype=np.int_)
class GoTestDataset(Dataset):
def __init__(self, games):
"""
Args:
games (list of string): A list of string, where each entry represents a single game.
"""
self.games = games
self.unroller = GameUnroller()
def __len__(self):
return len(self.games)
def __getitem__(self, idx):
game = self.games[idx]
inputs, labels = self.unroller.unroll(game) # we only return the raw feature inputs, things such as zero padding should be done by the user of this dataset
return np.array(inputs, dtype=np.float32), np.array(labels, dtype=np.int_)
def get_loader(file, mode):
'''
Args:
file (str): path to training set
model (str): use 'train' to get training set, use 'test' to get testing set, training set performs symmetric augmentation during training time
'''
assert mode in ['train', 'test']
with open(file) as f:
games = f.readlines()
games = [game.rstrip('\n') for game in games] # strip the newline char
games = [game.split(',')[1:] for game in games] # extract the moves and discard the file name
dataset = GoTrainDataset(games) if mode == 'train' else GoTestDataset(games)
return DataLoader(
dataset,
batch_size=dataset_config['batch_size'],
num_workers=dataset_config['num_workers'],
)