GPUCodeForces/S1/15/hingeembeddingloss_torch.py

28 lines
714 B
Python

# hingeembeddingloss_torch.py
import torch
import torch.nn as nn
import torch.nn.functional as F
BATCH_SIZE = 4096
FEATURE_DIM = 512
MARGIN = 1.0
class Model(nn.Module):
def __init__(self):
super().__init__()
self.criterion = nn.HingeEmbeddingLoss(margin=MARGIN, reduction='mean')
def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
return self.criterion(input, target)
def get_inputs():
input_scores = torch.randn(BATCH_SIZE, FEATURE_DIM, dtype=torch.float32)
target = torch.randint(0, 2, (BATCH_SIZE, FEATURE_DIM), dtype=torch.float32)
target[target == 0] = -1
return [input_scores, target]
def get_init_inputs():
return []