|
| 1 | +import numpy as np |
| 2 | +import torch |
| 3 | +import torch.nn as nn |
| 4 | +import torch.nn.functional as F |
| 5 | + |
| 6 | + |
| 7 | +# state-value network for pacman |
| 8 | +class PacmanNet(nn.Module): |
| 9 | + def __init__(self, input_channel_num, num_actions, extra_size): |
| 10 | + super().__init__() |
| 11 | + self.channels = input_channel_num |
| 12 | + self.embeddings = nn.ModuleList( |
| 13 | + [nn.Embedding(9, 16) for _ in range(input_channel_num)]) |
| 14 | + self.conv1 = nn.Conv2d(64, 64, kernel_size=8, stride=4) |
| 15 | + self.conv2 = nn.Conv2d(64, 64, kernel_size=4, stride=2) |
| 16 | + self.bn = nn.BatchNorm2d(64) |
| 17 | + self.conv3 = nn.Conv2d(64, 64, kernel_size=3, stride=2) |
| 18 | + |
| 19 | + self.encoder = nn.Linear(extra_size, 64) |
| 20 | + |
| 21 | + self.fc1 = nn.Linear(64, 32) |
| 22 | + self.fc2 = nn.Linear(32, num_actions) |
| 23 | + |
| 24 | + def forward(self, x, y): |
| 25 | + B, C, H, W = x.shape |
| 26 | + embedded_channels = [] |
| 27 | + for i in range(self.channels): |
| 28 | + flattened_channel = x[:, i, :, :].view(B, -1).long() |
| 29 | + embedded_channel = self.embeddings[i](flattened_channel) |
| 30 | + embedded_channel = embedded_channel.view( |
| 31 | + B, 16, H, W) |
| 32 | + embedded_channels.append(embedded_channel) |
| 33 | + # Concatenate along the channel dimension |
| 34 | + x = torch.cat(embedded_channels, dim=1) |
| 35 | + |
| 36 | + x = F.relu(self.conv1(x)) |
| 37 | + x = F.relu(self.conv2(x)) |
| 38 | + x = self.bn(x) |
| 39 | + x = F.relu(self.conv3(x)) |
| 40 | + y = F.sigmoid(self.encoder(y)) |
| 41 | + # print(x.shape) |
| 42 | + x = x.view(x.size(0), -1) |
| 43 | + x = F.relu(self.fc1(x+y)) |
| 44 | + return self.fc2(x) |
| 45 | + |
| 46 | + |
| 47 | +# state-value network for ghost |
| 48 | +class GhostNet(nn.Module): |
| 49 | + def __init__(self, input_channel_num, num_actions, extra_size): |
| 50 | + super().__init__() |
| 51 | + self.channels = input_channel_num |
| 52 | + self.embeddings = nn.ModuleList( |
| 53 | + [nn.Embedding(9, 16) for _ in range(input_channel_num)]) |
| 54 | + |
| 55 | + self.conv1 = nn.Conv2d(64, 64, kernel_size=8, stride=4) |
| 56 | + self.conv2 = nn.Conv2d(64, 64, kernel_size=4, stride=2) |
| 57 | + self.bn = nn.BatchNorm2d(64) |
| 58 | + self.conv3 = nn.Conv2d(64, 64, kernel_size=3, stride=2) |
| 59 | + |
| 60 | + self.encoder = nn.Linear(extra_size, 64) |
| 61 | + |
| 62 | + self.fc1 = nn.Linear(64, 32) |
| 63 | + self.fc2 = nn.Linear(32, num_actions*3) |
| 64 | + |
| 65 | + def forward(self, x, y): |
| 66 | + B, C, H, W = x.shape |
| 67 | + embedded_channels = [] |
| 68 | + for i in range(self.channels): |
| 69 | + flattened_channel = x[:, i, :, :].view(B, -1).long() |
| 70 | + embedded_channel = self.embeddings[i](flattened_channel) |
| 71 | + embedded_channel = embedded_channel.view( |
| 72 | + B, 16, H, W) |
| 73 | + embedded_channels.append(embedded_channel) |
| 74 | + # Concatenate along the channel dimension |
| 75 | + x = torch.cat(embedded_channels, dim=1) |
| 76 | + x = F.relu(self.conv1(x)) |
| 77 | + x = F.relu(self.conv2(x)) |
| 78 | + x = self.bn(x) |
| 79 | + x = F.relu(self.conv3(x)) |
| 80 | + # print(x.shape) |
| 81 | + |
| 82 | + y = F.sigmoid(self.encoder(y)) |
| 83 | + |
| 84 | + x = x.view(x.size(0), -1) |
| 85 | + x = F.relu(self.fc1(x+y)) |
| 86 | + return self.fc2(x).view(-1, 3, 5) |
| 87 | + |
| 88 | + |
| 89 | +# test the shape of the output |
| 90 | +if __name__ == "__main__": |
| 91 | + rand_input = torch.rand(1, 4, 38, 38) |
| 92 | + extra_input = torch.rand(1, 10) |
| 93 | + pacman_net = PacmanNet(4, 5, 10) |
| 94 | + res = pacman_net(rand_input, extra_input) |
| 95 | + print(res.shape) |
| 96 | + |
| 97 | + ghost_net = GhostNet(4, 5, 10) |
| 98 | + res = ghost_net(rand_input, extra_input) |
| 99 | + print(res.shape) |
0 commit comments