File size: 1,199 Bytes
626ab93 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | from __future__ import annotations
import torch
from torch import nn
class TuringSurrogate(nn.Module):
def __init__(self) -> None:
super().__init__()
self.network = nn.Sequential(
nn.Conv2d(4, 32, kernel_size=3, padding=1, padding_mode="circular"),
nn.SiLU(),
nn.Conv2d(32, 32, kernel_size=3, padding=1, padding_mode="circular"),
nn.SiLU(),
nn.Conv2d(32, 16, kernel_size=3, padding=1, padding_mode="circular"),
nn.SiLU(),
nn.Conv2d(16, 2, kernel_size=1),
)
def forward(
self,
state: torch.Tensor,
feed: torch.Tensor,
kill: torch.Tensor,
) -> torch.Tensor:
batch, _, height, width = state.shape
parameters = torch.stack([feed, kill], dim=1)
parameter_fields = parameters[:, :, None, None].expand(
batch,
2,
height,
width,
)
delta = self.network(torch.cat([state, parameter_fields], dim=1))
return torch.clamp(state + delta, 0, 1)
def parameter_count(model: nn.Module) -> int:
return sum(parameter.numel() for parameter in model.parameters())
|