-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytorch_DDPG.py
244 lines (205 loc) · 8.66 KB
/
pytorch_DDPG.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
240
241
242
243
244
"""
Deep Deterministic Policy Gradient (DDPG), Reinforcement Learning.
torch实现DDPG算法
"""
import random
from collections import deque
import torch
import numpy as np
import torch.nn as nn
seed = 1
torch.manual_seed(seed)
np.random.seed(seed)
torch.set_default_dtype(torch.float)
# Actor Net
# Actor:输入是state,输出的是一个确定性的action
class Actor(nn.Module):
def __init__(self, state_dim, action_dim, action_bound):
super(Actor, self).__init__()
self.action_bound = torch.FloatTensor(action_bound)
# layer
self.layer_1 = nn.Linear(state_dim, 30)
nn.init.normal_(self.layer_1.weight, 0., 0.3)
nn.init.constant_(self.layer_1.bias, 0.1)
# self.layer_1.weight.data.normal_(0.,0.3)
# self.layer_1.bias.data.fill_(0.1)
self.output = nn.Linear(30, action_dim)
self.output.weight.data.normal_(0., 0.3)
self.output.bias.data.fill_(0.1)
def forward(self, s):
s = torch.Tensor(s)
a = torch.relu(self.layer_1(s))
a = self.output(a)
# 对action进行放缩,实际上a in [-1,1]
return a[:, 0]
# Critic Net
# Critic输入的是当前的state以及Actor输出的action,输出的是Q-value
class Critic(nn.Module):
def __init__(self, state_dim, action_dim):
super(Critic, self).__init__()
n_layer = 30
# layer
self.layer_1 = nn.Linear(state_dim, n_layer)
nn.init.normal_(self.layer_1.weight, 0., 0.1)
nn.init.constant_(self.layer_1.bias, 0.1)
self.layer_2 = nn.Linear(1,action_dim)
nn.init.normal_(self.layer_2.weight, 0., 0.1)
nn.init.constant_(self.layer_2.bias, 0.1)
self.w1_a = torch.nn.Parameter(torch.Tensor(1, action_dim))
torch.nn.init.normal_(self.w1_a, 0, 0.1)
self.b1 = torch.nn.Parameter(torch.Tensor(action_dim))
torch.nn.init.constant_(self.b1, 0.1)
self.output = nn.Linear(n_layer, 1)
def forward(self, s, a):
s = torch.Tensor(s)
a = torch.Tensor(a)
s = self.layer_1(s)
a = self.layer_2(a.unsqueeze(1))# * self.w1_a + self.b1
q_val = self.output(torch.relu(s + a))
return q_val[:, 0]
# Deep Deterministic Policy Gradient
class DDPG(object):
def __init__(self, state_dim, action_dim, critic_action_dim, action_bound, replacement, memory_capacity=1000, gamma=0.9, lr_a=0.001,
lr_c=0.002, batch_size=128):
super(DDPG, self).__init__()
self.state_dim = state_dim
self.action_dim = action_dim
self.memory_capacity = memory_capacity
self.replacement = replacement
self.t_replace_counter = 0
self.gamma = gamma
self.lr_a = lr_a
self.lr_c = lr_c
self.batch_size = batch_size
# 记忆库
self.memory = deque(maxlen=memory_capacity) # Experience replay buffer
self.pointer = 0
# 定义 Actor 网络
self.actor = Actor(state_dim, action_dim, action_bound)
self.actor_target = Actor(state_dim, action_dim, action_bound)
# 定义 Critic 网络
self.critic = Critic(state_dim, critic_action_dim)
self.critic_target = Critic(state_dim, critic_action_dim)
# 定义优化器
self.aopt = torch.optim.Adam(self.actor.parameters(), lr=lr_a)
self.copt = torch.optim.Adam(self.critic.parameters(), lr=lr_c)
# 选取损失函数
self.mse_loss = nn.MSELoss()
def sample(self):
indices = np.random.choice(self.memory_capacity, size=self.batch_size)
return self.memory[indices, :]
def choose_action(self, s):
s = torch.FloatTensor(s)
s = np.reshape(s, [-1, self.state_dim])
action = self.actor(s)
return action.detach().numpy()
def learn(self):
# soft replacement and hard replacement
# 用于更新target网络的参数
if self.replacement['name'] == 'soft':
# soft的意思是每次learn的时候更新部分参数
tau = self.replacement['tau']
a_layers = self.actor_target.named_children()
c_layers = self.critic_target.named_children()
for al in a_layers:
a = self.actor.state_dict()[al[0] + '.weight']
al[1].weight.data.mul_((1 - tau))
al[1].weight.data.add_(tau * self.actor.state_dict()[al[0] + '.weight'])
al[1].bias.data.mul_((1 - tau))
al[1].bias.data.add_(tau * self.actor.state_dict()[al[0] + '.bias'])
for cl in c_layers:
cl[1].weight.data.mul_((1 - tau))
cl[1].weight.data.add_(tau * self.critic.state_dict()[cl[0] + '.weight'])
cl[1].bias.data.mul_((1 - tau))
cl[1].bias.data.add_(tau * self.critic.state_dict()[cl[0] + '.bias'])
else:
# hard的意思是每隔一定的步数才更新全部参数
if self.t_replace_counter % self.replacement['rep_iter'] == 0:
self.t_replace_counter = 0
a_layers = self.actor_target.named_children()
c_layers = self.critic_target.named_children()
for al in a_layers:
al[1].weight.data = self.actor.state_dict()[al[0] + '.weight']
al[1].bias.data = self.actor.state_dict()[al[0] + '.bias']
for cl in c_layers:
cl[1].weight.data = self.critic.state_dict()[cl[0] + '.weight']
cl[1].bias.data = self.critic.state_dict()[cl[0] + '.bias']
self.t_replace_counter += 1
# 从记忆库中采样bacth data
bm = np.array(random.sample(self.memory, self.batch_size))
bs = bm[:, :self.state_dim]
ba = bm[:, self.state_dim:self.state_dim + 1][:, 0]
br = torch.FloatTensor(bm[:, (self.state_dim + 1):(self.state_dim + 2)][:, 0])
bs_ = bm[:, (self.state_dim+2):(self.state_dim*2 + 2)]
# 训练Actor
a = self.actor(bs)
q = self.critic(bs, a)
a_loss = -torch.mean(q)
self.aopt.zero_grad()
a_loss.backward(retain_graph=True)
self.aopt.step()
# 训练critic
a_ = self.actor_target(bs_)
q_ = self.critic_target(bs_, a_)
q1 = self.critic(bs, ba)
q_target = br + self.gamma * q_ - q1
td_error = torch.mean(q_target.pow(2))
self.copt.zero_grad()
td_error.backward()
self.copt.step()
def store_transition(self, s, a, r, s_):
state, next_state = s[np.newaxis, :], s_[np.newaxis, :]
action, reward = np.array(a), np.array(r)
action = np.reshape(action, [1, -1])
reward = np.reshape(reward, [1, -1])
transition = np.concatenate((state, action, reward, next_state), axis=1)
self.pointer += 1
self.memory.append(transition[0, :])
import gym
import time
if __name__ == '__main__':
# hyper parameters
VAR = 3 # control exploration
MAX_EPISODES = 500
MAX_EP_STEPS = 200
MEMORY_CAPACITY = 10000
REPLACEMENT = [
dict(name='soft', tau=0.005),
dict(name='hard', rep_iter=600)
][0] # you can try different target replacement strategies
ENV_NAME = 'Pendulum-v1'
RENDER = False
# train
env = gym.make(ENV_NAME)
env = env.unwrapped
env.seed(1)
s_dim = env.observation_space.shape[0]
a_dim = env.action_space.shape[0]
a_bound = env.action_space.high
ddpg = DDPG(state_dim=s_dim,
action_dim=a_dim,
action_bound=a_bound,
replacement=REPLACEMENT,
memory_capacity=MEMORY_CAPACITY)
t1 = time.time()
for i in range(MAX_EPISODES):
s = env.reset()
ep_reward = 0
for j in range(MAX_EP_STEPS):
if RENDER:
env.render()
# Add exploration noise
a = ddpg.choose_action(s)
a = np.clip(np.random.normal(a, VAR), -2, 2) # 在动作选择上添加随机噪声
s_, r, done, info, _ = env.step(a)
ddpg.store_transition(s, a, r / 10, s_)
if ddpg.pointer > MEMORY_CAPACITY:
VAR *= .9995 # decay the action randomness
ddpg.learn()
s = s_
ep_reward += r
if j == MAX_EP_STEPS - 1:
print('Episode:', i, ' Reward: %i' % int(ep_reward), 'Explore: %.2f' % VAR, )
if ep_reward > -300: RENDER = True
break
print('Running time: ', time.time() - t1)