-
Notifications
You must be signed in to change notification settings - Fork 20
/
predict_with_all_models.py
executable file
·101 lines (85 loc) · 3.71 KB
/
predict_with_all_models.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
#!/usr/bin/python
"""This function loads the best models trained so far and use them to make
predictions using the datasets in the given include file"""
import os
import argparse
import cPickle as pkl
import numpy as np
import theano
from theano import tensor as T
import lasagne
import deepdish
import neural_networks
from params import feats_test_folder, MODEL_DIRECTORY, RESULTS_PATH
from params import nnet_params
dataname = 'votes'
def set_trace():
from IPython.core.debugger import Pdb
import sys
Pdb(color_scheme='Linux').set_trace(sys._getframe().f_back)
def dumpclean(obj):
if type(obj) == dict:
for k, v in obj.items():
if hasattr(v, '__iter__'):
print k
dumpclean(v)
else:
print '%s : %s' % (k, v)
elif type(obj) == list:
for v in obj:
if hasattr(v, '__iter__'):
dumpclean(v)
else:
print v
else:
print obj
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"include_path", type=str,
help="Path to CSV file with rows as 'include ?, train_file, test_file'")
args = parser.parse_args()
# store predictions on a dictionary
model_preds = {}
filepaths = np.loadtxt(args.include_path, dtype=object, delimiter=",")
for (include, train_path, test_path) in filepaths:
if include == '1':
model_name = os.path.basename(train_path)[:-3]
print("\nExecuting prediction on test set \n{}").format(model_name)
for filename in os.listdir(MODEL_DIRECTORY):
if filename.startswith(model_name):
# Load test set, separate target labels from dataset
data = np.load(os.path.join(feats_test_folder,
test_path)).astype(np.float32)
network = neural_networks.build_general_network(
(nnet_params['batch_size'], data.shape[1]-1), # target
nnet_params['n_layers'],
nnet_params['widths'],
nnet_params['non_linearities'],
drop_out=False)
# load best network model so far
parameters = deepdish.io.load(
os.path.join(MODEL_DIRECTORY, filename))
for i in xrange(len(parameters)):
parameters[i] = parameters[i].astype('float32')
lasagne.layers.set_all_param_values(network, parameters)
# set up neural network functions for predictions
input_var = T.fmatrix()
target_var = T.ivector()
prediction = lasagne.layers.get_output(
network, input_var, deterministic=True)
obj_fn = T.mean(T.neq(T.argmax(prediction, axis=1),
target_var))
validate_fn = theano.function(
inputs=[input_var, target_var], outputs=[obj_fn])
# compute predictions. last column is target variable
obj_val = validate_fn(data[:, :-1].astype(input_var.dtype),
data[:, -1].astype(target_var.dtype))
model_preds[filename] = obj_val
print("{} error rate on test set: {}").format(filename,
obj_val)
# dump dictionary
pkl.dump(model_preds, open(
os.path.join(RESULTS_PATH, '{}_results.np'.format(dataname)), 'wb'))
# print dictionary
dumpclean(model_preds)