17 lines
630 B
Python
17 lines
630 B
Python
from torch import nn
|
|
from torch.nn import functional
|
|
import torch
|
|
|
|
|
|
class Entropy_Loss(nn.Module):
|
|
def __init__(self):
|
|
super(Entropy_Loss, self).__init__()
|
|
|
|
def forward(self, outputs, labels):
|
|
# 範例: 使用均方誤差作為損失計算
|
|
# outputs = torch.argmax(outputs, 1)
|
|
outputs = torch.tensor(outputs, dtype=torch.float32).clone().detach()
|
|
labels = torch.tensor(labels, dtype=torch.float32).clone().detach()
|
|
|
|
loss = functional.cross_entropy(outputs, labels)
|
|
return torch.tensor(loss, requires_grad = True).clone().detach().requires_grad_(True) |