Reinforcement Learning - Introducing Goal Oriented Intelligence

with deeplizard.

Replay Memory Explained - Experience for Deep Q-Network Training

November 3, 2018 by

Blog

Replay Memory explained

What’s up, guys? In this post, we’ll continue our discussion of deep Q-networks and focus in on an important technique called experience replay that is utilized during the training process of a DQN. So, let’s get to it!

Last time, we covered each piece of the architecture that makes up a typical deep Q-network. Now, before we can move on to discussing exactly how a DQN is trained, we're first going to explain the concepts of experience replay and replay memory.

Experience Replay and Replay Memory

With deep Q-networks, we often utilize this technique called experience replay during training. With experience replay, we store the agent’s experiences at each time step in a data set called the replay memory. We represent the agent’s experience at time \(t\) as \(e_t\).

At time \(t\), the agent's experience \(e_t\) is defined as this tuple:

$$e_t=(s_t,a_t,r_{t+1},s_{t+1})$$

This tuple contains the state of the environment \(s_t\), the action \(a_t\) taken from state \(s_t\), the reward \(r_{t+1}\) given to the agent at time \(t+1\) as a result of the previous state-action pair \((s_t,a_t)\), and the next state of the environment \(s_{t+1}\). This tuple indeed gives us a summary of the agent’s experience at time \(t\).

All of the agent's experiences at each time step over all episodes played by the agent are stored in the replay memory. Well actually, in practice, we’ll usually see the replay memory set to some finite size limit, \(N\), and therefore, it will only store the last \(N\) experiences.

This replay memory data set is what we’ll randomly sample from to train the network. The act of gaining experience and sampling from the replay memory that stores these experience is called experience replay.

Why use experience replay?

Why would we choose to train the network on random samples from replay memory, rather than just providing the network with the sequential experiences as they occur in the environment?

A key reason for using replay memory is to break the correlation between consecutive samples.

If the network learned only from consecutive samples of experience as they occurred sequentially in the environment, the samples would be highly correlated and would therefore lead to inefficient learning. Taking random samples from replay memory breaks this correlation.

Combining a deep Q-network with experience replay

Alright, we now have the idea of experience replay down. From last time, we should also have an understanding of a general deep Q-network architecture, the data that the network accepts, and the output from the network.

As a quick refresher, remember that the network is passed a state from the environment, and in turn, the network outputs the Q-value for each action that can be taken from that state.

Let’s now bring all of this information in together with experience replay to see how they fit in with each other.

Setting up

Before training starts, we first initialize the replay memory data set \(D\) to capacity \(N\). So, the replay memory \(D\) will hold \(N\) total experiences.

Next, we initialize the network with random weights. We've covered weight initialization in the Deep Learning Fundamentals series, so if you need a refresher on this topic, check that out. The exact same concepts we covered there applies for deep Q-network weight initialization.

Next, for each episode, we initialize the starting state of the episode. In our previous discussion, we talked about states, including the starting state, being a frame of raw pixels from a game screen as an example.

Gaining experience

Now, for each time step \(t\) within the episode, we either explore the environment and select a random action, or we exploit the environment and select the greedy action for the given state that gives the highest Q-value. Remember, this is the exploration-exploitation trade-off that we discussed in detail in a previous post.

We then execute the selected action \(a_t\) in an emulator. So, for example, if the selected action was to move right, then from an emulator where the actions were being executed in the actual game environment, the agent would actually move right. We then observe the reward \(r_{t+1}\) given for this action, and we also observe the next state of the environment, \(s_{t+1}\). We then store the entire experience tuple \(e_t=(s_t,a_t,r_{t+1},s_{t+1})\) in replay memory \(D\).

Wrapping up

Here's a summary of what we have so far:

  1. Initialize replay memory capacity.
  2. Initialize the network with random weights.
  3. For each episode:
    1. Initialize the starting state.
    2. For each time step:
      1. Select an action.
        • Via exploration or exploitation
      2. Execute selected action in an emulator.
      3. Observe reward and next state.
      4. Store experience in replay memory.

In the next post, we're going to discover how exactly we sample from replay memory during training, as well as all the other details we need to know about training a DQN. Thanks for contributing to collective intelligence, and I'll see ya in the next one!

Description

Welcome back to this series on reinforcement learning! In this video, we’ll continue our discussion of deep Q-networks. Before we can move on to discussing exactly how a DQN is trained, we're first going to explain the concepts of experience replay and replay memory, which are utilized during the training process. So, let’s get to it! Check out the corresponding blog and other resources for this video at: http://deeplizard.com/learn/video/Bcuj2fTH4_4 ❤️🦎 Special thanks to the following polymaths of the deeplizard hivemind: Ruicong Xie Support collective intelligence, and join the deeplizard hivemind! Jürgen Schmidhuber interview: https://youtu.be/zK_x3Ba2l5Q Follow deeplizard: YouTube: https://www.youtube.com/deeplizard Twitter: https://twitter.com/deeplizard Facebook: https://www.facebook.com/Deeplizard-145413762948316 Steemit: https://steemit.com/@deeplizard Instagram: https://www.instagram.com/deeplizard/ Pinterest: https://www.pinterest.com/deeplizard/ Check out products deeplizard suggests on Amazon: https://www.amazon.com/shop/deeplizard Get a free Audible 30-day trial and 2 free audio books with deeplizard’s link: https://amzn.to/2yoqWRn Support deeplizard by browsing with Brave: https://brave.com/dee530 Support deeplizard with crypto: Bitcoin: 1AFgm3fLTiG5pNPgnfkKdsktgxLCMYpxCN Litecoin: LTZ2AUGpDmFm85y89PFFvVR5QmfX6Rfzg3 Ether: 0x9105cd0ecbc921ad19f6d5f9dd249735da8269ef Recommended books on AI: The Most Human Human: What Artificial Intelligence Teaches Us About Being Alive: http://amzn.to/2GtjKqu Life 3.0: Being Human in the Age of Artificial Intelligence https://amzn.to/2H5Iau4 Playlists: Data Science - https://www.youtube.com/playlist?list=PLZbbT5o_s2xrth-Cqs_R9-us6IWk9x27z Machine Learning - https://www.youtube.com/playlist?list=PLZbbT5o_s2xq7LwI2y8_QtvuXZedL6tQU Keras - https://www.youtube.com/playlist?list=PLZbbT5o_s2xrwRnXk_yCPtnqqo4_u2YGL TensorFlow.js - https://www.youtube.com/playlist?list=PLZbbT5o_s2xr83l8w44N_g3pygvajLrJ- PyTorch - https://www.youtube.com/watch?v=v5cngxo4mIg&list=PLZbbT5o_s2xrfNyHZsM6ufI0iZENK9xgG Reinforcement Learning - https://www.youtube.com/playlist?list=PLZbbT5o_s2xoWNVdDudn51XM8lOuZ_Njv Music: Thinking Music by Kevin MacLeod Jarvic 8 by Kevin MacLeod YouTube: https://www.youtube.com/channel/UCSZXFhRIx6b0dFX3xS8L1yQ Website: http://incompetech.com/ Licensed under Creative Commons: By Attribution 3.0 License http://creativecommons.org/licenses/by/3.0/