-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
197 lines (160 loc) · 7.91 KB
/
agent.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
import torch
import numpy as np
import random
from networks import Actor, Critic
import torch.optim as optim
import torch.nn.functional as F
class REDQ_Agent():
"""Interacts with and learns from the environment."""
def __init__(self,
state_size,
action_size,
replay_buffer,
batch_size,
random_seed,
lr,
hidden_size,
gamma,
tau,
device,
action_prior="uniform",
N=2,
M=2,
G=1):
"""Initialize an Agent object
Args:
state_size (int): State size
action_size (int): Action size
replay_buffer: Experience Replay Buffer
batch_size: Batch size when learning
random_seed (int): Random seed
lr (float): Learning rate
hidden_size (int): Number of hidden units per layer
gamma (float): Discount factor
tau (float): Tau, soft-update parameter
device (torch device): Training Device cpu or cuda:0
action_prior (str, optional): Action prior. Defaults to "uniform".
N (int, optional): Number of Q-Network Ensemble. Defaults to 2.
M (int, optional): Number of the subset of the Critic for update calculation. Defaults to 2.
G (int, optional): Critic Updates per step, UTD-raio. Defaults to 1.
"""
self.state_size = state_size
self.action_size = action_size
self.seed = random.seed(random_seed)
self.gamma = gamma
self.tau = tau
self.batch_size = batch_size
self.target_entropy = -action_size # -dim(A)
self.log_alpha = torch.tensor([0.0], requires_grad=True)
self.alpha = self.log_alpha.exp().detach()
self.alpha_optimizer = optim.Adam(params=[self.log_alpha], lr=lr)
self._action_prior = action_prior
self.alphas = []
print("Using: ", device)
self.device = device
# REDQ parameter
self.N = N # number of critics in the ensemble
self.M = M # number of target critics that are randomly selected
self.G = G # Updates per step ~ UTD-ratio
# Actor Network
self.actor_local = Actor(state_size, action_size, random_seed, hidden_size=hidden_size).to(device)
self.actor_optimizer = optim.Adam(self.actor_local.parameters(), lr=lr)
# Critic Network (w/ Target Network)
self.critics = []
self.target_critics = []
parameter = []
for i in range(self.N):
critic = Critic(state_size, action_size, i, hidden_size=hidden_size).to(device)
self.critics.append(critic)
parameter += list(critic.parameters())
target = Critic(state_size, action_size, i, hidden_size=hidden_size).to(device)
self.target_critics.append(target)
self.optimizer = optim.Adam(params=parameter, lr=lr)
# Replay memory
self.memory = replay_buffer
def step(self, state, action, reward, next_state, done):
"""Save experience in replay memory, and use random sample from buffer to learn."""
# Save experience / reward
self.memory.add(state, action, reward, next_state, done)
# Learn, if enough samples are available in memory
for update in range(self.G):
if len(self.memory) > self.batch_size:
experiences = self.memory.sample()
self.learn(update, experiences)
def act(self, state):
"""Returns actions for given state as per current policy."""
state = torch.from_numpy(state).float().to(self.device)
action, _, _ = self.actor_local.sample(state)
return action.detach().cpu()[0]
def eval(self, state):
state = torch.from_numpy(state).float().to(self.device)
_, _ , action = self.actor_local.sample(state)
return action.detach().cpu()[0]
def learn(self, step, experiences):
"""Updates actor, critics and entropy_alpha parameters using given batch of experience tuples.
Q_targets = r + γ * (min_critic_target(next_state, actor_target(next_state)) - α *log_pi(next_action|next_state))
Critic_loss = MSE(Q, Q_target)
Actor_loss = α * log_pi(a|s) - Q(s,a)
where:
actor_target(state) -> action
critic_target(state, action) -> Q-value
Params
======
experiences (Tuple[torch.Tensor]): tuple of (s, a, r, s', done) tuples
"""
states, actions, rewards, next_states, dones = experiences
# sample target critics
idx = np.random.choice(len(self.critics), self.M, replace=False) # replace=False so that not picking the same idx twice
# ---------------------------- update critic ---------------------------- #
with torch.no_grad():
# Get predicted next-state actions and Q values from target models
next_action, next_log_prob, _ = self.actor_local.sample(next_states)
# TODO: make this variable for possible more than two target critics
Q_target1_next = self.target_critics[idx[0]](next_states, next_action.squeeze(0))
Q_target2_next = self.target_critics[idx[1]](next_states, next_action.squeeze(0))
# take the min of both critics for updating
Q_target_next = torch.min(Q_target1_next, Q_target2_next) - self.alpha.to(self.device) * next_log_prob
Q_targets = rewards.cpu() + (self.gamma * (1 - dones.cpu()) * Q_target_next.cpu())
# Compute critic losses and update critics
Combined_loss = 0
for critic, target in zip(self.critics, self.target_critics):
Q = critic(states, actions).cpu()
Q_loss = 0.5*F.mse_loss(Q, Q_targets)
Combined_loss = Combined_loss + Q_loss
# Update critic
self.optimizer.zero_grad()
Combined_loss.backward()
self.optimizer.step()
# soft update of the targets
for critic, target in zip(self.critics, self.target_critics):
self.soft_update(critic, target)
# ---------------------------- update actor ---------------------------- #
if step == self.G-1:
actions_pred, log_prob, _ = self.actor_local.sample(states)
# TODO: make this variable for possible more than two critics
Q1 = self.critics[idx[0]](states, actions_pred.squeeze(0)).cpu()
Q2 = self.critics[idx[0]](states, actions_pred.squeeze(0)).cpu()
Q = torch.min(Q1,Q2)
actor_loss = (self.alpha * log_prob.cpu() - Q ).mean()
# Optimize the actor loss
self.actor_optimizer.zero_grad()
actor_loss.backward()
self.actor_optimizer.step()
# Compute alpha loss
alpha_loss = - (self.log_alpha.exp() * (log_prob.cpu() + self.target_entropy).detach().cpu()).mean()
self.alpha_optimizer.zero_grad()
alpha_loss.backward()
self.alpha_optimizer.step()
self.alpha = self.log_alpha.exp().detach()
self.alphas.append(self.alpha.detach())
def soft_update(self, local_model, target_model):
"""Soft update model parameters.
θ_target = τ*θ_local + (1 - τ)*θ_target
Params
======
local_model: PyTorch model (weights will be copied from)
target_model: PyTorch model (weights will be copied to)
tau (float): interpolation parameter
"""
for target_param, local_param in zip(target_model.parameters(), local_model.parameters()):
target_param.data.copy_(self.tau*local_param.data + (1.0-self.tau)*target_param.data)