File size: 527 Bytes
5fed0fc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import torch
import torch.nn.functional as F
def cross_entropy(logits: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
"""
Baseline cross entropy implementation using PyTorch.
Args:
logits: Input tensor of shape (M, N) - logits for M samples and N classes
targets: Input tensor of shape (M,) - target class indices
Returns:
Output tensor of shape (M,) - negative log-likelihood loss for each sample
"""
return F.cross_entropy(logits, targets, reduction='none')
|