-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.py
224 lines (183 loc) · 7.15 KB
/
utils.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
import numpy as np
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
# taken from the original paper https://github.com/jsyoon0823/TimeGAN/blob/master/utils.py
def sine_data_generation (no, seq_len, dim):
"""Sine data generation.
Args:
- no: the number of samples
- seq_len: sequence length of the time-series
- dim: feature dimensions
Returns:
- data: generated data
"""
# Initialize the output
data = list()
# Generate sine data
for i in range(no):
# Initialize each time-series
temp = list()
# For each feature
for k in range(dim):
# Randomly drawn frequency and phase
freq = np.random.uniform(0, 0.1)
phase = np.random.uniform(0, 0.1)
# Generate sine signal based on the drawn frequency and phase
temp_data = [np.sin(freq * j + phase) for j in range(seq_len)]
temp.append(temp_data)
# Align row/column
temp = np.transpose(np.asarray(temp))
# Normalize to [0,1]
temp = (temp + 1)*0.5
# Stack the generated data
data.append(temp)
return data
# Data normalization
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 random_generator (batch_size, z_dim, T_mb, max_seq_len):
"""Random vector generation. (Generates a latent vector that is used in the Generator.)
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., 10, [T_mb[i], z_dim])
temp[:T_mb[i],:] = temp_Z
Z_mb.append(temp_Z)
return Z_mb
def visualization (ori_data, generated_data, analysis):
"""Using PCA or tSNE for generated and original data visualization.
Args:
- ori_data: original data
- generated_data: generated synthetic data
- analysis: tsne or pca
"""
# Analysis sample size (for faster computation)
anal_sample_no = min([1000, len(ori_data)])
idx = np.random.permutation(len(ori_data))[:anal_sample_no]
# Data preprocessing
ori_data = np.asarray(ori_data)
generated_data = np.asarray(generated_data)
ori_data = ori_data[idx]
generated_data = generated_data[idx]
no, seq_len, dim = ori_data.shape
for i in range(anal_sample_no):
if (i == 0):
prep_data = np.reshape(np.mean(ori_data[0,:,:], 1), [1,seq_len])
prep_data_hat = np.reshape(np.mean(generated_data[0,:,:],1), [1,seq_len])
else:
prep_data = np.concatenate((prep_data,
np.reshape(np.mean(ori_data[i,:,:],1), [1,seq_len])))
prep_data_hat = np.concatenate((prep_data_hat,
np.reshape(np.mean(generated_data[i,:,:],1), [1,seq_len])))
# Visualization parameter
colors = ["red" for i in range(anal_sample_no)] + ["blue" for i in range(anal_sample_no)]
if analysis == 'pca':
# PCA Analysis
pca = PCA(n_components = 2)
pca.fit(prep_data)
pca_results = pca.transform(prep_data)
pca_hat_results = pca.transform(prep_data_hat)
# Plotting
f, ax = plt.subplots(1)
plt.scatter(pca_results[:,0], pca_results[:,1],
c = colors[:anal_sample_no], alpha = 0.2, label = "Original")
plt.scatter(pca_hat_results[:,0], pca_hat_results[:,1],
c = colors[anal_sample_no:], alpha = 0.2, label = "Synthetic")
ax.legend()
plt.title('PCA plot')
plt.xlabel('x-pca')
plt.ylabel('y_pca')
plt.show()
elif analysis == 'tsne':
# Do t-SNE Analysis together
prep_data_final = np.concatenate((prep_data, prep_data_hat), axis = 0)
# TSNE anlaysis
tsne = TSNE(n_components = 2, verbose = 1, perplexity = 40, n_iter = 300)
tsne_results = tsne.fit_transform(prep_data_final)
# Plotting
f, ax = plt.subplots(1)
plt.scatter(tsne_results[:anal_sample_no,0], tsne_results[:anal_sample_no,1],
c = colors[:anal_sample_no], alpha = 0.2, label = "Original")
plt.scatter(tsne_results[anal_sample_no:,0], tsne_results[anal_sample_no:,1],
c = colors[anal_sample_no:], alpha = 0.2, label = "Synthetic")
ax.legend()
plt.title('t-SNE plot')
plt.xlabel('x-tsne')
plt.ylabel('y_tsne')
plt.show()
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 discriminative_score(num_batches, G, D, S, E, dataloader, batch_size, dim, seq_len, hidden_dim):
"""
Function to calculate Discriminative score
args:
- num_batches: How many batches of data should be evaluated?
- G: Trained Generator
- D: Trained Discriminator
- S: Trained Supervisor
- E: Trained Embedder
- dataloader: the dataloader used for training
- batch_size: The batch size used for training
- dim: the number of features in the data
- seq_len: the length of each time series
- hidden_dim: the hidden dimension of the modules
"""
# optimally one should split the data into training, test, and validation set for this step
scores = []
for i in range(num_batches):
x = next(iter(dataloader))
random_data = random_generator(batch_size=batch_size, z_dim=dim,
T_mb=extract_time(x)[0], max_seq_len=extract_time(x)[1])
z = torch.tensor(random_data)
z = z.float()
# getting discriminator output for generated data
e_hat, _ = G(z)
e_hat = torch.reshape(e_hat, (batch_size, seq_len, hidden_dim))
H_hat, _ = Supervisor(e_hat)
H_hat = torch.reshape(H_hat, (batch_size, seq_len, hidden_dim))
Y_pred_fake = D(H_hat)
# getting discriminator output for real data
embed, _ = E(x)
embed = torch.reshape(embed, (batch_size, seq_len, hidden_dim))
Y_pred_real = D(embed)
# calculate scores for batch
bce = nn.BCEWithLogitsLoss()
score_fake = bce(Y_pred_fake, torch.zeros_like(Y_pred_fake))
score_real = bce(Y_pred_real, torch.ones_like(Y_pred_real))
total = torch.add(score_real, score_fake)
avg = torch.abs(total/2)
avg = avg.detach().numpy()
# append to scores
scores.append(avg)
# return average of scores for all batches
return np.mean(scores)