-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.py
128 lines (114 loc) · 5.11 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
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
# -*- coding: utf-8 -*-
"""
A pure implementation of the AlphaGo's Network part, the network's structure looks like LeNet
The original verion is written by:
@author: Junxiao Song
@github: https://github.com/junxiaosong/AlphaZero_Gomoku/blob/master/policy_value_net_pytorch.py
It is modified to simplify the code by:
@author: Yuan Liu
@github: https://github.com/cmusjtuliuyuan/AlphaGoZero/blob/master/model.py
"""
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
def set_learning_rate(optimizer, lr):
"""Sets the learning rate to the given value"""
for param_group in optimizer.param_groups:
param_group['lr'] = lr
class Net(nn.Module):
"""policy-value network module"""
def __init__(self, board_width, board_height):
super(Net, self).__init__()
self.board_width = board_width
self.board_height = board_height
# common layers
self.conv1 = nn.Conv2d(4, 32, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
# action policy layers
self.act_conv1 = nn.Conv2d(128, 4, kernel_size=1)
self.act_fc1 = nn.Linear(4*board_width*board_height, board_width*board_height)
# state value layers
self.val_conv1 = nn.Conv2d(128, 2, kernel_size=1)
self.val_fc1 = nn.Linear(2*board_width*board_height, 64)
self.val_fc2 = nn.Linear(64, 1)
def forward(self, state_input):
# common layers
x = F.relu(self.conv1(state_input))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
# action policy layers
x_act = F.relu(self.act_conv1(x))
x_act = x_act.view(-1, 4*self.board_width*self.board_height)
x_act = F.softmax(self.act_fc1(x_act), dim=1)
# state value layers
x_val = F.relu(self.val_conv1(x))
x_val = x_val.view(-1, 2*self.board_width*self.board_height)
x_val = F.relu(self.val_fc1(x_val))
x_val = F.tanh(self.val_fc2(x_val))
return x_act, x_val
class PolicyValueNet():
"""policy-value network """
def __init__(self, board_width, board_height, net_params=None, use_gpu=False):
self.use_gpu = use_gpu
self.board_width = board_width
self.board_height = board_height
self.l2_const = 1e-4 # coef of l2 penalty
# the policy value net module
if self.use_gpu:
self.policy_value_net = Net(board_width, board_height).cuda()
else:
self.policy_value_net = Net(board_width, board_height)
self.optimizer = optim.SGD(self.policy_value_net.parameters(),
weight_decay=self.l2_const,
momentum=0.9,
lr=0.001)
if net_params:
self.policy_value_net.load_state_dict(net_params)
def policy_value_fn(self, board):
"""
input: a batch of states
output: a batch of action probabilities and state values
"""
state_batch = board.current_state().reshape(-1, 4, self.board_width, self.board_height)
if self.use_gpu:
state_batch = Variable(torch.FloatTensor(state_batch).cuda())
act_probs, value = self.policy_value_net(state_batch)
return act_probs.data.cpu().numpy()[0], value.data.cpu().numpy()[0]
else:
state_batch = Variable(torch.FloatTensor(state_batch))
act_probs, value = self.policy_value_net(state_batch)
return act_probs.data.numpy()[0], value.data.numpy()[0]
def train_batch(self, state_batch, mcts_probs, winner_batch, lr):
"""perform a training step"""
# wrap in Variable
if self.use_gpu:
state_batch = Variable(torch.FloatTensor(state_batch).cuda())
mcts_probs = Variable(torch.FloatTensor(mcts_probs).cuda())
winner_batch = Variable(torch.FloatTensor(winner_batch).cuda())
else:
state_batch = Variable(torch.FloatTensor(state_batch))
mcts_probs = Variable(torch.FloatTensor(mcts_probs))
winner_batch = Variable(torch.FloatTensor(winner_batch))
# zero the parameter gradients
self.optimizer.zero_grad()
# set learning rate
set_learning_rate(self.optimizer, lr)
# forward
act_probs, value = self.policy_value_net(state_batch)
# define the loss = (z - v)^2 - pi^T * log(p) + c||theta||^2 (Note: the L2 penalty is incorporated in optimizer)
value_loss = F.mse_loss(value.view(-1), winner_batch)
policy_loss = -torch.mean(torch.sum(mcts_probs * torch.log(act_probs), 1))
loss = value_loss + policy_loss
# backward and optimize
loss.backward()
self.optimizer.step()
# calc policy entropy, for monitoring only
entropy = -torch.mean(torch.sum(act_probs * torch.log(act_probs), 1))
return loss.data[0], entropy.data[0]
def get_policy_param(self):
net_params = self.policy_value_net.state_dict()
return net_params