/
akp1n
/
ADVML
Обзор
Документация
Войти
/
akp1n
/
ADVML
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
task1/model.py
50 строк
2 KB
Artemiy
fix
24 фев 2025, 20:45
24 фев 2025, 20:45
57cd6cd
Код
Авторство
О чём код?
from collections.abc import Callable import torch import torch.nn as nn from torch import Tensor class Net(nn.Module): def __init__(self, input_channels: int, output_size: int, activation_function: nn.Module): super(Net, self).__init__() self.layers = nn.Sequential( nn.Conv2d(input_channels, 128, kernel_size=3, stride=1, padding=1), activation_function, nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1), activation_function ) self.fc = nn.Linear(64 * 28 * 28, output_size) def forward(self, x: Tensor) -> Tensor: x = self.layers(x) x = torch.flatten(x, start_dim=1) x = self.fc(x) return x class MyCustomActivation(nn.Module): def __init__(self, beta: float = 1.0): super(MyCustomActivation, self).__init__() self.beta = nn.Parameter(torch.tensor(beta)) def forward(self, x: torch.Tensor) -> torch.Tensor: return x * torch.sigmoid(self.beta * x) class MyCustomReLU(nn.Module): def forward(self, x: Tensor) -> Tensor: return torch.maximum(x, torch.tensor(0.0)) class MyCustomGELU(nn.Module): def forward(self, x: Tensor) -> Tensor: phi_x = 0.5 * (1 + torch.erf(x / torch.sqrt(torch.tensor(2)))) return x * phi_x class MyCustomSwish(nn.Module): def forward(self, x: Tensor) -> Tensor: sigma_x = 1 / (1 + torch.exp(-x)) return x * sigma_x class MyCustomSoftplus(nn.Module): def forward(self, x: Tensor) -> Tensor: return torch.log(1 + torch.exp(x))