-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_run.py
160 lines (135 loc) · 6.62 KB
/
eval_run.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import warnings
warnings.filterwarnings("ignore")
import numpy as np
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices("GPU")
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
except RuntimeError as e:
print(e)
from eval_utils.metric_utils import display_scores
from eval_utils.discriminative_metric import discriminative_score_metrics
from eval_utils.predictive_metric import predictive_score_metrics
import torch
from eval_utils.MMD import BMMD, cross_correlation_distribution, BMMD_Naive, VDS_Naive
def unnormalize_to_zero_to_one(x):
return (x + 1) * 0.5
def discriminative_score(dataname, iterations, fake_data, length=24):
if dataname == "energy":
ori_data = np.load(f"./OUTPUT/samples/energy_norm_truth_{length}_train.npy")
elif dataname == "stock":
ori_data = np.load(f"./OUTPUT/samples/stock_norm_truth_{length}_train.npy")
elif dataname == "sine":
ori_data = np.load(f"./OUTPUT/samples/sine_ground_truth_{length}_train.npy")
elif dataname == "fmri":
ori_data = np.load(f"./OUTPUT/samples/fmri_norm_truth_{length}_train.npy")
elif dataname == "mujoco":
ori_data = np.load(f"./OUTPUT/samples/mujoco_norm_truth_{length}_train.npy")
else:
raise NotImplementedError(f"Unkown dataname: {dataname}")
fake_data = unnormalize_to_zero_to_one(fake_data)
fake_data = fake_data[: ori_data.shape[0]]
discriminative_score = []
print(f"Fake data:", "min ", fake_data.min(), ", max ", fake_data.max())
print(f"Real data:", "min ", ori_data.min(), ", max ", ori_data.max())
for i in range(iterations):
print(i)
temp_disc, fake_acc, real_acc, values = discriminative_score_metrics(
ori_data[:], fake_data[: ori_data.shape[0]]
)
discriminative_score.append(temp_disc)
print(f"Iter {i}: ", temp_disc, ",", fake_acc, ",", real_acc, "\n")
print(f"{dataname}:")
display_scores(discriminative_score)
return values
def predictive_score(dataname, iterations, fake_data, length=24):
if dataname == "energy":
ori_data = np.load(f"./OUTPUT/samples/energy_norm_truth_{length}_train.npy")
elif dataname == "stock":
ori_data = np.load(f"./OUTPUT/samples/stock_norm_truth_{length}_train.npy")
elif dataname == "sine":
ori_data = np.load(f"./OUTPUT/samples/sine_ground_truth_{length}_train.npy")
elif dataname == "fmri":
ori_data = np.load(f"./OUTPUT/samples/fmri_norm_truth_{length}_train.npy")
elif dataname == "mujoco":
ori_data = np.load(f"./OUTPUT/samples/mujoco_norm_truth_{length}_train.npy")
else:
raise NotImplementedError(f"Unkown dataname: {dataname}")
fake_data = unnormalize_to_zero_to_one(fake_data)
fake_data = fake_data[: ori_data.shape[0]]
predictive_score = []
print(f"Fake data:", "min ", fake_data.min(), ", max ", fake_data.max())
print(f"Real data:", "min ", ori_data.min(), ", max ", ori_data.max())
for i in range(iterations):
temp_pred = predictive_score_metrics(ori_data, fake_data[: ori_data.shape[0]])
predictive_score.append(temp_pred)
print(i, " epoch: ", temp_pred, "\n")
print(f"{dataname}:")
display_scores(predictive_score)
def BMMD_score(dataname, fake_data, length=24):
if dataname == "energy":
ori_data = np.load(f"./OUTPUT/samples/energy_norm_truth_{length}_train.npy")
elif dataname == "stock":
ori_data = np.load(f"./OUTPUT/samples/stock_norm_truth_{length}_train.npy")
elif dataname == "sine":
ori_data = np.load(f"./OUTPUT/samples/sine_ground_truth_{length}_train.npy")
elif dataname == "fmri":
ori_data = np.load(f"./OUTPUT/samples/fmri_norm_truth_{length}_train.npy")
elif dataname == "mujoco":
ori_data = np.load(f"./OUTPUT/samples/mujoco_norm_truth_{length}_train.npy")
else:
raise NotImplementedError(f"Unkown dataname: {dataname}")
fake_data = unnormalize_to_zero_to_one(fake_data)
fake_data = fake_data[: ori_data.shape[0]]
ori_data = torch.tensor(ori_data).float()
fake_data = torch.tensor(fake_data).float()
ori_data = cross_correlation_distribution(ori_data).unsqueeze(-1).permute(1, 0, 2)
fake_data = cross_correlation_distribution(fake_data).unsqueeze(-1).permute(1, 0, 2)
assert ori_data.shape == fake_data.shape
mmd_loss = BMMD(ori_data, fake_data, "rbf").mean()
print(f"{dataname}:", mmd_loss)
def BMMD_score_naive(dataname, fake_data, length=24):
if dataname == "energy":
ori_data = np.load(f"./OUTPUT/samples/energy_norm_truth_{length}_train.npy")
elif dataname == "stock":
ori_data = np.load(f"./OUTPUT/samples/stock_norm_truth_{length}_train.npy")
elif dataname == "sine":
ori_data = np.load(f"./OUTPUT/samples/sine_ground_truth_{length}_train.npy")
elif dataname == "fmri":
ori_data = np.load(f"./OUTPUT/samples/fmri_norm_truth_{length}_train.npy")
elif dataname == "mujoco":
ori_data = np.load(f"./OUTPUT/samples/mujoco_norm_truth_{length}_train.npy")
else:
raise NotImplementedError(f"Unkown dataname: {dataname}")
fake_data = unnormalize_to_zero_to_one(fake_data)
fake_data = fake_data[: ori_data.shape[0]]
ori_data = torch.tensor(ori_data).float()
fake_data = torch.tensor(fake_data).float()
ori_data = cross_correlation_distribution(ori_data).unsqueeze(-1)
fake_data = cross_correlation_distribution(fake_data).unsqueeze(-1)
assert ori_data.shape == fake_data.shape
mmd_loss = BMMD_Naive(ori_data, fake_data, "rbf").mean()
print(f"{dataname} FDDS Score:", mmd_loss)
def VDS_score(dataname, fake_data, length=24):
if dataname == "energy":
ori_data = np.load(f"./OUTPUT/samples/energy_norm_truth_{length}_train.npy")
elif dataname == "stock":
ori_data = np.load(f"./OUTPUT/samples/stock_norm_truth_{length}_train.npy")
elif dataname == "sine":
ori_data = np.load(f"./OUTPUT/samples/sine_ground_truth_{length}_train.npy")
elif dataname == "fmri":
ori_data = np.load(f"./OUTPUT/samples/fmri_norm_truth_{length}_train.npy")
elif dataname == "mujoco":
ori_data = np.load(f"./OUTPUT/samples/mujoco_norm_truth_{length}_train.npy")
else:
raise NotImplementedError(f"Unkown dataname: {dataname}")
fake_data = unnormalize_to_zero_to_one(fake_data)
fake_data = fake_data[: ori_data.shape[0]]
ori_data = torch.tensor(ori_data).float()
fake_data = torch.tensor(fake_data).float()
vds_score = VDS_Naive(ori_data, fake_data, "rbf").mean()
print(f"{dataname} VDS Score:", vds_score)