|
| 1 | +import torch |
| 2 | +import numpy as np |
| 3 | +from torch import nn |
| 4 | +import torch.nn.functional as F |
| 5 | +from copy import deepcopy |
| 6 | +import sys |
| 7 | + |
| 8 | +from tianshou.data import to_torch |
| 9 | + |
| 10 | + |
| 11 | +class PPO_Extractor(nn.Module): |
| 12 | + def __init__(self, device="cpu", **kargs): |
| 13 | + super().__init__() |
| 14 | + self.device = device |
| 15 | + hidden_size = kargs["hidden_size"] |
| 16 | + fc_size = kargs["fc_size"] |
| 17 | + self.cnn_shape = kargs["cnn_shape"] |
| 18 | + |
| 19 | + self.rnn = nn.GRU(64, hidden_size, batch_first=True) |
| 20 | + self.rnn2 = nn.GRU(64, hidden_size, batch_first=True) |
| 21 | + self.dnn = nn.Sequential(nn.Linear(2, 64), nn.ReLU(),) |
| 22 | + self.cnn = nn.Sequential(nn.Conv1d(self.cnn_shape[1], 3, 3), nn.ReLU(),) |
| 23 | + self.raw_fc = nn.Sequential(nn.Linear((self.cnn_shape[0] - 2) * 3, 64), nn.ReLU(),) |
| 24 | + |
| 25 | + self.fc = nn.Sequential( |
| 26 | + nn.Linear(hidden_size * 2, hidden_size), nn.ReLU(), nn.Linear(hidden_size, 32), nn.ReLU(), |
| 27 | + ) |
| 28 | + |
| 29 | + def forward(self, inp): |
| 30 | + inp = to_torch(inp, dtype=torch.float32, device=self.device) |
| 31 | + # inp = torch.from_numpy(inp).to(torch.device('cpu')) |
| 32 | + seq_len = inp[:, -1].to(torch.long) |
| 33 | + batch_size = inp.shape[0] |
| 34 | + raw_in = inp[:, : 6 * 240] |
| 35 | + raw_in = torch.cat((torch.zeros_like(inp[:, : 6 * 30]), raw_in), dim=-1) |
| 36 | + raw_in = raw_in.reshape(-1, 30, 6).transpose(1, 2) |
| 37 | + dnn_in = inp[:, -19:-1].reshape(batch_size, -1, 2) |
| 38 | + cnn_out = self.cnn(raw_in).view(batch_size, 9, -1) |
| 39 | + assert not torch.isnan(cnn_out).any() |
| 40 | + rnn_in = self.raw_fc(cnn_out) |
| 41 | + assert not torch.isnan(rnn_in).any() |
| 42 | + rnn2_in = self.dnn(dnn_in) |
| 43 | + assert not torch.isnan(rnn2_in).any() |
| 44 | + rnn2_out = self.rnn2(rnn2_in)[0] |
| 45 | + assert not torch.isnan(rnn2_out).any() |
| 46 | + rnn_out = self.rnn(rnn_in)[0] |
| 47 | + assert not torch.isnan(rnn_out).any() |
| 48 | + rnn_out = rnn_out[torch.arange(rnn_out.size(0)), seq_len] |
| 49 | + rnn2_out = rnn2_out[torch.arange(rnn2_out.size(0)), seq_len] |
| 50 | + # dnn_out = self.dnn(dnn_in) |
| 51 | + fc_in = torch.cat((rnn_out, rnn2_out), dim=-1) |
| 52 | + self.feature = self.fc(fc_in) |
| 53 | + return self.feature |
| 54 | + |
| 55 | + |
| 56 | +class PPO_Actor(nn.Module): |
| 57 | + def __init__(self, extractor, out_shape, device=torch.device("cpu"), **kargs): |
| 58 | + super().__init__() |
| 59 | + self.extractor = extractor |
| 60 | + self.layer_out = nn.Sequential(nn.Linear(32, out_shape), nn.Softmax(dim=-1)) |
| 61 | + self.device = device |
| 62 | + |
| 63 | + def forward(self, obs, state=None, info={}): |
| 64 | + self.feature = self.extractor(obs) |
| 65 | + assert not (torch.isnan(self.feature).any() | torch.isinf(self.feature).any()), f"{self.feature}" |
| 66 | + out = self.layer_out(self.feature) |
| 67 | + return out, state |
| 68 | + |
| 69 | + |
| 70 | +class PPO_Critic(nn.Module): |
| 71 | + def __init__(self, extractor, out_shape, device=torch.device("cpu"), **kargs): |
| 72 | + super().__init__() |
| 73 | + self.extractor = extractor |
| 74 | + self.value_out = nn.Linear(32, 1) |
| 75 | + self.device = device |
| 76 | + |
| 77 | + def forward(self, obs, state=None, info={}): |
| 78 | + self.feature = self.extractor(obs) |
| 79 | + return self.value_out(self.feature).squeeze(dim=-1) |
0 commit comments