-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.py
executable file
·67 lines (51 loc) · 2.12 KB
/
metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python
# -*- coding: utf-8 -*-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Author: Zhongsheng Chen
# Date: 05/09/2020
# Copyright: Copyright 2020, Beijing University of Chemical Technology
# License: The MIT License (MIT)
# Email: zschen@mail.buct.edu.cn
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
import numpy as np
from scipy.stats import multivariate_normal
def gaussian_NLPD(y_real, y_pred, cov):
nll = -np.mean(
[multivariate_normal.logpdf(x=y_real[i], mean=y_pred[i], cov=cov[i]) for i in range(len(y_pred))])
return nll
def Parzen_NLPD(yTrue, yPred, bw):
n_instances = yTrue.shape[0]
nlpd = np.zeros((n_instances))
for i in range(n_instances):
n_samples = yPred[i].shape[0]
yt = np.tile(yTrue[i], n_samples)
E = -0.5 * np.power((yt - yPred[i].flatten()) / bw, 2)
max_exp = np.max(E, axis=-1, keepdims=True)
max_exp_rep = np.tile(max_exp, n_samples)
exp_ = np.exp(E - max_exp_rep)
constant = 0.5 * np.log(2 * np.pi) + np.log(n_samples * bw)
nlpd[i] = -np.log(np.sum(exp_)) - max_exp + constant
return np.mean(nlpd)
def Parzen(regcgan, x, y, n_sampling=100, n_bands=100):
n_instance = x.shape[0]
ypred_list = []
for i in range(n_instance):
x_ = np.tile(x[i], (n_sampling, 1))
ypred_ = regcgan.predict(x_)
ypred_list.append(ypred_)
return min_Parzen_NLPD(y, np.array(ypred_list), n_bands)
def min_Parzen_NLPD(yTrue, yPred, n_bands=100):
windows = np.linspace(0.01, 5, n_bands)
nlpd = []
for bw in windows:
nlpd.append(Parzen_NLPD(yTrue, yPred, bw))
inx = np.argmin(np.asarray(nlpd))
return nlpd[inx], windows[inx], nlpd
def Parzen_test(regcgan, X, y, bw, n_sampling=10):
n_instance = X.shape[0]
ypred_list = []
for i in range(n_instance):
x_ = np.tile(X[i], (n_sampling, 1))
ypred_ = regcgan._make_predict(x_)
ypred_list.append(ypred_)
return Parzen_NLPD(y, np.array(ypred_list), bw)