-
Notifications
You must be signed in to change notification settings - Fork 0
/
judge.py
executable file
·155 lines (125 loc) · 4.88 KB
/
judge.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
from __future__ import print_function
import numpy as np
import tensorflow as tf
import mf
import pickle
import sys
import sudoku
import argparse
import os
import sys
import setproctitle
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
np.set_printoptions(precision=3,suppress=True)
def judge(args):
setproctitle.setproctitle('testing {} on {}'.format(args.input, args.dataset))
with open(args.dataset,'rb') as f:
dataset_X, dataset_Y = pickle.load(f)
# todo: be backward compatible. (old format = weights, unary, annealing, new format = dictionnary)
with open(args.input,'rb') as f:
model_data = pickle.load(f)
print(model_data['args'])
print("Version:",model_data['version'])
zeropad = not(model_data['args'].nopad)
g = args.boardSz
n = (g**2)
if zeropad:
n *= 2
p = 1+g**2
n_samples = len(dataset_X) if args.n == -1 else args.n
batch_size = args.bs
n_modes = args.lognmodes
if zeropad:
inputs = sudoku.grid_to_clip(sudoku.expand_matrix(dataset_X,g,p))
else:
inputs = sudoku.grid_to_clip(dataset_X)
w = model_data['links']
u = model_data['unary']
a = model_data['annealing']
FNN = model_data['FNN']
_,_,L2,_ = FNN
h,k = L2.shape
links = tf.Variable(tf.convert_to_tensor(w))
links_sym = links+tf.transpose(links, [0, 1, 2, 4, 3])
unary = tf.Variable(tf.convert_to_tensor(u))
mmmf = mf.BatchedMultiModalMeanField(n, n, p, batch_size, links_sym, unary, np.exp(a), a.shape[0], k=k, h=h, FNN=FNN)
q_mf = mmmf.get_q_mf_values()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
sess.run(tf.global_variables_initializer())
n_correct = 0
n_correct_mul = 0
n_correct_one = 0
n_correct_tot = 0
n_mul = 0
n_one = 0
n_tot = 0
success_mul = []
success_one = []
failure_mul = []
failure_one = []
for b in range(n_samples/batch_size):
mmmf.reset_all(np.expand_dims(np.array(inputs[b*batch_size:(b+1)*batch_size]),axis=1))
for _ in range(n_modes):
mmmf.iteration(sess)
parameters = {
mmmf._theta_clip: np.reshape(mmmf._modes,(-1,2,n,n,p)),
mmmf._T: 1.
}
q_values = np.reshape(sess.run(q_mf, feed_dict=parameters),(batch_size,-1,n,n,p))
for modes_clip, q_modes, grid_input, grid_output in zip(mmmf._modes, q_values, dataset_X[b*batch_size:(b+1)*batch_size], dataset_Y[b*batch_size:(b+1)*batch_size]):
ok = False
res = []
if args.explain:
print("###")
print(sudoku.infer_grid(grid_input))
print(sudoku.infer_grid(grid_output))
for q_mode, clip_grid in zip(q_modes, sudoku.clip_to_grid(modes_clip)):
grid = sudoku.infer_grid(sudoku.reduce_matrix(q_mode,g,p) if zeropad else q_mode)
res.append(q_mode)
if args.explain:
print("##")
print(sudoku.infer_grid(sudoku.reduce_matrix(clip_grid,g,p) if zeropad else clip_grid))
print(np.max(q_mode,axis=-1))
print(grid)
print("##")
if sudoku.is_correct(grid,g):
ok = True
n_correct_tot += 1
n_sol = sudoku.n_solutions_grid(sudoku.infer_grid(grid_input),g)
is_one = False#sudoku.n_solutions_grid(sudoku.infer_grid(grid_input),g) == 1
if is_one:
n_one += 1
else:
n_mul += 1
n_tot += n_sol
res = np.array(res)
if ok:
if is_one:
success_one.append((grid_input,res))
n_correct_one += 1
else:
success_mul.append((grid_input,res))
n_correct_mul += 1
n_correct += 1
else:
if is_one:
failure_one.append((grid_input,res))
else:
failure_mul.append((grid_input,res))
print('{}/{} | S: {}/{} | M: {}/{} | T: {}/{} '.format(n_correct,batch_size*(b+1),n_correct_one,n_one,n_correct_mul,n_mul,n_correct_tot,n_tot),end='\r')
sys.stdout.flush()
print()
return (n_correct, n_samples, n_correct_tot, n_tot)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, default='')
parser.add_argument('--input', type=str, default='')
parser.add_argument('--boardSz', type=int, default=2)
parser.add_argument('--bs', type=int, default=50)
parser.add_argument('--lognmodes', type=int, default=2)
parser.add_argument('--explain', default=False, action='store_true')
parser.add_argument('--n', type=int, default=-1)
args = parser.parse_args()
judge(args)