-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToeNet.py
More file actions
44 lines (39 loc) · 1.71 KB
/
TicTacToeNet.py
File metadata and controls
44 lines (39 loc) · 1.71 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
import torch
import torch.nn as nn
from Game import Game
from PUCTNet import PUCTNet
class TicTacToeNet(PUCTNet):
def __init__(self, max_num_actions, num_players, device = "cpu"):
super().__init__(max_num_actions, num_players) # must be executed first
self.feature_extractor = nn.Sequential(
nn.Conv2d(1, 64, kernel_size = 3, padding = 1),
nn.Conv2d(64, 64, kernel_size = 3, padding = 1),
nn.ReLU(),
nn.Flatten(), # (B, C * W * H)
nn.Linear(64 * 3 * 3, 64),
nn.ReLU()
)
self.policy_head = nn.Sequential(
nn.Linear(64, 64),
nn.ReLU(),
nn.Linear(64, max_num_actions)
)
self.value_head = nn.Sequential(
nn.Linear(64, 64),
nn.ReLU(),
nn.Linear(64, 1)
)
self.device = device
self.to(device)
def predict(self, players, states, possible_actions_list):
# State: Bx3x3 (batch * height * width)
# States from game are already in the perspective of current player, no need to do conversion here
x = torch.tensor(states, dtype = torch.float32, device = self.device).unsqueeze(dim = 1) # transform state into valid input of shape BxCxHxW
policy_logits, value_logits = self(x)
# Scores provided to both players at current state in a zero-sum game
players = torch.tensor(players).to(self.device)
value = torch.tanh(value_logits)
value = value.repeat(1, 2)
value[players == 1, 0] = -value[players == 1, 0]
value[players == 0, 1] = -value[players == 0, 1]
return policy_logits, value # return raw policy logits; value must still be transformed