How to do it...

We solve the continuous Mountain Car problem using continuous A2C as follows:

  1. Import all the necessary packages and create a continuous Mountain Car instance:
>>> import gym>>> import torch>>> import torch.nn as nn>>> import torch.nn.functional as F>>> env = gym.make('MountainCarContinuous-v0')
  1. Let's start with the actor-critic neural network model:
>>> class ActorCriticModel(nn.Module): ...     def __init__(self, n_input, n_output, n_hidden): ...         super(ActorCriticModel, self).__init__() ...         self.fc = nn.Linear(n_input, n_hidden) ...         self.mu = nn.Linear(n_hidden, n_output) ...         self.sigma = nn.Linear(n_hidden, n_output) ...         self.value = nn.Linear(n_hidden, 1) ...         self.distribution = torch.distributions.Normal ... ... def forward(self, ...

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.