-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpolicy.py
199 lines (159 loc) · 6.72 KB
/
policy.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Categorical
from copy import deepcopy
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class Memory:
def __init__(self):
self.actions = []
self.states = []
self.logprobs = []
self.rewards = []
self.action_masks = []
def clear_memory(self):
del self.actions[:]
del self.states[:]
del self.logprobs[:]
del self.rewards[:]
del self.action_masks[:]
class ActorCritic(nn.Module):
def __init__(self, state_dim, action_dim, n_latent_var=256):
super(ActorCritic, self).__init__()
# actor
self.obs_layer = nn.Linear(state_dim, n_latent_var)
self.actor_layer = nn.Sequential(
nn.Linear(n_latent_var, n_latent_var),
nn.Tanh(),
nn.Linear(n_latent_var, action_dim)
)
#self.action_layer_weight = nn.Parameter(torch.ones(1,state_dim))
#self.action_layer_bias = nn.Parameter(torch.zeros(1, state_dim))
# critic
self.value_layer = nn.Sequential(
nn.Linear(n_latent_var, n_latent_var),
nn.Tanh(),
nn.Linear(n_latent_var, 1)
)
def forward(self):
raise NotImplementedError
def hidden_state(self, state):
hidden_state = self.obs_layer(state)
return hidden_state
def act(self, state, memory, action_mask):
# self.action_layer_weight * state + self.action_layer_bias #B, N
hidden_state = self.hidden_state(state)
logits = self.actor_layer(hidden_state)
inf_mask = torch.clamp(torch.log(action_mask.float()),
min=torch.finfo(torch.float32).min)
logits = logits + inf_mask
action_probs = F.softmax(logits, dim=-1)
dist = Categorical(action_probs)
action = dist.sample()
memory.states.append(state)
memory.actions.append(action)
memory.action_masks.append(deepcopy(action_mask))
memory.logprobs.append(dist.log_prob(action))
return action.detach()
def evaluate(self, state, action, action_mask):
hidden_state = self.hidden_state(state)
logits = self.actor_layer(hidden_state)
inf_mask = torch.clamp(torch.log(action_mask.float()),
min=torch.finfo(torch.float32).min)
logits = logits + inf_mask
action_probs = F.softmax(logits, dim=-1)
dist = Categorical(action_probs)
action_logprobs = dist.log_prob(action)
dist_entropy = dist.entropy()
state_value = self.value_layer(hidden_state)
return action_logprobs, torch.squeeze(state_value), dist_entropy
class PPO:
def __init__(self, state_dim, action_dim, lr, betas, K_epochs, eps_clip):
self.lr = lr
self.betas = betas
self.eps_clip = eps_clip
self.K_epochs = K_epochs
self.policy = ActorCritic(state_dim, action_dim).to(device)
self.optimizer = torch.optim.Adam(
self.policy.parameters(), lr=lr, betas=betas)
self.policy_old = ActorCritic(state_dim, action_dim).to(device)
self.policy_old.load_state_dict(self.policy.state_dict())
self.MseLoss = nn.MSELoss()
def update(self, memory):
# Monte Carlo estimate of state rewards:
rewards = []
# discounted_reward = 0
# for reward, is_terminal in zip(reversed(memory.rewards), reversed(memory.is_terminals)):
# if is_terminal:
# discounted_reward = 0
# discounted_reward = reward + (self.gamma * discounted_reward)
# rewards.insert(0, discounted_reward)
# Normalizing the rewards:
#rewards = torch.tensor(rewards, dtype=torch.float32).to(device)
rewards = memory.rewards[0].repeat(len(memory.actions))
rewards = rewards / (rewards.std() + 1e-5)
#rewards = (rewards - rewards.mean()) / (rewards.std() + 1e-5)
# convert list to tensor
old_states = torch.cat(memory.states, dim=0).detach()
old_actions = torch.cat(memory.actions, dim=0).detach()
old_logprobs = torch.cat(memory.logprobs, dim=0).detach()
old_actionmask = torch.cat(memory.action_masks, dim=0).detach()
# Optimize policy for K epochs:
for _ in range(self.K_epochs):
# Evaluating old actions and values :
logprobs, state_values, dist_entropy = self.policy.evaluate(
old_states, old_actions, old_actionmask)
# Finding the ratio (pi_theta / pi_theta__old):
ratios = torch.exp(logprobs - old_logprobs.detach())
# Finding Surrogate Loss:
advantages = rewards - state_values.detach()
surr1 = ratios * advantages
surr2 = torch.clamp(ratios, 1-self.eps_clip,
1+self.eps_clip) * advantages
loss = -torch.min(surr1, surr2) + 0.5 * \
self.MseLoss(state_values, rewards) - 0.01*dist_entropy
# take gradient step
self.optimizer.zero_grad()
loss.mean().backward()
self.optimizer.step()
# Copy new weights into old policy:
self.policy_old.load_state_dict(self.policy.state_dict())
def hard_sample(logits, dim=-1):
y_soft = F.softmax(logits, dim=-1)
index = y_soft.max(dim, keepdim=True)[1]
y_hard = torch.zeros_like(y_soft).scatter_(dim, index, 1.0)
ret = y_hard - y_soft.detach() + y_soft
return ret, index.squeeze(1)
class Actor(nn.Module):
def __init__(self, state_dim, action_dim, n_latent_var=256):
super().__init__()
# actor
self.obs_layer = nn.Linear(state_dim, n_latent_var)
self.actor_layer = nn.Sequential(
nn.Linear(n_latent_var, n_latent_var),
nn.Tanh(),
nn.Linear(n_latent_var, action_dim)
)
def forward(self, state, action_mask):
hidden_state = self.obs_layer(state)
logits = self.actor_layer(hidden_state)
inf_mask = torch.clamp(torch.log(action_mask.float()),
min=torch.finfo(torch.float32).min)
logits = logits + inf_mask
train_mask, actions = hard_sample(logits)
return train_mask, actions
class StraightThrough:
def __init__(self, state_dim, action_dim, lr, betas):
self.lr = lr
self.betas = betas
self.policy = Actor(state_dim, action_dim).to(device)
self.optimizer = torch.optim.Adam(
self.policy.parameters(), lr=lr, betas=betas)
def update(self, loss):
self.optimizer.zero_grad()
loss.mean().backward()
self.optimizer.step()
def main():
pass
if __name__ == '__main__':
main()