Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
TensorFlow 2 Reinforcement Learning Cookbook

You're reading from  TensorFlow 2 Reinforcement Learning Cookbook

Product type Book
Published in Jan 2021
Publisher Packt
ISBN-13 9781838982546
Pages 472 pages
Edition 1st Edition
Languages
Author (1):
Palanisamy P Palanisamy P
Profile icon Palanisamy P

Table of Contents (11) Chapters

Preface 1. Chapter 1: Developing Building Blocks for Deep Reinforcement Learning Using Tensorflow 2.x 2. Chapter 2: Implementing Value-Based, Policy-Based, and Actor-Critic Deep RL Algorithms 3. Chapter 3: Implementing Advanced RL Algorithms 4. Chapter 4: Reinforcement Learning in the Real World – Building Cryptocurrency Trading Agents 5. Chapter 5: Reinforcement Learning in the Real World – Building Stock/Share Trading Agents 6. Chapter 6: Reinforcement Learning in the Real World – Building Intelligent Agents to Complete Your To-Dos 7. Chapter 7: Deploying Deep RL Agents to the Cloud 8. Chapter 8: Distributed Training for Accelerated Development of Deep RL Agents 9. Chapter 9: Deploying Deep RL Agents on Multiple Platforms 10. Other Books You May Enjoy

Working with OpenAI Gym for RL training environments

This recipe provides a quick run-through for getting up and running with OpenAI Gym environments. The Gym environment and the interface provide a platform for training RL agents and is the most widely used and accepted RL environment interface.

Getting ready

We will be needing the full installation of OpenAI Gym to be able to use the available environments. Please follow the Gym installation steps listed at https://github.com/openai/gym#id5.

As a minimum, you should execute the following command:

pip install gym[atari]

How to do it…

Let's start by picking an environment and exploring the Gym interface. You may already be familiar with the basic function calls to create a Gym environment from the previous recipes.

Your steps should be formatted like so:

  1. Let's first explore the list of environments in Gym:
    #!/usr/bin/env python
    from gym import envs
    env_names = [spec.id for spec in envs.registry.all()]
    for name in sorted(env_names):
        print(name)
  2. This script will print the names of all the environments available through your Gym installation, sorted alphabetically. You can run this script using the following command to see the names of the environments that are installed and available in your system. You should see a long list of environments listed. The first few are shown in the following screenshot for your reference:
    Figure 1.9 – List of environments available using the openai-gym package

    Figure 1.9 – List of environments available using the openai-gym package

    Let's now see how we can run one of the Gym environments.

  3. The following script will let you explore any of the available Gym environments:
    #!/usr/bin/env python
    import gym
    import sys
    def run_gym_env(argv):
        env = gym.make(argv[1]) # Name of the environment 
                                # supplied as 1st argument
        env.reset()
        for _ in range(int(argv[2])):
            env.render()
            env.step(env.action_space.sample())
        env.close()
    if __name__ == "__main__":
        run_gym_env(sys.argv)
  4. You can save the preceding script to run_gym_env.py and run the script like this:
    (tf2rl-cookbook) praveen@g5: ~/tf2rl-cookbook/ch1/src$python run_gym_env.py Alien-v4 1000

    The script will render the Alien-v4 environment, which should look like the following screenshot:

Figure 1.10 – Sample output of run_gym_env.py with Alien-v4 1000 as the arguments

Figure 1.10 – Sample output of run_gym_env.py with Alien-v4 1000 as the arguments

Tip

You can change Alien-v4 to any of the available Gym environments listed in the previous step.

How it works…

A summary of how the Gym environments work is presented in the following table:

Table 1.1 – Summary of the Gym environment interface

Table 1.1 – Summary of the Gym environment interface

See also

You can find more information on OpenAI Gym here: http://gym.openai.com/.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}