In this recipe, we will demonstrate how to leverage learning models to play games. In our example, we show how to apply a Deep-Q Network for playing with the Keras framework.
- Let's start with importing the necessary libraries, as follows:
import gym import random import numpy as np import matplotlib.pyplot as plt from collections import deque from keras.models import Sequential from keras.optimizers import Adam from keras.layers import Dense, Flatten from keras.layers.convolutional import Conv2D from keras import backend as K
- First, we will plot an example input image of the game:
Figure 11.1: Example input image of Breakout by OpenAI
env = gym.make('BreakoutDeterministic-v4') observation = env.reset() for i in range(3): # The ball is released after 2 frames if i > 1: print(observation.shape) plt.imshow(observation) plt.show() # Get the next observation observation, _, _, _ ...