-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.py
More file actions
51 lines (39 loc) · 1.39 KB
/
Copy pathpipeline.py
File metadata and controls
51 lines (39 loc) · 1.39 KB
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
chess_data_raw = pd.read_csv('/content/chess_games.csv', usecols=['AN','WhiteElo'])
chess_data = chess_data_raw[chess_data_raw['WhiteElo'] >2000]
del chess_data_raw
gc.collect()
chess_data = chess_data[['AN']]
chess_data = chess_data[~chess_data['AN'].str.contains('{')]
chess_data = chess_data[chess_data['AN'].str.len() > 20]
game_indices = chess_data.index
train_indices, test_indices = train_test_split(game_indices, test_size=0.2, random_state=42)
train_data = chess_data.loc[train_indices]
test_data = chess_data.loc[test_indices]
print("Train set shape :", train_data.shape)
print("Test set shape :", train_data.shape)
print("Train set :")
print(train_data)
print("Test set :")
print(test_data)
class ChessDataset (Dataset):
def __init__(self,games , is_train=True):
super(ChessDataset,self).__init__()
self.games =games
self.is_train = is_train
def __len__(self):
return 706700 if self.is_train else 176676
def __getitem__(self, index):
game_i= np.random.randint(self.games.shape[0])
random_game = self.games ['AN'].values [game_i]
moves = create_move_list(random_game)
game_state_i= np.random.randint(len(moves)-1)
next_move = moves[game_state_i]
moves = moves[:game_state_i]
board = chess.Board()
for move in moves:
board.push_san(move)
x = board_2_rep(board)
y = move_2_rep(next_move,board)
if game_state_i % 2 == 1 :
x *= -1
return x,y