-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_rat_spn.py
268 lines (224 loc) · 8.43 KB
/
test_rat_spn.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
import numpy as np
import tensorflow as tf
import datasets
from datasets import DEBD
import argparse
import pickle
from models.RatSpn import RatSpn
def compute_performance(sess, data_x, data_labels, batch_size, spn):
"""Compute some performance measures, X-entropy, likelihood, number of correct samples."""
num_batches = int(np.ceil(float(data_x.shape[0]) / float(batch_size)))
test_idx = 0
num_correct_val = 0
CE_total = 0.0
ll = 0.0
margin = 0.0
objective = 0.0
for test_k in range(0, num_batches):
if test_k + 1 < num_batches:
batch_data = data_x[test_idx:test_idx + batch_size, :]
batch_labels = data_labels[test_idx:test_idx + batch_size]
else:
batch_data = data_x[test_idx:, :]
batch_labels = data_labels[test_idx:]
feed_dict = {spn.inputs: batch_data, spn.labels: batch_labels}
if spn.dropout_input_placeholder is not None:
feed_dict[spn.dropout_input_placeholder] = 1.0
if spn.dropout_sums_placeholder is not None:
feed_dict[spn.dropout_sums_placeholder ] = 1.0
num_correct_tmp, CE_tmp, out_tmp, ll_vals, margin_vals, objective_val = sess.run(
[spn.num_correct,
spn.cross_entropy,
spn.outputs,
spn.log_likelihood,
spn.log_margin_hinged,
spn.objective],
feed_dict=feed_dict)
num_correct_val += num_correct_tmp
CE_total += np.sum(CE_tmp)
ll += np.sum(ll_vals)
margin += np.sum(margin_vals)
objective += objective_val * batch_data.shape[0]
test_idx += batch_size
ll = ll / float(data_x.shape[0])
margin = margin / float(data_x.shape[0])
objective = objective / float(data_x.shape[0])
return num_correct_val, CE_total, ll, margin, objective
def run_testing():
#############
# Load data #
#############
if ARGS.data_set in ['mnist', 'fashion_mnist']:
train_x, train_labels, valid_x, valid_labels, test_x, test_labels = datasets.load_mnist(ARGS.data_path)
elif ARGS.data_set in DEBD:
train_x, test_x, valid_x = datasets.load_debd(ARGS.data_path, ARGS.data_set)
train_labels = np.zeros(train_x.shape[0], dtype=np.int32)
test_labels = np.zeros(test_x.shape[0], dtype=np.int32)
valid_labels = np.zeros(valid_x.shape[0], dtype=np.int32)
else:
if ARGS.data_set == '20ng_classify':
unpickled = pickle.load(open(ARGS.data_path + '/20ng-50-lda.pkl', "rb"))
elif ARGS.data_set == 'higgs':
unpickled = pickle.load(open(ARGS.data_path + '/higgs.pkl', "rb"))
elif ARGS.data_set == 'wine':
unpickled = pickle.load(open(ARGS.data_path + '/wine.pkl', "rb"))
elif ARGS.data_set == 'wine_multiclass':
unpickled = pickle.load(open(ARGS.data_path + '/wine_multiclass.pkl', "rb"))
elif ARGS.data_set == 'theorem':
unpickled = pickle.load(open(ARGS.data_path + '/theorem.pkl', "rb"))
elif ARGS.data_set == 'imdb':
unpickled = pickle.load(open(ARGS.data_path + '/imdb-dense-nmf-200.pkl', "rb"))
train_x = unpickled[0]
train_labels = unpickled[1]
valid_x = unpickled[2]
valid_labels = unpickled[3]
test_x = unpickled[4]
test_labels = unpickled[5]
######################
# Data preprocessing #
######################
if ARGS.low_variance_threshold >= 0.0:
v = np.var(train_x, 0)
mu = np.mean(v)
idx = v > ARGS.low_variance_threshold * mu
train_x = train_x[:, idx]
test_x = test_x[:, idx]
if valid_x is not None:
valid_x = valid_x[:, idx]
# zero-mean, unit-variance
if ARGS.normalization == "zmuv":
train_x_mean = np.mean(train_x, 0)
train_x_std = np.std(train_x, 0)
train_x = (train_x - train_x_mean) / (train_x_std + ARGS.zmuv_min_sigma)
test_x = (test_x - train_x_mean) / (train_x_std + ARGS.zmuv_min_sigma)
if valid_x is not None:
valid_x = (valid_x - train_x_mean) / (train_x_std + ARGS.zmuv_min_sigma)
num_classes = len(np.unique(train_labels))
num_dims = int(train_x.shape[1])
ndo, nco, ARGS_orig, region_graph_layers = pickle.load(open(ARGS.model_description_file, 'rb'))
if ndo != num_dims or nco != num_classes:
raise RuntimeError('Inconsistent number of dimensions/classes when trying to retrieve model.')
# Make Tensorflow model
rat_spn = RatSpn(region_graph_layers, num_classes, ARGS=ARGS_orig)
# session
if ARGS.GPU_fraction <= 0.95:
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=ARGS.GPU_fraction)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
else:
sess = tf.Session()
# init/load model
print("Loading model")
init = tf.global_variables_initializer()
sess.run(init)
init_saver = tf.train.Saver(rat_spn.all_params)
init_saver.restore(sess, ARGS.model_init_file)
###########
# Testing #
###########
print("Run testing")
num_correct_train, CE_total, train_LL, train_MARG, train_loss = compute_performance(
sess,
train_x,
train_labels,
100,
rat_spn)
train_ACC = 100. * float(num_correct_train) / float(train_x.shape[0])
train_CE = CE_total / float(train_x.shape[0])
print(' ###')
print(' ### accuracy on train set = {} CE = {} LL: {} negmargin: {}'.format(
train_ACC,
train_CE,
train_LL,
train_MARG))
num_correct_test, CE_total, test_LL, test_MARG, test_loss = compute_performance(
sess,
test_x,
test_labels,
100,
rat_spn)
test_ACC = 100. * float(num_correct_test) / float(test_x.shape[0])
test_CE = CE_total / float(test_x.shape[0])
print(' ###')
print(' ### accuracy on test set = {} CE = {} LL: {} negmargin: {}'.format(test_ACC, test_CE, test_LL, test_MARG))
num_correct_valid, CE_total, valid_LL, valid_MARG, valid_loss = compute_performance(
sess,
valid_x,
valid_labels,
100,
rat_spn)
valid_ACC = 100. * float(num_correct_valid) / float(valid_x.shape[0])
valid_CE = CE_total / float(valid_x.shape[0])
print(' ###')
print(' ### accuracy on valid set = {} CE = {} LL: {} margin: {}'.format(
valid_ACC,
valid_CE,
valid_LL,
valid_MARG))
def make_parser():
parser = argparse.ArgumentParser()
#
# infrastructure arguments
#
infra_arg = parser.add_argument_group('Paths')
infra_arg.add_argument(
'--data_set',
choices=['mnist',
'fashion_mnist',
'20ng_classify',
'higgs',
'wine',
'wine_multiclass',
'eeg-eye',
'theorem',
'imdb'] + DEBD,
default='mnist',
help='Path to data. (%(default)s)'
)
infra_arg.add_argument(
'--data_path',
default='data/mnist/',
help='Path to data. (%(default)s)'
)
infra_arg.add_argument(
'--model_description_file',
default='pretrained/mnist/spn_description.pkl',
help='Pickled model description. (%(default)s)'
)
infra_arg.add_argument(
'--model_init_file',
default='pretrained/mnist/model.ckpt-171',
help='Model file to start from. (%(default)s)'
)
infra_arg.add_argument(
'--GPU_fraction',
type=float,
default=1.0,
help='Fraction of GPU memory to be used by Tensorflow. (%(default)s)'
)
#
# preprocessing arguments
#
preprop_arg = parser.add_argument_group('Pre-processing')
preprop_arg.add_argument(
'--low_variance_threshold',
type=float,
default=0.001,
help='Remove columns whose variance is smaller than low_variance_threshold * mean(variance). (%(default)s)'
)
preprop_arg.add_argument(
'--normalization',
choices=['none', 'zmuv'],
default='zmuv',
help='Which normalization to apply? zmuv: zero-mean-unit-variance (%(default)s)',
)
preprop_arg.add_argument(
'--zmuv_min_sigma',
type=float,
default=0.001,
help='Minimum variance for zmuv normalization (if applied). (%(default)s)',
)
return parser
if __name__ == '__main__':
parser = make_parser()
ARGS = parser.parse_args()
run_testing()