|
|
|
|
|
|
|
|
|
|
from mlagents.trainers.torch.layers import linear_layer, Initialization, Swish |
|
|
|
|
|
|
|
|
|
|
|
import torch |
|
|
|
from torch import nn |
|
|
|
|
|
|
|
|
|
|
def __init__(self, input_size: int, normalize: bool = False): |
|
|
|
self.normalizer: Optional[Normalizer] = None |
|
|
|
super().__init__() |
|
|
|
|
|
|
|
self.normalizer: Optional[Normalizer] = None |
|
|
|
super().__init__() |
|
|
|
if normalize: |
|
|
|
self.normalizer = Normalizer(input_size) |
|
|
|
|
|
|
|
|
|
|
return self.seq_layers(inputs) |
|
|
|
return inputs |
|
|
|
|
|
|
|
def copy_normalization(self, other_input: "VectorInput") -> None: |
|
|
|
if self.normalizer is not None and other_input.normalizer is not None: |
|
|
|
|
|
|
if self.normalizer is not None: |
|
|
|
self.normalizer.update(inputs) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SimpleVisualEncoder(nn.Module): |
|
|
|
|
|
|
nn.Conv2d(16, 32, [4, 4], [2, 2]), |
|
|
|
nn.LeakyReLU(), |
|
|
|
) |
|
|
|
self.dense = nn.Sequential( |
|
|
|
linear_layer( |
|
|
|
self.final_flat, |
|
|
|
self.h_size, |
|
|
|
kernel_init=Initialization.KaimingHeNormal, |
|
|
|
kernel_gain=1.0, |
|
|
|
), |
|
|
|
nn.LeakyReLU(), |
|
|
|
) |
|
|
|
|
|
|
|
def output_size(self) -> int: |
|
|
|
return self.final_flat |
|
|
|
def forward(self, visual_obs: torch.Tensor) -> None: |
|
|
|
def forward(self, visual_obs: torch.Tensor) -> torch.Tensor: |
|
|
|
hidden = self.dense(hidden) |
|
|
|
def __init__(self, height, width, initial_channels, output_size): |
|
|
|
def __init__( |
|
|
|
self, height: int, width: int, initial_channels: int, output_size: int |
|
|
|
): |
|
|
|
super().__init__() |
|
|
|
self.h_size = output_size |
|
|
|
conv_1_hw = conv_output_shape((height, width), 8, 4) |
|
|
|
|
|
|
nn.Conv2d(64, 64, [3, 3], [1, 1]), |
|
|
|
nn.LeakyReLU(), |
|
|
|
) |
|
|
|
self.dense = nn.Sequential( |
|
|
|
linear_layer( |
|
|
|
self.final_flat, |
|
|
|
self.h_size, |
|
|
|
kernel_init=Initialization.KaimingHeNormal, |
|
|
|
kernel_gain=1.0, |
|
|
|
), |
|
|
|
nn.LeakyReLU(), |
|
|
|
) |
|
|
|
|
|
|
|
def output_size(self) -> int: |
|
|
|
return self.final_flat |
|
|
|
def forward(self, visual_obs: torch.Tensor) -> None: |
|
|
|
def forward(self, visual_obs: torch.Tensor) -> torch.Tensor: |
|
|
|
hidden = self.dense(hidden) |
|
|
|
return hidden |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ResNetVisualEncoder(nn.Module): |
|
|
|
def __init__(self, height, width, initial_channels, final_hidden): |
|
|
|
def __init__(self, height: int, width: int, initial_channels: int): |
|
|
|
super().__init__() |
|
|
|
n_channels = [16, 32, 32] # channel for each stack |
|
|
|
n_blocks = 2 # number of residual blocks |
|
|
|
|
|
|
layers.append(ResNetBlock(channel)) |
|
|
|
last_channel = channel |
|
|
|
layers.append(Swish()) |
|
|
|
self.dense = linear_layer( |
|
|
|
n_channels[-1] * height * width, |
|
|
|
final_hidden, |
|
|
|
kernel_init=Initialization.KaimingHeNormal, |
|
|
|
kernel_gain=1.0, |
|
|
|
) |
|
|
|
self._output_size = n_channels[-1] * height * width |
|
|
|
def forward(self, visual_obs): |
|
|
|
def output_size(self) -> int: |
|
|
|
return self._output_size |
|
|
|
|
|
|
|
def forward(self, visual_obs: torch.Tensor) -> torch.Tensor: |
|
|
|
return torch.relu(self.dense(before_out)) |
|
|
|
return torch.relu(before_out) |