-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRL.py
198 lines (155 loc) · 6.25 KB
/
RL.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
import torch
import numpy as np
from torch import nn
# 第三回講義資料に概ね則っている
class DynamicsModel(nn.Module):
def __init__(self, input_dim, output_dim, units=(32, 32)):
super().__init__()
self.model = nn.Sequential(
nn.Linear(input_dim, units[0]),
nn.ReLU(),
nn.Linear(units[0], units[1]),
nn.ReLU(),
nn.Linear(units[1], output_dim)
)
self._loss_fn = nn.MSELoss(reduction="mean")
self._optimizer = torch.optim.Adam(self.model.parameters(), lr=0.001)
def predict(self, inputs):
return self.model(inputs)
def fit(self, inputs, labels):
predicts = self.predict(inputs)
loss = self._loss_fn(predicts, labels)
self._optimizer.zero_grad()
loss.backward()
self._optimizer.step()
return loss.data.numpy()
class RandomPolicy:
def __init__(self, max_action, act_dim):
self._max_action = max_action # action の最大値
self._act_dim = act_dim # action の次元数
def get_actions(self, batch_size):
return np.random.uniform(
low=-self._max_action,
high=self._max_action,
size=(batch_size, self._act_dim))
class GaussianActor(nn.Module):
def __init__(self, state_shape, action_shape):
super().__init__()
self.net = nn.Sequential(
nn.Linear(state_shape[0], 64),
nn.Tanh(),
nn.Linear(64, 64),
nn.Tanh(),
nn.Linear(64, action_shape[0]),
)
self.log_stds = nn.Parameter(torch.zeros(1, action_shape[0]))
def forward(self, states):
return torch.tanh(self.net(states))
def sample(self, states):
return reparameterize(self.net(states), self.log_stds)
def evaluate_log_pi(self, states, actions):
return compute_log_probs(self.net(states), self.log_stds, actions)
class Critic(nn.Module):
def __init__(self, state_shape):
super().__init__()
self.net = nn.Sequential(
nn.Linear(state_shape[0], 64),
nn.Tanh(),
nn.Linear(64, 64),
nn.Tanh(),
nn.Linear(64, 1)
)
def forward(self, states):
return self.net(states)
class PPO:
def __init__(self,
state_shape,
action_shape,
max_action=1.,
device=torch.device('cpu'),
seed=0,
batch_size=64,
lr=3e-4,
discount=0.9,
horizon=2048,
n_epoch=10,
clip_eps=0.2,
lam=0.95,
coef_ent=0.,
max_grad_norm=10.):
fix_seed(seed)
self.actor = GaussianActor(state_shape, action_shape).to(device)
self.optim_actor = torch.optim.Adam(self.actor.parameters(), lr=lr)
self.critic = Critic(state_shape).to(device)
self.optim_critic = torch.optim.Adam(self.critic.parameters(), lr=lr)
self.max_action = max_action
self.device = device
self.batch_size = batch_size
self.discount = discount
self.horizon = horizon
self.n_epoch = n_epoch
self.clip_eps = clip_eps
self.lam = lam
self.coef_ent = coef_ent
self.max_grad_norm = max_grad_norm
def get_action(self, state, test=False):
state = torch.tensor(state, dtype=torch.float, device=self.device).unsqueeze_(0)
with torch.no_grad():
if test:
action = self.actor(state)
else:
action, _ = self.actor.sample(state)
return action.cpu().numpy()[0] * self.max_action
def get_action_and_val(self, state):
state = torch.tensor(state, dtype=torch.float, device=self.device).unsqueeze_(0)
with torch.no_grad():
action, logp = self.actor.sample(state)
value = self.critic(state)
return action * self.max_action, logp, value
def train(self, states, actions, advantages, logp_olds, returns):
states = torch.from_numpy(states).float()
actions = torch.from_numpy(actions / self.max_action).float()
advantages = torch.from_numpy(advantages).float()
logp_olds = torch.from_numpy(logp_olds).float()
returns = torch.from_numpy(returns).float()
self.update_actor(states, actions, logp_olds, advantages)
self.update_critic(states, returns)
def update_critic(self, states, targets):
loss_critic = (self.critic(states) - targets).pow_(2).mean()
self.optim_critic.zero_grad()
loss_critic.backward(retain_graph=False)
nn.utils.clip_grad_norm_(self.critic.parameters(), self.max_grad_norm)
self.optim_critic.step()
def update_actor(self, states, actions, logp_olds, advantages):
log_pis = self.actor.evaluate_log_pi(states, actions)
mean_entropy = -log_pis.mean()
ratios = (log_pis - logp_olds).exp_()
loss_actor1 = -ratios * advantages
loss_actor2 = -torch.clamp(
ratios,
1.0 - self.clip_eps,
1.0 + self.clip_eps
) * advantages
loss_actor = torch.max(loss_actor1, loss_actor2).mean() - self.coef_ent * mean_entropy
self.optim_actor.zero_grad()
loss_actor.backward(retain_graph=False)
nn.utils.clip_grad_norm_(self.actor.parameters(), self.max_grad_norm)
self.optim_actor.step()
def fix_seed(seed):
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
def reparameterize(means, log_stds):
noises = torch.randn_like(means)
actions = means + noises * log_stds.exp()
actions = torch.tanh(actions)
log_pis = calculate_log_pi(log_stds, noises, actions)
return actions, log_pis
def atanh(x):
return 0.5 * (torch.log(1 + x + 1e-6) - torch.log(1 - x + 1e-6))
def compute_log_probs(means, log_stds, actions):
noises = (atanh(actions) - means) / (log_stds.exp() + 1e-8)
return calculate_log_pi(log_stds, noises, actions)
def calculate_log_pi(log_stds, noises, actions):
return ((-0.5 * noises.pow(2) - log_stds).sum(dim=-1, keepdim=True) - 0.5 *
np.log(2 * np.pi) * log_stds.size(-1) - torch.log(1 - actions.pow(2) + 1e-6).sum(dim=-1, keepdim=True))