-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseline.py
187 lines (147 loc) · 5.57 KB
/
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
"""
A2C + GAE baseline
Ensure every trajectory has the same length.
"""
import gym
import numpy as np
import random
import time
import torch
from torch.distributions import Categorical
import torch.nn.functional as F
from models import ActorCritic
import matplotlib.pyplot as plt
ITERATION_NUMS = 1000
SAMPLE_NUMS = 50
LR = 0.01
# Best lambda value is lower than gamma,
# empirically lambda introduces far less bias than gamma for a reasonably accruate value function
GAMMA = 0.99
LAMBDA = 0.98
CLIP_GRAD_NORM = 40
def run(random_seed):
torch.manual_seed(random_seed)
np.random.seed(random_seed)
random.seed(random_seed)
gym_name = "CartPole-v1"
task = gym.make(gym_name)
task.seed(random_seed)
discrete = isinstance(task.action_space, gym.spaces.Discrete)
STATE_DIM = task.observation_space.shape[0]
ACTION_DIM = task.action_space.n if discrete else task.action_space.shape[0]
agent = ActorCritic(STATE_DIM, ACTION_DIM)
optim = torch.optim.Adam(agent.parameters(), lr=LR)
scheduler = torch.optim.lr_scheduler.LinearLR(optim, start_factor=1.0, end_factor=0.0, total_iters=ITERATION_NUMS)
gamma = GAMMA
iterations = []
test_results = []
init_state = task.reset()
for i in range(ITERATION_NUMS):
states, actions, advs, v_targets, current_state = roll_out(agent, task, SAMPLE_NUMS, init_state, gamma)
init_state = current_state
train(agent, optim, scheduler, states, actions, advs, v_targets, ACTION_DIM)
# testing
if (i + 1) % (ITERATION_NUMS // 10) == 0:
result = test(gym_name, agent)
print("iteration:", i + 1, "test result:", result / 10.0)
iterations.append(i + 1)
test_results.append(result / 10)
return test_results
def roll_out(agent, task, sample_nums, init_state, gamma):
states = []
actions = []
advs = []
v_targets = []
rewards = []
v_t_s = []
v_t1_s = []
dones = []
state = init_state
for i in range(sample_nums):
states.append(state)
act, v_t = choose_action(agent, state)
actions.append(act)
next_state, reward, done, _ = task.step(act.numpy())
with torch.no_grad():
_, v_t1 = agent(torch.Tensor(next_state))
rewards.append(reward)
v_t = v_t.detach().numpy()
v_t1 = v_t1.detach().numpy()
v_t_s.append(v_t)
v_t1_s.append(v_t1)
dones.append(1 if done is False else 0)
state = next_state
if done:
state = task.reset()
adv, v_target = gae_calculater(rewards, v_t_s, v_t1_s, dones, gamma, LAMBDA)
advs.append(adv)
v_targets.append(v_target)
rewards = []
v_t_s = []
v_t1_s = []
dones = []
adv, v_target = gae_calculater(rewards, v_t_s, v_t1_s, dones, gamma, LAMBDA)
advs.append(adv)
v_targets.append(v_target)
advs = [item for sublist in advs for item in sublist]
v_targets = [item for sublist in v_targets for item in sublist]
return states, actions, advs, v_targets, state
def gae_calculater(rewards, v_t_s, v_t1_s, dones, gamma, lambda_):
"""
Calculate advantages and target v-values
"""
batch_size = len(rewards)
advs = np.zeros(batch_size + 1)
for t in reversed(range(0, batch_size)):
delta = rewards[t] - v_t_s[t] + (gamma * v_t1_s[t] * dones[t])
advs[t] = delta + (gamma * lambda_ * advs[t+1] * dones[t])
value_target = advs[:batch_size] + np.squeeze(v_t_s) # target v is calculated from adv.
return advs[:batch_size], value_target
def train(agent, optim, scheduler, states, actions, advs, v_targets, action_dim):
states = torch.Tensor(np.array(states))
actions = torch.tensor(actions, dtype=torch.int64).view(-1, 1)
v_targets = torch.Tensor(v_targets).detach()
advs = torch.Tensor(advs).detach()
logits, v = agent(states)
logits = logits.view(-1, action_dim)
v = v.view(-1)
probs = F.softmax(logits, dim=1)
log_probs = F.log_softmax(logits, dim=1)
log_probs_act = log_probs.gather(1, actions).view(-1)
loss_policy = - (advs * log_probs_act).mean()
loss_critic = F.mse_loss(v_targets, v, reduction='mean')
loss_entropy = - (log_probs * probs).mean()
loss = loss_policy + .25 * loss_critic - .001 * loss_entropy
optim.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm_(agent.parameters(), CLIP_GRAD_NORM)
optim.step()
scheduler.step()
def test(gym_name, agent):
result = 0
test_task = gym.make(gym_name)
for test_epi in range(10):
state = test_task.reset()
for test_step in range(500):
act, _ = choose_action(agent, state)
next_state, reward, done, _ = test_task.step(act.numpy())
result += reward
state = next_state
if done:
break
return result
@torch.no_grad()
def choose_action(agent, state):
logits, v = agent(torch.Tensor(state))
act_probs = F.softmax(logits, dim=-1)
m = Categorical(act_probs)
act = m.sample()
return act, v
if __name__ == '__main__':
date = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime())
total_test_results = []
for random_seed in range(30):
test_results = run(random_seed)
total_test_results.append(test_results)
dir = 'runs/Baseline/learning_results' + date + '.npy'
np.save(dir, total_test_results)