forked from Philip-Bachman/Sequential-Generation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestImpTM.py
248 lines (221 loc) · 8.87 KB
/
TestImpTM.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
##################################################################
# Code for testing the variational Multi-Stage Generative Model. #
##################################################################
# basic python
import numpy as np
import numpy.random as npr
import cPickle
# theano business
import theano
import theano.tensor as T
# phil's sweetness
import utils
from GPSImputer import TemplateMatchImputer
from load_data import load_udm, load_mnist, load_tfd, load_svhn_gray
from HelperFuncs import construct_masked_data, shift_and_scale_into_01, \
row_shuffle, to_fX
###############################
###############################
## TEST GPS IMPUTER ON MNIST ##
###############################
###############################
def test_mnist_nll(occ_dim=15, drop_prob=0.0):
RESULT_PATH = "IMP_MNIST_TM/"
#########################################
# Format the result tag more thoroughly #
#########################################
dp_int = int(100.0 * drop_prob)
result_tag = RESULT_PATH + "TM_OD{}_DP{}".format(occ_dim, dp_int)
##########################
# Get some training data #
##########################
rng = np.random.RandomState(1234)
dataset = 'data/mnist.pkl.gz'
datasets = load_udm(dataset, as_shared=False, zero_mean=False)
Xtr = datasets[0][0]
Xva = datasets[1][0]
Xtr = to_fX(shift_and_scale_into_01(Xtr))
Xva = to_fX(shift_and_scale_into_01(Xva))
tr_samples = Xtr.shape[0]
va_samples = Xva.shape[0]
batch_size = 200
batch_reps = 1
all_pix_mean = np.mean(np.mean(Xtr, axis=1))
data_mean = to_fX(all_pix_mean * np.ones((Xtr.shape[1],)))
TM = TemplateMatchImputer(x_train=Xtr, x_type='bernoulli')
log_name = "{}_RESULTS.txt".format(result_tag)
out_file = open(log_name, 'wb')
Xva = row_shuffle(Xva)
# record an estimate of performance on the test set
xi, xo, xm = construct_masked_data(Xva, drop_prob=drop_prob, \
occ_dim=occ_dim, data_mean=data_mean)
result = TM.best_match_nll(xo, xm)
match_on_known = np.mean(result[0])
match_on_unknown = np.mean(result[1])
str0 = "Test 1:"
str1 = " match on known : {}".format(match_on_known)
str2 = " match on unknown : {}".format(match_on_unknown)
joint_str = "\n".join([str0, str1, str2])
print(joint_str)
out_file.write(joint_str+"\n")
out_file.flush()
out_file.close()
return
def test_mnist_img(occ_dim=15, drop_prob=0.0):
#########################################
# Format the result tag more thoroughly #
#########################################
dp_int = int(100.0 * drop_prob)
result_tag = RESULT_PATH + "TM_OD{}_DP{}".format(occ_dim, dp_int)
##########################
# Get some training data #
##########################
rng = np.random.RandomState(1234)
dataset = 'data/mnist.pkl.gz'
datasets = load_udm(dataset, as_shared=False, zero_mean=False)
Xtr = datasets[0][0]
Xva = datasets[1][0]
Xtr = to_fX(shift_and_scale_into_01(Xtr))
Xva = to_fX(shift_and_scale_into_01(Xva))
tr_samples = Xtr.shape[0]
va_samples = Xva.shape[0]
batch_size = 200
batch_reps = 1
all_pix_mean = np.mean(np.mean(Xtr, axis=1))
data_mean = to_fX(all_pix_mean * np.ones((Xtr.shape[1],)))
TM = TemplateMatchImputer(x_train=Xtr, x_type='bernoulli')
Xva = row_shuffle(Xva)
# record an estimate of performance on the test set
xi, xo, xm = construct_masked_data(Xva[:500], drop_prob=drop_prob, \
occ_dim=occ_dim, data_mean=data_mean)
img_match_on_known, img_match_on_unknown = TM.best_match_img(xo, xm)
display_count = 100
# visualize matches on known elements
Xs = np.zeros((2*display_count, Xva.shape[1]))
for idx in range(display_count):
Xs[2*idx] = xi[idx]
Xs[(2*idx)+1] = img_match_on_known[idx]
file_name = "{0:s}_SAMPLES_MOK.png".format(result_tag)
utils.visualize_samples(Xs, file_name, num_rows=20)
# visualize matches on unknown elements
Xs = np.zeros((2*display_count, Xva.shape[1]))
for idx in range(display_count):
Xs[2*idx] = xi[idx]
Xs[(2*idx)+1] = img_match_on_unknown[idx]
file_name = "{0:s}_SAMPLES_MOU.png".format(result_tag)
utils.visualize_samples(Xs, file_name, num_rows=20)
return
def test_tfd_nll(occ_dim=15, drop_prob=0.0):
RESULT_PATH = "IMP_TFD_TM/"
#########################################
# Format the result tag more thoroughly #
#########################################
dp_int = int(100.0 * drop_prob)
result_tag = RESULT_PATH + "TM_OD{}_DP{}".format(occ_dim, dp_int)
##########################
# Get some training data #
##########################
rng = np.random.RandomState(1234)
data_file = 'data/tfd_data_48x48.pkl'
dataset = load_tfd(tfd_pkl_name=data_file, which_set='unlabeled', fold='all')
Xtr_unlabeled = dataset[0]
dataset = load_tfd(tfd_pkl_name=data_file, which_set='train', fold='all')
Xtr_train = dataset[0]
Xtr = np.vstack([Xtr_unlabeled, Xtr_train])
dataset = load_tfd(tfd_pkl_name=data_file, which_set='valid', fold='all')
Xva = dataset[0]
Xtr = to_fX(shift_and_scale_into_01(Xtr))
Xva = to_fX(shift_and_scale_into_01(Xva))
tr_samples = Xtr.shape[0]
va_samples = Xva.shape[0]
batch_size = 250
batch_reps = 1
all_pix_mean = np.mean(np.mean(Xtr, axis=1))
data_mean = to_fX( all_pix_mean * np.ones((Xtr.shape[1],)) )
TM = TemplateMatchImputer(x_train=Xtr, x_type='bernoulli')
log_name = "{}_RESULTS.txt".format(result_tag)
out_file = open(log_name, 'wb')
Xva = row_shuffle(Xva)
# record an estimate of performance on the test set
xi, xo, xm = construct_masked_data(Xva, drop_prob=drop_prob, \
occ_dim=occ_dim, data_mean=data_mean)
result = TM.best_match_nll(xo, xm)
match_on_known = np.mean(result[0])
match_on_unknown = np.mean(result[1])
str0 = "Test 1:"
str1 = " match on known : {}".format(match_on_known)
str2 = " match on unknown : {}".format(match_on_unknown)
joint_str = "\n".join([str0, str1, str2])
print(joint_str)
out_file.write(joint_str+"\n")
out_file.flush()
out_file.close()
return
def test_svhn_nll(occ_dim=15, drop_prob=0.0):
RESULT_PATH = "IMP_SVHN_TM/"
#########################################
# Format the result tag more thoroughly #
#########################################
dp_int = int(100.0 * drop_prob)
result_tag = RESULT_PATH + "TM_OD{}_DP{}".format(occ_dim, dp_int)
##########################
# Get some training data #
##########################
rng = np.random.RandomState(1234)
tr_file = 'data/svhn_train_gray.pkl'
te_file = 'data/svhn_test_gray.pkl'
ex_file = 'data/svhn_extra_gray.pkl'
data = load_svhn_gray(tr_file, te_file, ex_file=ex_file, ex_count=200000)
Xtr = to_fX( shift_and_scale_into_01(np.vstack([data['Xtr'], data['Xex']])) )
Xva = to_fX( shift_and_scale_into_01(data['Xte']) )
tr_samples = Xtr.shape[0]
va_samples = Xva.shape[0]
batch_size = 250
batch_reps = 1
all_pix_mean = np.mean(np.mean(Xtr, axis=1))
data_mean = to_fX( all_pix_mean * np.ones((Xtr.shape[1],)) )
TM = TemplateMatchImputer(x_train=Xtr, x_type='bernoulli')
log_name = "{}_RESULTS.txt".format(result_tag)
out_file = open(log_name, 'wb')
Xva = row_shuffle(Xva)
# record an estimate of performance on the test set
xi, xo, xm = construct_masked_data(Xva, drop_prob=drop_prob, \
occ_dim=occ_dim, data_mean=data_mean)
result = TM.best_match_nll(xo, xm)
match_on_known = np.mean(result[0])
match_on_unknown = np.mean(result[1])
str0 = "Test 1:"
str1 = " match on known : {}".format(match_on_known)
str2 = " match on unknown : {}".format(match_on_unknown)
joint_str = "\n".join([str0, str1, str2])
print(joint_str)
out_file.write(joint_str+"\n")
out_file.flush()
out_file.close()
return
if __name__=="__main__":
#########
# MNIST #
#########
# test_mnist_nll(occ_dim=0, drop_prob=0.6)
# test_mnist_nll(occ_dim=0, drop_prob=0.7)
# test_mnist_nll(occ_dim=0, drop_prob=0.8)
# test_mnist_nll(occ_dim=0, drop_prob=0.9)
# test_mnist_nll(occ_dim=14, drop_prob=0.0)
# test_mnist_nll(occ_dim=16, drop_prob=0.0)
# test_mnist_img(occ_dim=0, drop_prob=0.6)
# test_mnist_img(occ_dim=0, drop_prob=0.7)
# test_mnist_img(occ_dim=0, drop_prob=0.8)
# test_mnist_img(occ_dim=0, drop_prob=0.9)
# test_mnist_img(occ_dim=14, drop_prob=0.0)
# test_mnist_img(occ_dim=16, drop_prob=0.0)
########
# SVHN #
########
#test_svhn_nll(occ_dim=17, drop_prob=0.0)
#test_svhn_nll(occ_dim=0, drop_prob=0.8)
#######
# TFD #
#######
#test_tfd_nll(occ_dim=25, drop_prob=0.0)
test_tfd_nll(occ_dim=0, drop_prob=0.8)