self_example/pytorch_example/RUL/baseModel/loss/Evaluate.py

79 lines
1.8 KiB
Python

# -*- encoding:utf-8 -*-
'''
@Author : dingjiawen
@Date : 2023/11/23 16:32
@Usage :
@Desc :
'''
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
def mae(y_true, y_predict):
return np.mean(np.mean(np.abs(y_true - y_predict), axis=-1))
def mape(y_true, y_predict):
return np.mean(np.mean(np.abs((y_true - y_predict) / y_true), axis=-1))
def score(y_true, y_predict):
Eri = y_predict - y_true
dw = np.log(0.5)
total = []
if len(y_true.shape) > 1:
b, c = y_true.shape
for i in range(b):
cList = []
for j in range(c):
if Eri[i, j] < 0:
cList.append(np.exp(-(Eri[i, j] / 13)) - 1)
else:
cList.append(np.exp((Eri[i, j] / 10)) - 1)
total.append(np.stack(cList))
total = np.stack(total, axis=0)
return np.mean(np.mean(total, axis=-1))
else:
b, = y_true.shape
for i in range(b):
if Eri[i] <= 0:
total.append(np.exp((-dw) * (Eri[i] / 5)))
else:
total.append(np.exp((dw) * (Eri[i] / 20)))
total = np.stack(total, axis=0)
return np.mean(total)
pass
def rmse(y_true, y_predict):
loss = np.sqrt(np.mean(np.mean((y_predict - y_true) ** 2, axis=-1)))
return loss
def getEvaluate(y_true, y_predict):
return [rmse(y_true, y_predict), mae(y_true, y_predict), mape(y_true, y_predict), score(y_true, y_predict)]
if __name__ == '__main__':
# a = torch.log(torch.tensor(0.5))
# print(a)
# x = torch.rand((32,))
# y = torch.rand((32,))
x = np.random.random(size=(32,))
y = np.random.random(size=(32,))
print(mae(x, y))
print(mape(x, y))
print(rmse(x, y))
print(score(x, y))