microzero-play / model.py
ARotting's picture
Publish Playable neural MCTS Tic-Tac-Toe agent
c7928dd verified
Raw
History Blame Contribute Delete
839 Bytes
from __future__ import annotations
import torch
from torch import nn
class MicroZeroNet(nn.Module):
def __init__(self) -> None:
super().__init__()
self.trunk = nn.Sequential(
nn.Linear(9, 64),
nn.LayerNorm(64),
nn.SiLU(),
nn.Linear(64, 64),
nn.SiLU(),
)
self.policy = nn.Linear(64, 9)
self.value = nn.Sequential(
nn.Linear(64, 32),
nn.SiLU(),
nn.Linear(32, 1),
nn.Tanh(),
)
def forward(self, board: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
hidden = self.trunk(board)
return self.policy(hidden), self.value(hidden).squeeze(-1)
def parameter_count(model: nn.Module) -> int:
return sum(parameter.numel() for parameter in model.parameters())