-
Notifications
You must be signed in to change notification settings - Fork 463
/
sac.py
173 lines (143 loc) · 5.79 KB
/
sac.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
import gym
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Normal
import numpy as np
import collections, random
#Hyperparameters
lr_pi = 0.0005
lr_q = 0.001
init_alpha = 0.01
gamma = 0.98
batch_size = 32
buffer_limit = 50000
tau = 0.01 # for target network soft update
target_entropy = -1.0 # for automated alpha update
lr_alpha = 0.001 # for automated alpha update
class ReplayBuffer():
def __init__(self):
self.buffer = collections.deque(maxlen=buffer_limit)
def put(self, transition):
self.buffer.append(transition)
def sample(self, n):
mini_batch = random.sample(self.buffer, n)
s_lst, a_lst, r_lst, s_prime_lst, done_mask_lst = [], [], [], [], []
for transition in mini_batch:
s, a, r, s_prime, done = transition
s_lst.append(s)
a_lst.append([a])
r_lst.append([r])
s_prime_lst.append(s_prime)
done_mask = 0.0 if done else 1.0
done_mask_lst.append([done_mask])
return torch.tensor(s_lst, dtype=torch.float), torch.tensor(a_lst, dtype=torch.float), \
torch.tensor(r_lst, dtype=torch.float), torch.tensor(s_prime_lst, dtype=torch.float), \
torch.tensor(done_mask_lst, dtype=torch.float)
def size(self):
return len(self.buffer)
class PolicyNet(nn.Module):
def __init__(self, learning_rate):
super(PolicyNet, self).__init__()
self.fc1 = nn.Linear(3, 128)
self.fc_mu = nn.Linear(128,1)
self.fc_std = nn.Linear(128,1)
self.optimizer = optim.Adam(self.parameters(), lr=learning_rate)
self.log_alpha = torch.tensor(np.log(init_alpha))
self.log_alpha.requires_grad = True
self.log_alpha_optimizer = optim.Adam([self.log_alpha], lr=lr_alpha)
def forward(self, x):
x = F.relu(self.fc1(x))
mu = self.fc_mu(x)
std = F.softplus(self.fc_std(x))
dist = Normal(mu, std)
action = dist.rsample()
log_prob = dist.log_prob(action)
real_action = torch.tanh(action)
real_log_prob = log_prob - torch.log(1-torch.tanh(action).pow(2) + 1e-7)
return real_action, real_log_prob
def train_net(self, q1, q2, mini_batch):
s, _, _, _, _ = mini_batch
a, log_prob = self.forward(s)
entropy = -self.log_alpha.exp() * log_prob
q1_val, q2_val = q1(s,a), q2(s,a)
q1_q2 = torch.cat([q1_val, q2_val], dim=1)
min_q = torch.min(q1_q2, 1, keepdim=True)[0]
loss = -min_q - entropy # for gradient ascent
self.optimizer.zero_grad()
loss.mean().backward()
self.optimizer.step()
self.log_alpha_optimizer.zero_grad()
alpha_loss = -(self.log_alpha.exp() * (log_prob + target_entropy).detach()).mean()
alpha_loss.backward()
self.log_alpha_optimizer.step()
class QNet(nn.Module):
def __init__(self, learning_rate):
super(QNet, self).__init__()
self.fc_s = nn.Linear(3, 64)
self.fc_a = nn.Linear(1,64)
self.fc_cat = nn.Linear(128,32)
self.fc_out = nn.Linear(32,1)
self.optimizer = optim.Adam(self.parameters(), lr=learning_rate)
def forward(self, x, a):
h1 = F.relu(self.fc_s(x))
h2 = F.relu(self.fc_a(a))
cat = torch.cat([h1,h2], dim=1)
q = F.relu(self.fc_cat(cat))
q = self.fc_out(q)
return q
def train_net(self, target, mini_batch):
s, a, r, s_prime, done = mini_batch
loss = F.smooth_l1_loss(self.forward(s, a) , target)
self.optimizer.zero_grad()
loss.mean().backward()
self.optimizer.step()
def soft_update(self, net_target):
for param_target, param in zip(net_target.parameters(), self.parameters()):
param_target.data.copy_(param_target.data * (1.0 - tau) + param.data * tau)
def calc_target(pi, q1, q2, mini_batch):
s, a, r, s_prime, done = mini_batch
with torch.no_grad():
a_prime, log_prob= pi(s_prime)
entropy = -pi.log_alpha.exp() * log_prob
q1_val, q2_val = q1(s_prime,a_prime), q2(s_prime,a_prime)
q1_q2 = torch.cat([q1_val, q2_val], dim=1)
min_q = torch.min(q1_q2, 1, keepdim=True)[0]
target = r + gamma * done * (min_q + entropy)
return target
def main():
env = gym.make('Pendulum-v1')
memory = ReplayBuffer()
q1, q2, q1_target, q2_target = QNet(lr_q), QNet(lr_q), QNet(lr_q), QNet(lr_q)
pi = PolicyNet(lr_pi)
q1_target.load_state_dict(q1.state_dict())
q2_target.load_state_dict(q2.state_dict())
score = 0.0
print_interval = 20
for n_epi in range(10000):
s, _ = env.reset()
done = False
count = 0
while count < 200 and not done:
a, log_prob= pi(torch.from_numpy(s).float())
s_prime, r, done, truncated, info = env.step([2.0*a.item()])
memory.put((s, a.item(), r/10.0, s_prime, done))
score +=r
s = s_prime
count += 1
if memory.size()>1000:
for i in range(20):
mini_batch = memory.sample(batch_size)
td_target = calc_target(pi, q1_target, q2_target, mini_batch)
q1.train_net(td_target, mini_batch)
q2.train_net(td_target, mini_batch)
entropy = pi.train_net(q1, q2, mini_batch)
q1.soft_update(q1_target)
q2.soft_update(q2_target)
if n_epi%print_interval==0 and n_epi!=0:
print("# of episode :{}, avg score : {:.1f} alpha:{:.4f}".format(n_epi, score/print_interval, pi.log_alpha.exp()))
score = 0.0
env.close()
if __name__ == '__main__':
main()