Let's solve the Pong environment using a CNN-based DQN as follows:
- 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")
- Then, we specify three actions:
>>> ACTIONS = [0, 2, 3] >>> n_action = 3
These actions are not moving, moving up, and moving down.
- 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, ...