We develop the Q-function approximator based on the linear function as follows:
- Import all the necessary packages:
>>> import torch>>> from torch.autograd import Variable>>> import math
The variable wraps a tensor and supports backpropagation.
- 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) ... ...