-
Notifications
You must be signed in to change notification settings - Fork 5
/
ppo.py
97 lines (71 loc) · 3.17 KB
/
ppo.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
import numpy as np
from utils import *
from hparams import HyperParams as hp
def get_gae(rewards, masks, values):
rewards = torch.Tensor(rewards)
masks = torch.Tensor(masks)
returns = torch.zeros_like(rewards)
advants = torch.zeros_like(rewards)
running_returns = 0
previous_value = 0
running_advants = 0
for t in reversed(range(0, len(rewards))):
running_returns = rewards[t] + hp.gamma * running_returns * masks[t]
running_tderror = rewards[t] + hp.gamma * previous_value * masks[t] - \
values.data[t]
running_advants = running_tderror + hp.gamma * hp.lamda * \
running_advants * masks[t]
returns[t] = running_returns
previous_value = values.data[t]
advants[t] = running_advants
advants = (advants - advants.mean()) / advants.std()
return returns, advants
def surrogate_loss(actor, advants, states, old_policy, actions, index):
mu, std, logstd = actor(torch.Tensor(states))
new_policy = log_density(actions, mu, std, logstd)
old_policy = old_policy[index]
ratio = torch.exp(new_policy - old_policy)
surrogate = ratio * advants
return surrogate, ratio
def train_model(actor, critic, memory, actor_optim, critic_optim):
memory = np.array(memory)
states = np.vstack(memory[:, 0])
actions = list(memory[:, 1])
rewards = list(memory[:, 2])
masks = list(memory[:, 3])
values = critic(torch.Tensor(states))
# ----------------------------
# step 1: get returns and GAEs and log probability of old policy
returns, advants = get_gae(rewards, masks, values)
mu, std, logstd = actor(torch.Tensor(states))
old_policy = log_density(torch.Tensor(actions), mu, std, logstd)
criterion = torch.nn.MSELoss()
n = len(states)
arr = np.arange(n)
# ----------------------------
# step 2: get value loss and actor loss and update actor & critic
for epoch in range(10):
np.random.shuffle(arr)
for i in range(n // hp.batch_size):
batch_index = arr[hp.batch_size * i: hp.batch_size * (i + 1)]
batch_index = torch.LongTensor(batch_index)
inputs = torch.Tensor(states)[batch_index]
returns_samples = returns.unsqueeze(1)[batch_index]
advants_samples = advants.unsqueeze(1)[batch_index]
actions_samples = torch.Tensor(actions)[batch_index]
loss, ratio = surrogate_loss(actor, advants_samples, inputs,
old_policy.detach(), actions_samples,
batch_index)
values = critic(inputs)
critic_loss = criterion(values, returns_samples)
critic_optim.zero_grad()
critic_loss.backward()
critic_optim.step()
clipped_ratio = torch.clamp(ratio,
1.0 - hp.clip_param,
1.0 + hp.clip_param)
clipped_loss = clipped_ratio * advants_samples
actor_loss = -torch.min(loss, clipped_loss).mean()
actor_optim.zero_grad()
actor_loss.backward()
actor_optim.step()