GPUCodeForces/S1/gsd123_#52/ModReLU_torch.py

29 lines
600 B
Python

import torch
import torch.nn as nn
class Model(nn.Module):
def __init__(self, b=0.0):
super().__init__()
self.b = b
def forward(self, x: torch.Tensor) -> torch.Tensor:
term_abs = x.abs()
condition = term_abs + self.b >= 0.0
y_active = (term_abs + self.b) * torch.sign(x)
return torch.where(condition, y_active, torch.zeros_like(x))
batch_size = 128
feature_dim = 512
def get_inputs():
x = torch.randn(batch_size, feature_dim, dtype=torch.float32)
return [x]
def get_init_inputs():
return [0.0]