We develop deep Q-learning using double DQNs as follows:
- 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")
- 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 = ...