This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gail.py
166 lines (156 loc) · 7.18 KB
/
gail.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
import numpy as np
import torch
import torch.nn.functional as F
from torch import nn
from torch.optim import Adam
# Generative Adversarial Imitation Learning
# https://arxiv.org/abs/1606.03476
class Actor(nn.Module):
def __init__(self, state_space, action_space, activation, log_std = 0):
super().__init__()
self.state_space = state_space
self.action_space = action_space
self.activaten = activation
self.log_std = log_std
self.l1 = nn.Linear(self.state_space, 128)
self.l2 = nn.Linear(128, 128)
self.l3 = nn.Linear(128, self.action_space)
self.log_std = nn.Parameter(torch.ones(1, action_space) * self.log_std)
def forward(self, state):
x = self.activation(self.l2(self.l1(states)))
mean = self.l3(x)
log_std = self.log_std.expand_as(mean)
std = torch.exp(log_std)
return mean, log_std, std
class Critic(nn.Module):
def __init__(self, state_space, activation):
super().__init__()
self.state_space = state_space
self.activation = activation
self.l1 = nn.Linear(state_space, 128)
self.l2 = nn.Linear(128, 128)
self.l3 = nn.Linear(128, 1)
def forward(self, states):
x = self.activation(self.l2(self.l1(states)))
value = self.l3(x)
return value
class Discriminator(nn.Module):
def __init__(self, state_space, action_space, activation):
super().__init__()
self.state_action_space = state_space + action_space
self.activation = activation
self.l1 = nn.Linear(self.state_action_space, 128)
self.l2 = nn.Linear(128, 128)
self.l3 = nn.Linear(128, 1)
def forward(self, states_actions):
x = self.l2(self.l1(states_actions))
x = self.activation(x)
probability = torch.sigmoid(self.l3(x))
return probability
class GAIL(object):
def __init__(self, env, activation, gamma, tau):
self.device = torch.device('cuda' if torch.cuda.is_available()
else 'cpu')
self.state_space = env.observation_space.shape[0]
self.action_space = env.action_space.n
self.activation = activation
self.gamma = gamma
self.tau = tau
self.clip_threshold = 1e-3
self.l2_reguralization = 1e-1
self.policy = Actor(self.state_space, self.action_space,
self.activation)
self.critic = Critic(self.state_space, self.activation).to(
self.device)
self.discriminator = Discriminator(self.state_space, self.action_space,
self.activation).to(self.device)
self.policy_optimizer = Adam(self.policy.parameters(), lr = 1e-3)
self.critic_optimizer = Adam(self.critic.parameters(), lr = 1e-3)
self.discriminator_optimizer = Adam(self.discriminator.parameters(),
lr = 1e-3)
self.discriminator_criterion = nn.BCELoss()
def select_action(self, state):
mean, _, std = self.policy.forward(state)
action = torch.nomal(mean, std)
return action
def compute_advantage(self, values, rewards, not_done):
batch_size = len(rewards)
advantages = torch.FloatTensor(batch_size).to(self.device)
value_old = 0.0
advantage_old = 0
for i in reversed(range(batch_size)):
delta = rewards[i] + self.gamma * value_old * not_done[i] - \
values.data[i]
advantages[i] = delta + self.gamma * self.tau * advantage_old * \
not_done[i]
value_old = values.data[i]
advantage_old = advantages[i]
returns = values + advantages
advantages = (advantages - advantages.mean()) / (advantages.std() +
self.noise)
return advantages, returns
def update_parameters(self, batch):
states = torch.from_numpy(np.stack(batch.state)).float().to(self.device)
actions = torch.from_numpy(np.stack(batch.action)).long().to(
self.device)
rewards = torch.from_numpy(np.stack(batch.reward)).float().to(
self.device)
not_done = torch.from_numpy(np.stack(batch.not_done)).float().to(
self.device)
with torch.no_grads():
values = self.critic(states)
mean, log_std, std = self.policy(states)
log_probabilities_target = compute_gaussian_log(
actions, mean, log_std, std)
advantages, returns = self.compute_advantage(values, rewards,
not_done, self.gamma, self.tau)
discriminator_optimizer.zero_grad()
generator_value = discriminator(torch.cat([states, actions], 1))
expert_value = discriminator(
torch.from_numpy(expert_trajectory).float())
generator_loss = generator_criterion(generator_value,
torch.ones((shape.states[0], 1)))
discriminator_loss = discriminator_criterion(generator_value,
torch.ones((states.shape[0], 1)).to(self.device)) + \
discriminator_criterion(expert_value,
torch.zeros((expert_trajectory.shape[0], 1)).to(self.device))
discriminator_loss.backward()
discriminator_optimizer.step()
batch_size = states.shape[0]
indices = np.arange(batch_size)
np.random.shuffle(indices)
indices = torch.LongTensor(indices).to(self.device)
states = states[indices].clone()
actions = actions[indices].clone()
returns = returns[indices].clone()
advantages = advantages[indices].clone()
log_probabilities_target = log_probabilities_target[indices].clone()
for i in range(int(math.ceil(batch_size / optimization_batch_size))):
indices = slice(i * optimization_batch_size,
min((i + 1) * optimization_batch_size, batch_size))
states_batch = states[indices]
action_batch = actions[indices]
returns_batch = returns[indices]
advantages_batch = advantages[indices]
log_probabilities_target_batch = log_probabilities_target[indices]
values_current = self.critic(states_batch)
values_loss = (values_current - returns_batch).pow(2).mean()
for param in critic.parameters():
values_loss += param.pow(2).sum() * l2_regularization
self.critic_optimizer.zero_grad()
self.values_loss.backward()
self.critic_optimizer.step()
action, mean, log_std, std = self.policy(states_batch)
log_probabilities = compute_gaussian_log(
actions, mean, log_std, std)
probabilities_ratio = torch.exp(log_probabilities -
log_probabilities_target_batch)
surrogate_loss1 = probabilities_ratio * advantages
surrogate_loss2 = torch.clamp(probabilities_ratio,
1.0 - self.clip_threshold, 1.0 + self.clip_threshold) * \
advantages_batch
policy_loss = -torch.tanh(surrogate_loss1, surrogate_loss2).mean()
policy_optimizer.zero_grad()
policy_loss.backward()
nn.utils.clip_grad_norm_(policy.parameters(), 40)
policy_optimizer.step()