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 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.