-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGAIN.py
241 lines (172 loc) · 7.01 KB
/
GAIN.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
"""
This code is adapted from https://github.com/jsyoon0823/GAIN
Information about GAIN:
Reference: J. Yoon, J. Jordon, M. van der Schaar, "GAIN: Missing Data Imputation using Generative Adversarial Nets," ICML, 2018.
Paper Link: http://medianetlab.ee.ucla.edu/papers/ICML_GAIN.pdf
Appendix Link: http://medianetlab.ee.ucla.edu/papers/ICML_GAIN_Supp.pdf
"""
import tensorflow as tf
from tqdm import tqdm
import numpy as np
from utils import mse, load_data
def main(p_miss = 0.5, p_hint=0.3, alpha=800, num_epochs=2000, dataset="text",
mode="mcar", para=0.5, train=None, rand_seed=42):
np.random.seed(rand_seed)
tf.set_random_seed(rand_seed)
n, p, xmiss, xhat_0, mask, data_x, data_y = load_data(p_miss, dataset=dataset, mode=mode, para=para, train=train, rand_seed=rand_seed)
# Mini batch size
mb_size = 64
# Imput Dim (Fixed)
train_rate = 1
Data = data_x
# Parameters
No = n
Dim = p
# Hidden state dimensions
H_Dim1 = Dim
H_Dim2 = Dim
# %% Missing introducing
Missing = mask*1
# %% Train Test Division
idx = np.random.permutation(No)
Train_No = int(No * train_rate)
Test_No = No - Train_No
# Train / Test Features
trainX = Data[idx[:Train_No], :]
testX = Data[idx[Train_No:], :]
# Train / Test Missing Indicators
trainM = Missing[idx[:Train_No], :]
testM = Missing[idx[Train_No:], :]
# %% Necessary Functions
# 1. Xavier Initialization Definition
def xavier_init(size):
in_dim = size[0]
xavier_stddev = 1. / tf.sqrt(in_dim / 2.)
return tf.random_normal(shape=size, stddev=xavier_stddev)
# Hint Vector Generation
def sample_M(m, n, p):
A = np.random.uniform(0., 1., size=[m, n])
B = A > p
C = 1. * B
return C
'''
GAIN Consists of 3 Components
- Generator
- Discriminator
- Hint Mechanism
'''
# %% GAIN Architecture
# %% 1. Input Placeholders
# 1.1. Data Vector
X = tf.placeholder(tf.float32, shape=[None, Dim])
# 1.2. Mask Vector
M = tf.placeholder(tf.float32, shape=[None, Dim])
# 1.3. Hint vector
H = tf.placeholder(tf.float32, shape=[None, Dim])
# 1.4. X with missing values
New_X = tf.placeholder(tf.float32, shape=[None, Dim])
# %% 2. Discriminator
D_W1 = tf.Variable(xavier_init([Dim * 2, H_Dim1])) # Data + Hint as inputs
D_b1 = tf.Variable(tf.zeros(shape=[H_Dim1]))
D_W2 = tf.Variable(xavier_init([H_Dim1, H_Dim2]))
D_b2 = tf.Variable(tf.zeros(shape=[H_Dim2]))
D_W3 = tf.Variable(xavier_init([H_Dim2, Dim]))
D_b3 = tf.Variable(tf.zeros(shape=[Dim])) # Output is multi-variate
theta_D = [D_W1, D_W2, D_W3, D_b1, D_b2, D_b3]
# %% 3. Generator
G_W1 = tf.Variable(xavier_init([Dim * 2, H_Dim1])) # Data + Mask as inputs (Random Noises are in Missing Components)
G_b1 = tf.Variable(tf.zeros(shape=[H_Dim1]))
G_W2 = tf.Variable(xavier_init([H_Dim1, H_Dim2]))
G_b2 = tf.Variable(tf.zeros(shape=[H_Dim2]))
G_W3 = tf.Variable(xavier_init([H_Dim2, Dim]))
G_b3 = tf.Variable(tf.zeros(shape=[Dim]))
theta_G = [G_W1, G_W2, G_W3, G_b1, G_b2, G_b3]
# %% GAIN Function
# %% 1. Generator
def generator(new_x, m):
inputs = tf.concat(axis=1, values=[new_x, m]) # Mask + Data Concatenate
G_h1 = tf.nn.relu(tf.matmul(inputs, G_W1) + G_b1)
G_h2 = tf.nn.relu(tf.matmul(G_h1, G_W2) + G_b2)
G_prob = tf.nn.sigmoid(tf.matmul(G_h2, G_W3) + G_b3) # [0,1] normalized Output
return G_prob
# %% 2. Discriminator
def discriminator(new_x, h):
inputs = tf.concat(axis=1, values=[new_x, h]) # Hint + Data Concatenate
D_h1 = tf.nn.relu(tf.matmul(inputs, D_W1) + D_b1)
D_h2 = tf.nn.relu(tf.matmul(D_h1, D_W2) + D_b2)
D_logit = tf.matmul(D_h2, D_W3) + D_b3
D_prob = tf.nn.sigmoid(D_logit) # [0,1] Probability Output
return D_prob
# %% 3. Other functions
# Random sample generator for Z
def sample_Z(m, n):
return np.random.uniform(0., 0.01, size=[m, n])
# Mini-batch generation
def sample_idx(m, n):
A = np.random.permutation(m)
idx = A[:n]
return idx
# %% Structure
# Generator
G_sample = generator(New_X, M)
# Combine with original data
Hat_New_X = New_X * M + G_sample * (1 - M)
# Discriminator
D_prob = discriminator(Hat_New_X, H)
# %% Loss
D_loss1 = -tf.reduce_mean(M * tf.log(D_prob + 1e-8) + (1 - M) * tf.log(1. - D_prob + 1e-8))
G_loss1 = -tf.reduce_mean((1 - M) * tf.log(D_prob + 1e-8))
MSE_train_loss = tf.reduce_mean((M * New_X - M * G_sample) ** 2) / tf.reduce_mean(M)
D_loss = D_loss1
G_loss = G_loss1 + alpha * MSE_train_loss
# %% MSE Performance metric
MSE_test_loss = tf.reduce_mean(((1 - M) * X - (1 - M) * G_sample) ** 2) / tf.reduce_mean(1 - M)
# %% Solver
D_solver = tf.train.AdamOptimizer().minimize(D_loss, var_list=theta_D)
G_solver = tf.train.AdamOptimizer().minimize(G_loss, var_list=theta_G)
# Sessions
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# %% Iterations
errors = []
# %% Start Iterations
for it in tqdm(range(num_epochs)):
# %% Inputs
mb_idx = sample_idx(Train_No, mb_size)
X_mb = trainX[mb_idx, :]
Z_mb = sample_Z(mb_size, Dim)
M_mb = trainM[mb_idx, :]
H_mb1 = sample_M(mb_size, Dim, 1 - p_hint)
H_mb = M_mb * H_mb1
New_X_mb = M_mb * X_mb + (1 - M_mb) * Z_mb # Missing Data Introduce
_, D_loss_curr = sess.run([D_solver, D_loss1], feed_dict={M: M_mb, New_X: New_X_mb, H: H_mb})
_, G_loss_curr, MSE_train_loss_curr, MSE_test_loss_curr = sess.run(
[G_solver, G_loss1, MSE_train_loss, MSE_test_loss],
feed_dict={X: X_mb, M: M_mb, New_X: New_X_mb, H: H_mb})
# %% Intermediate Losses
if it % 50 == 0:
Z_mb = sample_Z(n, p)
New_X_mb = Missing * data_x + (1 - Missing) * Z_mb
x_filled = sess.run(G_sample, feed_dict={X: data_x, M: Missing, New_X: New_X_mb})
print('Iter: {}'.format(it))
print('Train_loss: {:.4}'.format(np.sqrt(MSE_train_loss_curr)))
print('Test_loss: {:.4}'.format(np.sqrt(MSE_test_loss_curr)))
errors.append(mse(x_filled, data_x, mask))
print("Real MSE: ", errors[-1])
# %% Final Loss
if train_rate != 1:
Z_mb = sample_Z(Test_No, Dim)
M_mb = testM
X_mb = testX
New_X_mb = M_mb * X_mb + (1 - M_mb) * Z_mb # Missing Data Introduce
MSE_final, Sample = sess.run([MSE_test_loss, G_sample], feed_dict={X: testX, M: testM, New_X: New_X_mb})
print('Final Test MSE: ' + str(MSE_final))
# Real Error
Z_mb = sample_Z(n, p)
New_X_mb = Missing * data_x + (1 - Missing) * Z_mb
x_filled = sess.run(G_sample, feed_dict={X: data_x, M: Missing, New_X: New_X_mb})
real_mse = mse(x_filled, data_x, mask)
print("Real final MSE: " + str(real_mse))
return x_filled, real_mse
if __name__ == "__main__":
main()