-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddpg.py
More file actions
213 lines (175 loc) · 7.39 KB
/
ddpg.py
File metadata and controls
213 lines (175 loc) · 7.39 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from typing import Sequence
import flax
import flax.linen as nn
import jax
import jax.numpy as jnp
import optax
from flax.training.train_state import TrainState
class Actor(nn.Module):
"""Actor network that learns a deterministic policy."""
n_hidden_units: Sequence[int]
n_action: int
action_min: jnp.ndarray # (1 x n_action)
action_max: jnp.ndarray # (1 x n_action)
activation: nn.activation = nn.relu
@nn.compact
def __call__(self, state):
"""
Args:
state: (batch_size x n_state)
Returns
action: (batch_size x n_action)
"""
x = self.activation(nn.Dense(self.n_hidden_units[0])(state))
for n in self.n_hidden_units[1:]:
x = self.activation(nn.Dense(n)(x))
# output layer
x = nn.Dense(self.n_action)(x)
action = nn.tanh(
x
) # last activation is a tanh to squash the action outputs to between -1 and 1
# scale the action outputs to the limits given by the environment
# such that -1 corresponds to the minimum action and 1 corresponds to maximum action
action = 0.5 * (
action * (self.action_max - self.action_min)
+ (self.action_max + self.action_min)
)
return action
class Critic(nn.Module):
"""Critic network that learns the action-value function."""
n_hidden_units: Sequence[int]
activation: nn.activation = nn.relu
@nn.compact
def __call__(self, state, action):
"""
Args:
state: (batch_size x n_state)
action: (batch_size x n_action)
Returns
action-value function q(s, a): (batch_size x 1)
"""
x = jnp.concatenate([state, action], axis=-1)
for n in self.n_hidden_units:
x = self.activation(nn.Dense(n)(x))
# output layer
q = nn.Dense(1)(x)
return q
class TrainState(TrainState):
target_params: flax.core.FrozenDict # add the parameters of the target networks
class DDPG:
"""Deep Deterministic Policy Gradient."""
def __init__(self, config, env):
# training parameters
self.actor_lr = config.train.actor_learning_rate
self.critic_lr = config.train.critic_learning_rate
self.gamma = config.train.discount_factor
self.tau = config.train.soft_update_rate
# create actor network
self.actor = Actor(
config.network.actor_hidden_units,
n_action=env.action_size,
action_min=env.action_low,
action_max=env.action_high,
activation=getattr(nn, config.network.activation_function),
)
# create critic network
self.critic = Critic(
config.network.critic_hidden_units,
activation=getattr(nn, config.network.activation_function),
)
self.actor.apply = jax.jit(self.actor.apply)
self.critic.apply = jax.jit(self.critic.apply)
self.update_actor = jax.jit(self.update_actor)
self.update_critic = jax.jit(self.update_critic)
self.update_target_networks = jax.jit(self.update_target_networks)
def initial_network_state(self, key, state, action):
key, actor_key, critic_key = jax.random.split(key, 3)
# initialize the parameters of the actor network
actor_state = TrainState.create(
apply_fn=self.actor.apply,
params=self.actor.init(actor_key, state),
target_params=self.actor.init(actor_key, state),
tx=optax.adam(learning_rate=self.actor_lr),
)
# initialize the parameters of the critic network
critic_state = TrainState.create(
apply_fn=self.critic.apply,
params=self.critic.init(critic_key, state, action),
target_params=self.critic.init(critic_key, state, action),
tx=optax.adam(learning_rate=self.critic_lr),
)
return actor_state, critic_state
def update_critic(
self,
actor_state: TrainState,
critic_state: TrainState,
state: jnp.ndarray,
action: jnp.ndarray,
next_state: jnp.ndarray,
reward: jnp.float64,
terminated: jnp.ndarray,
):
# get next action using the target policy on the next state
next_action = self.actor.apply(actor_state.target_params, next_state)
# get the q value at the next state (at time t+1) using the target policy and target q network
next_q_target = self.critic.apply(
critic_state.target_params, next_state, next_action
).reshape(-1)
# we need to reshape because next_q_target is batch_size x 1, whereas reward and terminated are batch_size,
# compute the target value for the current state (this is denoted by y in the paper)
# the Bellman update target estimated using the reward r at time t and q value at time t+1
# i.e., one-step temporal difference
# when an episode ends due to termination we don’t bootstrap,
# when it ends due to truncation, we bootstrap.
target = reward + self.gamma * (1 - terminated) * next_q_target
# define the q loss
# we want the gradient of the MSE loss from learning q network with respect to its parameters
def compute_q_loss(params):
# get the q value of the learning q network
q = self.critic.apply(params, state, action).reshape(-1)
# compute mean squared error
mse = jnp.mean((q - target) ** 2)
return mse
# compute gradient
q_loss, q_grad = jax.value_and_grad(compute_q_loss)(critic_state.params)
# update critic
critic_state = critic_state.apply_gradients(grads=q_grad)
return critic_state, q_loss
def update_actor(
self, actor_state: TrainState, critic_state: TrainState, state: jnp.ndarray
):
# define the policy loss that will give us the policy gradient,
# once differentiated with respect to the parameters of the learning actor network
# this is given by the the chain rule
# dJ/dtheta^mu = mean (dq/da * dmu/dtheta^mu))
# we choose this loss as the negative average q values
# because we want a policy that maximizes q
def compute_policy_loss(params):
J = -jnp.mean(
self.critic.apply(
critic_state.params, state, self.actor.apply(params, state)
)
)
return J
# compute gradient
policy_loss, policy_grad = jax.value_and_grad(compute_policy_loss)(
actor_state.params
)
# update actor
actor_state = actor_state.apply_gradients(grads=policy_grad)
return actor_state, policy_loss
def update_target_networks(self, actor_state: TrainState, critic_state: TrainState):
# update the target networks' parameters such that they are updated as a moving average
# theta = tau * theta_new + (1-tau) * theta
# this enables stability during learning
critic_state = critic_state.replace(
target_params=optax.incremental_update(
critic_state.params, critic_state.target_params, self.tau
)
)
actor_state = actor_state.replace(
target_params=optax.incremental_update(
actor_state.params, actor_state.target_params, self.tau
)
)
return actor_state, critic_state