GPUCodeForces/S1/uucoco_#24/LogMeanExp_torch.py

20 lines
523 B
Python

import torch
import torch.nn as nn
class Model(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x: torch.Tensor) -> torch.Tensor:
# LogMeanExp(x) = log(mean(exp(x))) = LogSumExp(x) - log(N)
return torch.logsumexp(x, dim=-1) - torch.log(torch.tensor(x.size(-1), dtype=x.dtype, device=x.device))
batch_size = 1024
feature_dim = 4096
def get_inputs():
x = torch.randn(batch_size, feature_dim, dtype=torch.float32)
return [x]
def get_init_inputs():
return []