DQN

This post documents my implementation of the Deep Q Network (DQN) algorithm.


A Deep Q Network implementation in tensorflow with target network & random experience replay. The code is tested with Gym’s discrete action space environment, CartPole-v0 on Colab.

Code on my Github

If Github is not loading the Jupyter notebook, a known Github issue, click here to view the notebook on Jupyter’s nbviewer.


Notations:

Model network = \(Q_{\theta}\)

Model parameter = \(\theta\)

Model network Q value = \(Q_{\theta}\) (s, a)

Target network = \(Q_{\phi}\)

Target parameter = \(\phi\)

Target network Q value = \(Q_{\phi}\) (\(s^{'}\), \(a^{'}\))


Equations:

TD target = r (s, a) \(+\) \(\gamma\) \(max_{a}\) \(Q_{\phi}\) (\(s^{'}\), \(a^{'}\))

TD error = (TD target) \(-\) (Model network Q value) = [r (s, a) \(+\) \(\gamma\) \(max_{a^{'}}\) \(Q_{\phi}\) (\(s^{'}\), \(a^{'}\))] \(-\) \(Q_{\theta}\) (s, a)


Key implementation details:

Update target parameter \(\phi\) with model parameter \(\theta\). Copy \(\theta\) to \(\phi\) with either soft or hard parameter update.

Hard parameter update:

with tf.variable_scope('hard_replace'):
  self.target_replace_hard = [t.assign(m) for t, m in zip(self.target_net_params, self.model_net_params)]   
# hard params replacement
if self.learn_step % self.tau_step == 0:
    self.sess.run(self.target_replace_hard)  
self.learn_step += 1

Soft parameter update: polyak \(\cdot\) \(\theta\) + (1 \(-\) polyak) \(\cdot\) \(\phi\)

with tf.variable_scope('soft_replace'):            
  self.target_replace_soft = [t.assign(self.polyak * m + (1 - self.polyak) * t)
                              for t, m in zip(self.target_net_params, self.model_net_params)]   

Stop TD target from contributing to gradient computation:

# exclude td_target in gradient computation
td_target = tf.stop_gradient(td_target)

Tensorflow graph:

image


References:

Human-level control through deep reinforcement learning (Mnih et al., 2015)



2020

PBT for MARL

46 minute read

My attempt to implement a water down version of PBT (Population based training) for MARL (Multi-agent reinforcement learning).

Back to top ↑

2019

.bash_profile for Mac

15 minute read

This post demonstrates how to create customized functions to bundle commands in a .bash_profile file on Mac.

DPPO distributed tensorflow

72 minute read

This post documents my implementation of the Distributed Proximal Policy Optimization (Distributed PPO or DPPO) algorithm. (Distributed continuous version)

A3C distributed tensorflow

27 minute read

This post documents my implementation of the A3C (Asynchronous Advantage Actor Critic) algorithm (Distributed discrete version).

Distributed Tensorflow

78 minute read

This post demonstrates a simple usage example of distributed Tensorflow with Python multiprocessing package.

N-step targets

79 minute read

This post documents my implementation of the N-step Q-values estimation algorithm.

Dueling DDQN with PER

53 minute read

This post documents my implementation of the Dueling Double Deep Q Network with Priority Experience Replay (Duel DDQN with PER) algorithm.

Dueling DDQN

25 minute read

This post documents my implementation of the Dueling Double Deep Q Network (Dueling DDQN) algorithm.

DDQN

31 minute read

This post documents my implementation of the Double Deep Q Network (DDQN) algorithm.

DQN

24 minute read

This post documents my implementation of the Deep Q Network (DQN) algorithm.

Back to top ↑