forked from DeepRLChinese/DeepRL-Chinese
-
Notifications
You must be signed in to change notification settings - Fork 0
/
08_reinforce_with_baseline.py
268 lines (217 loc) · 8.85 KB
/
08_reinforce_with_baseline.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
"""8.3节带基线的REINFORCE算法实现。"""
import argparse
import os
from collections import defaultdict
import gym
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Categorical
class ValueNet(nn.Module):
def __init__(self, dim_state):
super().__init__()
self.fc1 = nn.Linear(dim_state, 64)
self.fc2 = nn.Linear(64, 32)
self.fc3 = nn.Linear(32, 1)
def forward(self, state):
x = F.relu(self.fc1(state))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
class PolicyNet(nn.Module):
def __init__(self, dim_state, num_action):
super().__init__()
self.fc1 = nn.Linear(dim_state, 64)
self.fc2 = nn.Linear(64, 32)
self.fc3 = nn.Linear(32, num_action)
def forward(self, state):
x = F.relu(self.fc1(state))
x = F.relu(self.fc2(x))
x = self.fc3(x)
prob = F.softmax(x, dim=-1)
return prob
class REINFORCE_with_Baseline:
def __init__(self, args):
self.args = args
self.V = ValueNet(args.dim_state)
self.V_target = ValueNet(args.dim_state)
self.pi = PolicyNet(args.dim_state, args.num_action)
self.V_target.load_state_dict(self.V.state_dict())
def get_action(self, state):
probs = self.pi(state)
m = Categorical(probs)
action = m.sample()
logp_action = m.log_prob(action)
return action, logp_action
def compute_value_loss(self, bs, blogp_a, br, bd, bns):
# 累积奖励。
r_lst = []
R = 0
for i in reversed(range(len(br))):
R = self.args.discount * R + br[i]
r_lst.append(R)
r_lst.reverse()
batch_r = torch.tensor(r_lst)
# 计算value loss。
value_loss = F.mse_loss(self.V(bs).squeeze(), batch_r)
return value_loss
def compute_policy_loss(self, bs, blogp_a, br, bd, bns):
# 累积奖励。
r_lst = []
R = 0
for i in reversed(range(len(br))):
R = self.args.discount * R + br[i]
r_lst.append(R)
r_lst.reverse()
batch_r = torch.tensor(r_lst)
# 计算policy loss。
with torch.no_grad():
advantage = self.V(bs).squeeze() - batch_r
policy_loss = 0
for i, logp_a in enumerate(blogp_a):
policy_loss += logp_a * advantage[i]
policy_loss = policy_loss.mean()
return policy_loss
def soft_update(self, tau=0.01):
def soft_update_(target, source, tau_=0.01):
for target_param, param in zip(target.parameters(), source.parameters()):
target_param.data.copy_(target_param.data * (1.0 - tau_) + param.data * tau_)
soft_update_(self.V_target, self.V, tau)
class Rollout:
def __init__(self):
self.state_lst = []
self.action_lst = []
self.logp_action_lst = []
self.reward_lst = []
self.done_lst = []
self.next_state_lst = []
def put(self, state, action, logp_action, reward, done, next_state):
self.state_lst.append(state)
self.action_lst.append(action)
self.logp_action_lst.append(logp_action)
self.reward_lst.append(reward)
self.done_lst.append(done)
self.next_state_lst.append(next_state)
def tensor(self):
bs = torch.as_tensor(self.state_lst).float()
ba = torch.as_tensor(self.action_lst).float()
blogp_a = self.logp_action_lst
br = self.reward_lst
bd = torch.as_tensor(self.done_lst)
bns = torch.as_tensor(self.next_state_lst).float()
return bs, ba, blogp_a, br, bd, bns
class INFO:
def __init__(self):
self.log = defaultdict(list)
self.episode_length = 0
self.episode_reward = 0
self.max_episode_reward = -float("inf")
def put(self, done, reward):
if done is True:
self.episode_length += 1
self.episode_reward += reward
self.log["episode_length"].append(self.episode_length)
self.log["episode_reward"].append(self.episode_reward)
if self.episode_reward > self.max_episode_reward:
self.max_episode_reward = self.episode_reward
self.episode_length = 0
self.episode_reward = 0
else:
self.episode_length += 1
self.episode_reward += reward
def train(args, env, agent: REINFORCE_with_Baseline):
V_optimizer = torch.optim.Adam(agent.V.parameters(), lr=args.lr)
pi_optimizer = torch.optim.Adam(agent.pi.parameters(), lr=args.lr)
info = INFO()
rollout = Rollout()
state, _ = env.reset()
for step in range(args.max_steps):
action, logp_action = agent.get_action(torch.tensor(state).float())
next_state, reward, terminated, truncated, _ = env.step(action.item())
done = terminated or truncated
info.put(done, reward)
rollout.put(
state,
action,
logp_action,
reward,
done,
next_state,
)
state = next_state
if done is True:
# 模型训练。
bs, ba, blogp_a, br, bd, bns = rollout.tensor()
value_loss = agent.compute_value_loss(bs, blogp_a, br, bd, bns)
V_optimizer.zero_grad()
value_loss.backward(retain_graph=True)
V_optimizer.step()
policy_loss = agent.compute_policy_loss(bs, blogp_a, br, bd, bns)
pi_optimizer.zero_grad()
policy_loss.backward()
pi_optimizer.step()
agent.soft_update()
# 打印信息。
info.log["value_loss"].append(value_loss.item())
info.log["policy_loss"].append(policy_loss.item())
episode_reward = info.log["episode_reward"][-1]
episode_length = info.log["episode_length"][-1]
value_loss = info.log["value_loss"][-1]
print(f"step={step}, reward={episode_reward:.0f}, length={episode_length}, max_reward={info.max_episode_reward}, value_loss={value_loss:.1e}")
# 重置环境。
state, _ = env.reset()
rollout = Rollout()
# 保存模型。
if episode_reward == info.max_episode_reward:
save_path = os.path.join(args.output_dir, "model.bin")
torch.save(agent.pi.state_dict(), save_path)
if step % 10000 == 0:
plt.plot(info.log["value_loss"], label="value loss")
plt.legend()
plt.savefig(f"{args.output_dir}/value_loss.png", bbox_inches="tight")
plt.close()
plt.plot(info.log["episode_reward"])
plt.savefig(f"{args.output_dir}/episode_reward.png", bbox_inches="tight")
plt.close()
def eval(args, env, agent):
agent = REINFORCE_with_Baseline(args)
model_path = os.path.join(args.output_dir, "model.bin")
agent.pi.load_state_dict(torch.load(model_path))
episode_length = 0
episode_reward = 0
state, _ = env.reset()
for i in range(5000):
episode_length += 1
action, _ = agent.get_action(torch.from_numpy(state))
next_state, reward, terminated, truncated, info = env.step(action.item())
done = terminated or truncated
episode_reward += reward
state = next_state
if done is True:
print(f"episode reward={episode_reward}, episode length={episode_length}")
state, _ = env.reset()
episode_length = 0
episode_reward = 0
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--env", default="CartPole-v1", type=str, help="Environment name.")
parser.add_argument("--dim_state", default=4, type=int, help="Dimension of state.")
parser.add_argument("--num_action", default=2, type=int, help="Number of action.")
parser.add_argument("--output_dir", default="output", type=str, help="Output directory.")
parser.add_argument("--seed", default=42, type=int, help="Random seed.")
parser.add_argument("--max_steps", default=100_000, type=int, help="Maximum steps for interaction.")
parser.add_argument("--discount", default=0.99, type=float, help="Discount coefficient.")
parser.add_argument("--lr", default=3e-3, type=float, help="Learning rate.")
parser.add_argument("--batch_size", default=32, type=int, help="Batch size.")
parser.add_argument("--no_cuda", action="store_true", help="Avoid using CUDA when available")
parser.add_argument("--do_train", action="store_true", help="Train policy.")
parser.add_argument("--do_eval", action="store_true", help="Evaluate policy.")
args = parser.parse_args()
env = gym.make(args.env)
agent = REINFORCE_with_Baseline(args)
if args.do_train:
train(args, env, agent)
if args.do_eval:
eval(args, env, agent)