💻 源码
"""
y_pred.argmax(dim=-1)
(y_pred_class == y).sum().item()

"""

import torch

torch.manual_seed(5)
# 1.创建logits, (batch_size, 4)
logits = torch.randn(size=(8, 4))
print(f"logits:{logits},shape: {logits.shape}")
# 2.经过argmax(dim=-1),(batch_size,)
y_pred_class = logits.argmax(dim=-1)
print(f"y_pred_class:{y_pred_class},shape: {y_pred_class.shape}")
# 3.创建y_true, (batch_size,)
y_true = torch.ones((8,))
print(f"y_true:{y_true},shape: {y_true.shape}")

# 4.计算预测正确的数量(y_pred_class == y).sum().item()
print((y_pred_class == y_true).sum().item())