How to do it...

We develop the Q-function approximator based on the linear function as follows:

  1. Import all the necessary packages:
>>> import torch>>> from torch.autograd import Variable>>> import math

The variable wraps a tensor and supports backpropagation.

  1. Then, start the __init__method of the linear function's Estimator class:
>>> class Estimator(): ...     def __init__(self, n_feat, n_state, n_action, lr=0.05): ...         self.w, self.b = self.get_gaussian_wb(n_feat, n_state) ...         self.n_feat = n_feat ...         self.models = [] ...         self.optimizers = [] ...         self.criterion = torch.nn.MSELoss() ...         for _ in range(n_action): ...             model = torch.nn.Linear(n_feat, 1) ...             self.models.append(model) ...             optimizer = torch.optim.SGD(model.parameters(), lr) ... ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.