-
Notifications
You must be signed in to change notification settings - Fork 34
/
model.py
43 lines (29 loc) · 1.2 KB
/
model.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
class Connect2Model(nn.Module):
def __init__(self, board_size, action_size, device):
super(Connect2Model, self).__init__()
self.device = device
self.size = board_size
self.action_size = action_size
self.fc1 = nn.Linear(in_features=self.size, out_features=16)
self.fc2 = nn.Linear(in_features=16, out_features=16)
# Two heads on our network
self.action_head = nn.Linear(in_features=16, out_features=self.action_size)
self.value_head = nn.Linear(in_features=16, out_features=1)
self.to(device)
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
action_logits = self.action_head(x)
value_logit = self.value_head(x)
return F.softmax(action_logits, dim=1), torch.tanh(value_logit)
def predict(self, board):
board = torch.FloatTensor(board.astype(np.float32)).to(self.device)
board = board.view(1, self.size)
self.eval()
with torch.no_grad():
pi, v = self.forward(board)
return pi.data.cpu().numpy()[0], v.data.cpu().numpy()[0]