-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.py
388 lines (299 loc) · 12.4 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"""Time-series Generative Adversarial Networks (TimeGAN) Codebase.
Reference: Jinsung Yoon, Daniel Jarrett, Mihaela van der Schaar,
"Time-series Generative Adversarial Networks,"
Neural Information Processing Systems (NeurIPS), 2019.
Paper link: https://papers.nips.cc/paper/8789-time-series-generative-adversarial-networks
Last updated Date: April 24th 2020
Code author: Jinsung Yoon (jsyoon0823@gmail.com)
-----------------------------
predictive_metrics.py
Note: Use post-hoc RNN to classify original data and synthetic data
Output: discriminative score (np.abs(classification accuracy - 0.5))
"""
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.metrics import mean_absolute_error
def train_test_divide (data_x, data_x_hat, data_t, data_t_hat, train_rate = 0.8):
"""Divide train and test data for both original and synthetic data.
Args:
- data_x: original data
- data_x_hat: generated data
- data_t: original time
- data_t_hat: generated time
- train_rate: ratio of training data from the original data
"""
# Divide train/test index (original data)
no = len(data_x)
idx = np.random.permutation(no)
train_idx = idx[:int(no*train_rate)]
test_idx = idx[int(no*train_rate):]
train_x = [data_x[i] for i in train_idx]
test_x = [data_x[i] for i in test_idx]
train_t = [data_t[i] for i in train_idx]
test_t = [data_t[i] for i in test_idx]
# Divide train/test index (synthetic data)
no = len(data_x_hat)
idx = np.random.permutation(no)
train_idx = idx[:int(no*train_rate)]
test_idx = idx[int(no*train_rate):]
train_x_hat = [data_x_hat[i] for i in train_idx]
test_x_hat = [data_x_hat[i] for i in test_idx]
train_t_hat = [data_t_hat[i] for i in train_idx]
test_t_hat = [data_t_hat[i] for i in test_idx]
return train_x, train_x_hat, test_x, test_x_hat, train_t, train_t_hat, test_t, test_t_hat
def extract_time (data):
"""Returns Maximum sequence length and each sequence length.
Args:
- data: original data
Returns:
- time: extracted time information
- max_seq_len: maximum sequence length
"""
time = list()
max_seq_len = 0
for i in range(len(data)):
max_seq_len = max(max_seq_len, len(data[i][:,0]))
time.append(len(data[i][:,0]))
return time, max_seq_len
def rnn_cell(module_name, hidden_dim):
"""Basic RNN Cell.
Args:
- module_name: gru, lstm, or lstmLN
Returns:
- rnn_cell: RNN Cell
"""
assert module_name in ['gru','lstm','lstmLN']
# GRU
if (module_name == 'gru'):
rnn_cell = tf.nn.rnn_cell.GRUCell(num_units=hidden_dim, activation=tf.nn.tanh)
# LSTM
elif (module_name == 'lstm'):
rnn_cell = tf.contrib.rnn.BasicLSTMCell(num_units=hidden_dim, activation=tf.nn.tanh)
# LSTM Layer Normalization
elif (module_name == 'lstmLN'):
rnn_cell = tf.contrib.rnn.LayerNormBasicLSTMCell(num_units=hidden_dim, activation=tf.nn.tanh)
return rnn_cell
def random_generator (batch_size, z_dim, T_mb, max_seq_len):
"""Random vector generation.
Args:
- batch_size: size of the random vector
- z_dim: dimension of random vector
- T_mb: time information for the random vector
- max_seq_len: maximum sequence length
Returns:
- Z_mb: generated random vector
"""
Z_mb = list()
for i in range(batch_size):
temp = np.zeros([max_seq_len, z_dim])
temp_Z = np.random.uniform(0., 1, [T_mb[i], z_dim])
temp[:T_mb[i],:] = temp_Z
Z_mb.append(temp_Z)
return Z_mb
def batch_generator(data, time, batch_size):
"""Mini-batch generator.
Args:
- data: time-series data
- time: time information
- batch_size: the number of samples in each batch
Returns:
- X_mb: time-series data in each batch
- T_mb: time information in each batch
"""
no = len(data)
idx = np.random.permutation(no)
train_idx = idx[:batch_size]
X_mb = list(data[i] for i in train_idx)
T_mb = list(time[i] for i in train_idx)
return X_mb, T_mb
def MinMaxScaler(data):
"""Min Max normalizer.
Args:
- data: original data
Returns:
- norm_data: normalized data
"""
numerator = data - np.min(data, 0)
denominator = np.max(data, 0) - np.min(data, 0)
norm_data = numerator / (denominator + 1e-7)
return norm_data
def real_data_loading(ori_data, seq_len):
"""Load and preprocess real-world datasets.
Args:
- data_name: stock or energy
- seq_len: sequence length
Returns:
- data: preprocessed data.
"""
# Flip the data to make chronological data
ori_data = ori_data[::-1]
# Normalize the data
ori_data = MinMaxScaler(ori_data)
# Preprocess the dataset
temp_data = []
# Cut data by sequence length
for i in range(0, len(ori_data) - seq_len):
_x = ori_data[i:i + seq_len]
temp_data.append(_x)
# Mix the datasets (to make it similar to i.i.d)
idx = np.random.permutation(len(temp_data))
data = []
for i in range(len(temp_data)):
data.append(temp_data[idx[i]])
return data
def discriminative_score_metrics (ori_data, generated_data):
"""Use post-hoc RNN to classify original data and synthetic data
Args:
- ori_data: original data
- generated_data: generated synthetic data
Returns:
- discriminative_score: np.abs(classification accuracy - 0.5)
"""
# Initialization on the Graph
tf.reset_default_graph()
# Basic Parameters
no, seq_len, dim = np.asarray(ori_data).shape
# Set maximum sequence length and each sequence length
ori_time, ori_max_seq_len = extract_time(ori_data)
generated_time, generated_max_seq_len = extract_time(ori_data)
max_seq_len = max([ori_max_seq_len, generated_max_seq_len])
## Builde a post-hoc RNN discriminator network
# Network parameters
hidden_dim = int(dim/2)
iterations = 2000
batch_size = 128
# Input place holders
# Feature
X = tf.placeholder(tf.float32, [None, max_seq_len, dim], name = "myinput_x")
X_hat = tf.placeholder(tf.float32, [None, max_seq_len, dim], name = "myinput_x_hat")
T = tf.placeholder(tf.int32, [None], name = "myinput_t")
T_hat = tf.placeholder(tf.int32, [None], name = "myinput_t_hat")
# discriminator function
def discriminator (x, t):
"""Simple discriminator function.
Args:
- x: time-series data
- t: time information
Returns:
- y_hat_logit: logits of the discriminator output
- y_hat: discriminator output
- d_vars: discriminator variables
"""
with tf.variable_scope("discriminator", reuse = tf.AUTO_REUSE) as vs:
d_cell = tf.nn.rnn_cell.GRUCell(num_units=hidden_dim, activation=tf.nn.tanh, name = 'd_cell')
d_outputs, d_last_states = tf.nn.dynamic_rnn(d_cell, x, dtype=tf.float32, sequence_length = t)
y_hat_logit = tf.contrib.layers.fully_connected(d_last_states, 1, activation_fn=None)
y_hat = tf.nn.sigmoid(y_hat_logit)
d_vars = [v for v in tf.all_variables() if v.name.startswith(vs.name)]
return y_hat_logit, y_hat, d_vars
y_logit_real, y_pred_real, d_vars = discriminator(X, T)
y_logit_fake, y_pred_fake, _ = discriminator(X_hat, T_hat)
# Loss for the discriminator
d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = y_logit_real,
labels = tf.ones_like(y_logit_real)))
d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = y_logit_fake,
labels = tf.zeros_like(y_logit_fake)))
d_loss = d_loss_real + d_loss_fake
# optimizer
d_solver = tf.train.AdamOptimizer().minimize(d_loss, var_list = d_vars)
## Train the discriminator
# Start session and initialize
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# Train/test division for both original and generated data
train_x, train_x_hat, test_x, test_x_hat, train_t, train_t_hat, test_t, test_t_hat = \
train_test_divide(ori_data, generated_data, ori_time, generated_time)
# Training step
for itt in range(iterations):
# Batch setting
X_mb, T_mb = batch_generator(train_x, train_t, batch_size)
X_hat_mb, T_hat_mb = batch_generator(train_x_hat, train_t_hat, batch_size)
# Train discriminator
_, step_d_loss = sess.run([d_solver, d_loss],
feed_dict={X: X_mb, T: T_mb, X_hat: X_hat_mb, T_hat: T_hat_mb})
## Test the performance on the testing set
y_pred_real_curr, y_pred_fake_curr = sess.run([y_pred_real, y_pred_fake],
feed_dict={X: test_x, T: test_t, X_hat: test_x_hat, T_hat: test_t_hat})
y_pred_final = np.squeeze(np.concatenate((y_pred_real_curr, y_pred_fake_curr), axis = 0))
y_label_final = np.concatenate((np.ones([len(y_pred_real_curr),]), np.zeros([len(y_pred_fake_curr),])), axis = 0)
# Compute the accuracy
acc = accuracy_score(y_label_final, (y_pred_final>0.5))
discriminative_score = np.abs(0.5-acc)
return discriminative_score
def predictive_score_metrics (ori_data, generated_data):
"""Report the performance of Post-hoc RNN one-step ahead prediction.
Args:
- ori_data: original data
- generated_data: generated synthetic data
Returns:
- predictive_score: MAE of the predictions on the original data
"""
# Initialization on the Graph
tf.reset_default_graph()
# Basic Parameters
no, seq_len, dim = np.asarray(ori_data).shape
# Set maximum sequence length and each sequence length
ori_time, ori_max_seq_len = extract_time(ori_data)
generated_time, generated_max_seq_len = extract_time(ori_data)
max_seq_len = max([ori_max_seq_len, generated_max_seq_len])
## Builde a post-hoc RNN predictive network
# Network parameters
hidden_dim = int(dim/2)
iterations = 5000
batch_size = 128
# Input place holders
X = tf.placeholder(tf.float32, [None, max_seq_len-1, dim-1], name = "myinput_x")
T = tf.placeholder(tf.int32, [None], name = "myinput_t")
Y = tf.placeholder(tf.float32, [None, max_seq_len-1, 1], name = "myinput_y")
# Predictor function
def predictor (x, t):
"""Simple predictor function.
Args:
- x: time-series data
- t: time information
Returns:
- y_hat: prediction
- p_vars: predictor variables
"""
with tf.variable_scope("predictor", reuse = tf.AUTO_REUSE) as vs:
p_cell = tf.nn.rnn_cell.GRUCell(num_units=hidden_dim, activation=tf.nn.tanh, name = 'p_cell')
p_outputs, p_last_states = tf.nn.dynamic_rnn(p_cell, x, dtype=tf.float32, sequence_length = t)
y_hat_logit = tf.contrib.layers.fully_connected(p_outputs, 1, activation_fn=None)
y_hat = tf.nn.sigmoid(y_hat_logit)
p_vars = [v for v in tf.all_variables() if v.name.startswith(vs.name)]
return y_hat, p_vars
y_pred, p_vars = predictor(X, T)
# Loss for the predictor
p_loss = tf.losses.absolute_difference(Y, y_pred)
# optimizer
p_solver = tf.train.AdamOptimizer().minimize(p_loss, var_list = p_vars)
## Training
# Session start
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# Training using Synthetic dataset
for itt in range(iterations):
# Set mini-batch
idx = np.random.permutation(len(generated_data))
train_idx = idx[:batch_size]
X_mb = list(generated_data[i][:-1,:(dim-1)] for i in train_idx)
T_mb = list(generated_time[i]-1 for i in train_idx)
Y_mb = list(np.reshape(generated_data[i][1:,(dim-1)],[len(generated_data[i][1:,(dim-1)]),1]) for i in train_idx)
# Train predictor
_, step_p_loss = sess.run([p_solver, p_loss], feed_dict={X: X_mb, T: T_mb, Y: Y_mb})
## Test the trained model on the original data
idx = np.random.permutation(len(ori_data))
train_idx = idx[:no]
X_mb = list(ori_data[i][:-1,:(dim-1)] for i in train_idx)
T_mb = list(ori_time[i]-1 for i in train_idx)
Y_mb = list(np.reshape(ori_data[i][1:,(dim-1)], [len(ori_data[i][1:,(dim-1)]),1]) for i in train_idx)
# Prediction
pred_Y_curr = sess.run(y_pred, feed_dict={X: X_mb, T: T_mb})
# Compute the performance in terms of MAE
MAE_temp = 0
for i in range(no):
MAE_temp = MAE_temp + mean_absolute_error(Y_mb[i], pred_Y_curr[i,:,:])
predictive_score = MAE_temp / no
return predictive_score