-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_generator.py
128 lines (102 loc) · 4.34 KB
/
data_generator.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import torch
import gdown
import numpy as np
from torch.nn import functional as F
from torch.utils.data import Dataset
# Dataset for Auto encoder
class TrainSet_AE(Dataset):
def __init__(self, train_games):
super().__init__()
self.train_games = train_games
self.bitboard = None
for board in list(self.train_games):
temp = np.load('data/bitboard/' + board)
self.bitboard = temp[:int(len(temp)*.85)]
np.random.shuffle(self.bitboard)
def __getitem__(self, index):
return torch.from_numpy(self.bitboard[index]).type(torch.FloatTensor), 0
def __len__(self):
return self.bitboard.shape[0]
class TestSet_AE(Dataset):
def __init__(self, test_games):
super().__init__()
self.test_games = test_games
self.bitboard = None
for board in list(self.test_games):
temp = np.load('data/bitboard/' + board)
self.bitboard = temp[int(len(temp)*.85):]
np.random.shuffle(self.bitboard)
def __getitem__(self, index):
return torch.from_numpy(self.bitboard[index]).type(torch.FloatTensor), 0
def __len__(self):
return self.bitboard.shape[0]
# dataset for Siamese
class TrainSet(Dataset):
def __init__(self, games, labels):
super().__init__()
self.games = games
self.labels = labels
self.train_games = None
self.train_labels_win = None
self.train_games_win = None
self.train_games_loss = None
for file in list(self.games):
game = np.load(file)
self.train_games = game[:int(len(game)*.85)]
for file in list(self.labels):
label = np.load(file)
self.train_label_win = label[:int(len(label)*.85)]
self.train_games_win = self.train_games[self.train_label_win == 1]
self.train_games_loss = self.train_games[self.train_label_win == -1]
def __getitem__(self, index):
rand_win = self.train_games_win[
np.random.randint(0, self.train_games_win.shape[0])]
rand_loss = self.train_games_loss[
np.random.randint(0, self.train_games_loss.shape[0])]
order = np.random.randint(0,2)
if order == 0:
stacked = np.hstack((rand_win, rand_loss))
stacked = torch.from_numpy(stacked).type(torch.FloatTensor)
label = torch.from_numpy(np.array([1, 0])).type(torch.FloatTensor)
return stacked, label
else:
stacked = np.hstack((rand_loss, rand_win))
stacked = torch.from_numpy(stacked).type(torch.FloatTensor)
label = torch.from_numpy(np.array([0, 1])).type(torch.FloatTensor)
return stacked, label
def __len__(self):
return len(self.train_games_win) + len(self.train_games_loss)
class TestSet(Dataset):
def __init__(self, games, labels):
super().__init__()
self.games = games
self.labels = labels
self.test_games = None
self.test_labels_win = None
self.test_games_win = None
self.test_games_loss = None
for file in list(self.games):
game = np.load(file)
self.test_games = game[int(len(game)*.85):]
for file in list(self.labels):
label = np.load(file)
self.test_label_win = label[int(len(label)*.85):]
self.test_games_win = self.test_games[self.test_label_win == 1]
self.test_games_loss = self.test_games[self.test_label_win == -1]
def __getitem__(self, index):
rand_win = self.test_games_win[np.random.randint(0, self.test_games_win.shape[0])]
rand_loss = self.test_games_loss[np.random.randint(0, self.test_games_loss.shape[0])]
order = np.random.randint(0,2)
if order == 0:
stacked = np.hstack((rand_win, rand_loss))
stacked = torch.from_numpy(stacked).type(torch.FloatTensor)
label = torch.from_numpy(np.array([1, 0])).type(torch.FloatTensor)
return stacked, label
else:
stacked = np.hstack((rand_loss, rand_win))
stacked = torch.from_numpy(stacked).type(torch.FloatTensor)
label = torch.from_numpy(np.array([0, 1])).type(torch.FloatTensor)
return stacked, label
def __len__(self):
return len(self.test_games_win) + len(self.test_games_loss)