Stomach_Cancer_Pytorch/Model_Loss/Loss.py

17 lines
584 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_New = torch.as_tensor(outputs, dtype=torch.float32)
labels_New = torch.as_tensor(labels, dtype=torch.float32)
loss = functional.cross_entropy(outputs_New, labels_New)
return torch.as_tensor(loss, dtype = torch.float32)