-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDQN_shrink_SymNN.py
239 lines (199 loc) · 7.31 KB
/
DQN_shrink_SymNN.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""
Shrink NN until it fails to play perfectly.
The goal is to find the minimal NN size that can solve TicTacToe.
1. Should we use SymNN for this as first try?
If not then use fully-connected, which seems easier to control
Using:
PyTorch: 1.9.1+cpu
gym: 0.8.0
"""
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.autograd import Variable
from torch.distributions import Categorical
from torch.distributions import Normal
import random
import numpy as np
np.random.seed(7)
torch.manual_seed(7)
device = torch.device("cpu")
class ReplayBuffer:
def __init__(self, capacity):
self.capacity = capacity
self.buffer = []
self.position = 0
def push(self, state, action, reward, next_state, done):
if len(self.buffer) < self.capacity:
self.buffer.append(None)
self.buffer[self.position] = (state, action, reward, next_state, done)
self.position = (self.position + 1) % self.capacity
def last_reward(self):
return self.buffer[self.position-1][2]
def sample(self, batch_size):
# **** Old method: random sample
# batch = random.sample(self.buffer, batch_size)
# New method uses the latest data, seems to converge a bit faster
# initially, but overall performance is similar to old method
if self.position >= batch_size:
batch = self.buffer[self.position - batch_size : self.position]
else:
batch = self.buffer[: self.position] + self.buffer[-(batch_size - self.position) :]
assert len(batch) == batch_size, "batch size incorrect"
states, actions, rewards, next_states, dones = \
map(np.stack, zip(*batch)) # stack for each element
'''
the * serves as unpack: sum(a,b) <=> batch=(a,b), sum(*batch) ;
zip: a=[1,2], b=[2,3], zip(a,b) => [(1, 2), (2, 3)] ;
the map serves as mapping the function on each list element: map(square, [2,3]) => [4,9] ;
np.stack((1,2)) => array([1, 2])
'''
# print("sampled state=", state)
# print("sampled action=", action)
return states, actions, rewards, next_states, dones
def __len__(self):
return len(self.buffer)
class symNN(nn.Module):
def __init__(self, input_dim, action_dim, hidden_dim, activation=F.relu, init_w=3e-3):
super(symNN, self).__init__()
# **** h-network, also referred to as "phi" in the literature
# input dim = 2 because each proposition is a 2-vector
self.h1 = nn.Linear(2, hidden_dim, bias=True)
self.relu1 = nn.Tanh()
self.h2 = nn.Linear(hidden_dim, 9, bias=True)
self.relu2 = nn.Tanh()
# **** g-network, also referred to as "rho" in the literature
# input dim can be arbitrary, here chosen to be n_actions
self.g1 = nn.Linear(9, hidden_dim, bias=True)
self.relu3 = nn.Tanh()
# output dim must be n_actions
self.g2 = nn.Linear(hidden_dim, action_dim, bias=True)
def forward(self, x):
# input dim = n_features = 9 x 2 = 18
# there are 9 h-networks each taking a dim-2 vector input
# First we need to split the input into 9 parts:
xs = torch.split(x, 2, dim=1)
# print("xs=", xs)
# h-network:
ys = []
for i in range(9): # repeat h1 9 times
ys.append( self.relu1( self.h1(xs[i]) ))
zs = []
for i in range(9): # repeat h2 9 times
zs.append( self.relu2( self.h2(ys[i]) ))
# add all the z's together:
z = torch.stack(zs, dim=1)
z = torch.sum(z, dim=1)
# g-network:
z1 = self.g1(z)
z1 = self.relu3(z1)
z2 = self.g2(z1)
# z2 = self.softmax(z2)
return z2 # = logits
class DQN():
def __init__(
self,
action_dim,
state_dim,
learning_rate = 3e-4,
gamma = 0.9 ):
super(DQN, self).__init__()
self.action_dim = action_dim
self.state_dim = state_dim
self.lr = learning_rate
self.gamma = gamma
self.replay_buffer = ReplayBuffer(int(1e6))
hidden_dim = 4
self.symnet = symNN(state_dim, action_dim, hidden_dim, activation=F.relu).to(device)
self.q_criterion = nn.MSELoss()
self.q_optimizer = optim.Adam(self.symnet.parameters(), lr=self.lr)
def choose_action(self, state, deterministic=True):
state = torch.FloatTensor(state[0:18]).unsqueeze(0).to(device)
logits = self.symnet(state)
probs = torch.softmax(logits, dim=1)
dist = Categorical(probs)
action = dist.sample().numpy()[0]
# print("chosen action=", action)
return action
def update(self, batch_size, reward_scale, gamma=0.99):
alpha = 1.0 # trade-off between exploration (max entropy) and exploitation (max Q)
state, action, reward, next_state, done = self.replay_buffer.sample(batch_size)
# print('sample (state, action, reward, next state, done):', state, action, reward, next_state, done)
state = torch.FloatTensor(state).to(device)[:,0:18]
next_state = torch.FloatTensor(next_state).to(device)[:,0:18]
action = torch.LongTensor(action).to(device)
reward = torch.FloatTensor(reward).to(device) # .to(device) # reward is single value, unsqueeze() to add one dim to be [reward] at the sample dim;
done = torch.BoolTensor(done).to(device)
logits = self.symnet(state)
next_logits = self.symnet(next_state)
# **** Train deep Q function, this is just Bellman equation:
# DQN(st,at) += η [ R + γ max_a DQN(s_t+1,a) - DQN(st,at) ]
# DQN[s, action] += self.lr *( reward + self.gamma * np.max(DQN[next_state, :]) - DQN[s, action] )
# max 是做不到的,但似乎也可以做到。 DQN 输出的是 probs.
# probs 和 Q 有什么关系? Q 的 Boltzmann 是 probs (SAC 的做法).
# This implies that Q = logits.
# logits[at] += self.lr *( reward + self.gamma * np.max(logits[next_state, next_a]) - logits[at] )
q = logits[range(logits.shape[0]), action]
m = torch.max(next_logits, 1, keepdim=False).values
# print("m:", m.shape)
# q = q + self.lr *( reward + self.gamma * m - q )
target_q = torch.where(done, reward, reward + self.gamma * m)
# print("q, target_q:", q.shape, target_q.shape)
q_loss = self.q_criterion(q, target_q.detach())
self.q_optimizer.zero_grad()
q_loss.backward()
self.q_optimizer.step()
return
def visualize_q(self, board):
# convert board vector to state vector
vec = []
for i in range(9):
symbol = board[i]
vec += [symbol, i]
state = torch.FloatTensor(vec).unsqueeze(0).to(device)
logits = self.symnet(state)
probs = torch.softmax(logits, dim=1)
return probs.squeeze(0)
def net_info(self):
config_h = "(2)-4-9"
config_g = "9-4-(9)"
total = 0
neurons = config_h.split('-')
last_n = 3
for n in neurons[1:]:
n = int(n)
total += last_n * n
last_n = n
total *= 9
neurons = config_g.split('-')
for n in neurons[1:-1]:
n = int(n)
total += last_n * n
last_n = n
total += last_n * 9
return (config_h + ':' + config_g, total)
def play_random(self, state, action_space):
# Select an action (0-9) randomly
# NOTE: random player never chooses occupied squares
empties = [0,1,2,3,4,5,6,7,8]
# Find and collect all empty squares
# scan through all 9 propositions, each proposition is a 2-vector
for i in range(0, 18, 2):
# 'proposition' is a numpy array[2]
sym = state[i]
if sym == 1 or sym == -1:
x = state[i + 1]
j = x + 4
empties.remove(j)
# Select an available square randomly
action = random.sample(empties, 1)[0]
return action
def save_net(self, fname):
torch.save(self.symnet.state_dict(), \
"PyTorch_models/" + fname + ".dict")
print("Model saved.")
def load_net(self, fname):
self.symnet.load_state_dict(torch.load("PyTorch_models/" + fname + ".dict"))
self.symnet.eval()
print("Model loaded.")