How to do it...

We develop deep Q-learning using double DQNs as follows:

  1. Import the necessary modules and create a Mountain Car environment:
 >>> import gym >>> import torch >>> from collections import deque >>> import random >>> import copy >>> from torch.autograd import Variable >>> env = gym.envs.make("MountainCar-v0")
  1. To incorporate the target network in the experience replay phase, we first initialize it in the __init__ method of the DQN class:
>>> class DQN(): ...     def __init__(self, n_state, n_action,                      n_hidden=50, lr=0.05): ...         self.criterion = torch.nn.MSELoss() ...         self.model = torch.nn.Sequential( ...                         torch.nn.Linear(n_state, n_hidden), ...                         torch.nn.ReLU(), ...                         torch.nn.Linear(n_hidden, n_action) ...                 ) ... self.optimizer = ...

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.