How to do it...

Let's solve the Pong environment using a CNN-based DQN as follows:

  1. Import the necessary modules and create a Pong environment:
 >>> import gym >>> import torch >>> import random >>> from collections import deque >>> import copy >>> from torch.autograd import Variable >>> import torch.nn as nn >>> import torch.nn.functional as F >>> env = gym.envs.make("PongDeterministic-v4")
  1. Then, we specify three actions:
 >>> ACTIONS = [0, 2, 3] >>> n_action = 3

These actions are not moving, moving up, and moving down.

  1. Now, we develop an image processing function to downsize the image:
 >>> import torchvision.transforms as T >>> from PIL import Image >>> image_size = 84 >>> transform = T.Compose([T.ToPILImage(), ... T.Resize((image_size, ...

Get PyTorch 1.x Reinforcement Learning Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.