forked from ccf-ai-infra/GPUCodeForces
42 lines
1014 B
Python
42 lines
1014 B
Python
import torch
|
|
import torch.nn as nn
|
|
import torch.nn.functional as F
|
|
|
|
class Model(nn.Module):
|
|
"""
|
|
Sigmoid Derivative算子实现
|
|
"""
|
|
def __init__(self):
|
|
super(Model, self).__init__()
|
|
|
|
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
"""
|
|
计算Sigmoid Derivative
|
|
|
|
Args:
|
|
x: 输入张量 [任意形状]
|
|
|
|
Returns:
|
|
sigmoid'(x) = sigmoid(x) * (1 - sigmoid(x))
|
|
"""
|
|
# 方法1: 直接计算
|
|
sigmoid_x = torch.sigmoid(x)
|
|
return sigmoid_x * (1 - sigmoid_x)
|
|
|
|
# 方法2: 数值稳定版本(用于对比)
|
|
# return torch.nn.functional.softplus(-x).exp() * torch.sigmoid(x).square()
|
|
|
|
# 适合测试的数据规模
|
|
batch_size = 256
|
|
channels = 64
|
|
height = 32
|
|
width = 32
|
|
|
|
def get_inputs():
|
|
# 生成测试数据,包含正负值
|
|
x = torch.randn(batch_size, channels, height, width) * 2.0 # 扩大范围
|
|
return [x]
|
|
|
|
def get_init_inputs():
|
|
return [] # 无需初始化
|