2020-03-28 07:27:18 +08:00
|
|
|
import torch
|
|
|
|
import numpy as np
|
|
|
|
from torch import nn
|
|
|
|
import torch.nn.functional as F
|
|
|
|
|
|
|
|
|
|
|
|
class Actor(nn.Module):
|
2020-07-09 22:57:01 +08:00
|
|
|
"""For advanced usage (how to customize the network), please refer to
|
|
|
|
:ref:`build_the_network`.
|
|
|
|
"""
|
|
|
|
|
2020-07-22 14:42:08 +08:00
|
|
|
def __init__(self, preprocess_net, action_shape, hidden_layer_size=128):
|
2020-03-28 07:27:18 +08:00
|
|
|
super().__init__()
|
|
|
|
self.preprocess = preprocess_net
|
2020-07-22 14:42:08 +08:00
|
|
|
self.last = nn.Linear(hidden_layer_size, np.prod(action_shape))
|
2020-03-28 07:27:18 +08:00
|
|
|
|
|
|
|
def forward(self, s, state=None, info={}):
|
2020-07-22 14:42:08 +08:00
|
|
|
r"""s -> Q(s, \*)"""
|
2020-03-28 07:27:18 +08:00
|
|
|
logits, h = self.preprocess(s, state)
|
2020-03-31 16:13:53 +08:00
|
|
|
logits = F.softmax(self.last(logits), dim=-1)
|
2020-03-28 07:27:18 +08:00
|
|
|
return logits, h
|
|
|
|
|
|
|
|
|
|
|
|
class Critic(nn.Module):
|
2020-07-09 22:57:01 +08:00
|
|
|
"""For advanced usage (how to customize the network), please refer to
|
|
|
|
:ref:`build_the_network`.
|
|
|
|
"""
|
|
|
|
|
2020-07-22 14:42:08 +08:00
|
|
|
def __init__(self, preprocess_net, hidden_layer_size=128):
|
2020-03-28 07:27:18 +08:00
|
|
|
super().__init__()
|
|
|
|
self.preprocess = preprocess_net
|
2020-07-22 14:42:08 +08:00
|
|
|
self.last = nn.Linear(hidden_layer_size, 1)
|
2020-03-28 07:27:18 +08:00
|
|
|
|
2020-07-09 22:57:01 +08:00
|
|
|
def forward(self, s, **kwargs):
|
2020-07-22 14:42:08 +08:00
|
|
|
"""s -> V(s)"""
|
2020-07-09 22:57:01 +08:00
|
|
|
logits, h = self.preprocess(s, state=kwargs.get('state', None))
|
2020-03-28 07:27:18 +08:00
|
|
|
logits = self.last(logits)
|
|
|
|
return logits
|
|
|
|
|
|
|
|
|
|
|
|
class DQN(nn.Module):
|
2020-07-09 22:57:01 +08:00
|
|
|
"""For advanced usage (how to customize the network), please refer to
|
|
|
|
:ref:`build_the_network`.
|
|
|
|
"""
|
2020-03-28 07:27:18 +08:00
|
|
|
|
|
|
|
def __init__(self, h, w, action_shape, device='cpu'):
|
|
|
|
super(DQN, self).__init__()
|
|
|
|
self.device = device
|
|
|
|
|
2020-03-28 12:03:49 +08:00
|
|
|
self.conv1 = nn.Conv2d(4, 16, kernel_size=5, stride=2)
|
2020-03-28 07:27:18 +08:00
|
|
|
self.bn1 = nn.BatchNorm2d(16)
|
|
|
|
self.conv2 = nn.Conv2d(16, 32, kernel_size=5, stride=2)
|
|
|
|
self.bn2 = nn.BatchNorm2d(32)
|
|
|
|
self.conv3 = nn.Conv2d(32, 32, kernel_size=5, stride=2)
|
|
|
|
self.bn3 = nn.BatchNorm2d(32)
|
|
|
|
|
|
|
|
def conv2d_size_out(size, kernel_size=5, stride=2):
|
|
|
|
return (size - (kernel_size - 1) - 1) // stride + 1
|
|
|
|
|
|
|
|
convw = conv2d_size_out(conv2d_size_out(conv2d_size_out(w)))
|
|
|
|
convh = conv2d_size_out(conv2d_size_out(conv2d_size_out(h)))
|
|
|
|
linear_input_size = convw * convh * 32
|
2020-03-28 12:03:49 +08:00
|
|
|
self.fc = nn.Linear(linear_input_size, 512)
|
|
|
|
self.head = nn.Linear(512, action_shape)
|
2020-03-28 07:27:18 +08:00
|
|
|
|
|
|
|
def forward(self, x, state=None, info={}):
|
2020-07-22 14:42:08 +08:00
|
|
|
r"""x -> Q(x, \*)"""
|
2020-03-28 07:27:18 +08:00
|
|
|
if not isinstance(x, torch.Tensor):
|
2020-07-09 22:57:01 +08:00
|
|
|
x = torch.tensor(x, device=self.device, dtype=torch.float32)
|
2020-03-28 07:27:18 +08:00
|
|
|
x = x.permute(0, 3, 1, 2)
|
|
|
|
x = F.relu(self.bn1(self.conv1(x)))
|
|
|
|
x = F.relu(self.bn2(self.conv2(x)))
|
|
|
|
x = F.relu(self.bn3(self.conv3(x)))
|
2020-03-28 12:03:49 +08:00
|
|
|
x = self.fc(x.reshape(x.size(0), -1))
|
|
|
|
return self.head(x), state
|