average loss
This commit is contained in:
parent
1288565cff
commit
f8c8884ce9
|
|
@ -1,3 +1,4 @@
|
||||||
|
from turtle import forward
|
||||||
import torch
|
import torch
|
||||||
import torch.nn as nn
|
import torch.nn as nn
|
||||||
|
|
||||||
|
|
@ -22,7 +23,33 @@ class mean_absolute_error(nn.Module):
|
||||||
def forward(self, prediction:torch.Tensor, target: torch.Tensor):
|
def forward(self, prediction:torch.Tensor, target: torch.Tensor):
|
||||||
|
|
||||||
return self.loss_fun(prediction, target)
|
return self.loss_fun(prediction, target)
|
||||||
|
|
||||||
|
class Avergeloss(nn.Module):
|
||||||
|
|
||||||
|
def __init__(self,losses):
|
||||||
|
|
||||||
|
self.valid_losses = nn.ModuleList()
|
||||||
|
for loss in losses:
|
||||||
|
loss = self.validate_loss(loss)
|
||||||
|
self.valid_losses.append(loss())
|
||||||
|
|
||||||
|
|
||||||
|
def validate_loss(self,loss:str):
|
||||||
|
if loss not in LOSS_MAP.keys():
|
||||||
|
raise ValueError()
|
||||||
|
else:
|
||||||
|
return LOSS_MAP[loss]
|
||||||
|
|
||||||
|
def forward(self,prediction:torch.Tensor, target:torch.Tensor):
|
||||||
|
loss = 0.0
|
||||||
|
for loss_fun in self.valid_losses:
|
||||||
|
loss += loss_fun(prediction, target)
|
||||||
|
|
||||||
|
return loss
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
LOSS_MAP = {"mea":mean_absolute_error, "mse": mean_squared_error}
|
LOSS_MAP = {"mea":mean_absolute_error, "mse": mean_squared_error}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue