50951
|
1 |
'''
|
|
2 |
Created on Jan 11, 2013
|
|
3 |
|
|
4 |
Searches for the best parameters.
|
|
5 |
|
|
6 |
@author: Daniel Kuehlwein
|
|
7 |
'''
|
|
8 |
|
|
9 |
import logging,sys,os
|
|
10 |
from multiprocessing import Process,Queue,current_process,cpu_count
|
|
11 |
from mash import mash
|
|
12 |
|
|
13 |
def worker(inQueue, outQueue):
|
|
14 |
for func, args in iter(inQueue.get, 'STOP'):
|
|
15 |
result = func(*args)
|
|
16 |
#print '%s says that %s%s = %s' % (current_process().name, func.__name__, args, result)
|
|
17 |
outQueue.put(result)
|
|
18 |
|
53100
|
19 |
def run_mash(runId,inputDir,logFile,predictionFile,predef,\
|
50951
|
20 |
learnTheories,theoryDefValPos,theoryDefValNeg,theoryPosWeight,\
|
|
21 |
NBDefaultPriorWeight,NBDefVal,NBPosWeight,\
|
53100
|
22 |
sineFeatures,sineWeight,quiet=True):
|
50951
|
23 |
# Init
|
|
24 |
runId = str(runId)
|
|
25 |
predictionFile = predictionFile + runId
|
53100
|
26 |
args = ['--statistics','--init','--inputDir',inputDir,'--log',logFile,'--theoryFile','../tmp/t'+runId,'--modelFile','../tmp/m'+runId,'--dictsFile','../tmp/d'+runId,
|
50951
|
27 |
'--theoryDefValPos',str(theoryDefValPos),'--theoryDefValNeg',str(theoryDefValNeg),'--theoryPosWeight',str(theoryPosWeight),\
|
|
28 |
'--NBDefaultPriorWeight',str(NBDefaultPriorWeight),'--NBDefVal',str(NBDefVal),'--NBPosWeight',str(NBPosWeight)]
|
|
29 |
if learnTheories:
|
53100
|
30 |
args += ['--learnTheories']
|
50951
|
31 |
if sineFeatures:
|
|
32 |
args += ['--sineFeatures','--sineWeight',str(sineWeight)]
|
53100
|
33 |
if not predef == '':
|
|
34 |
args += ['--predef',predef]
|
|
35 |
if quit:
|
|
36 |
args += ['-q']
|
|
37 |
#print args
|
50951
|
38 |
mash(args)
|
|
39 |
# Run
|
53100
|
40 |
args = ['-i',inputFile,'-p',predictionFile,'--statistics','--cutOff','1024','--log',logFile,'--theoryFile','../tmp/t'+runId,'--modelFile','../tmp/m'+runId,'--dictsFile','../tmp/d'+runId,\
|
50951
|
41 |
'--theoryDefValPos',str(theoryDefValPos),'--theoryDefValNeg',str(theoryDefValNeg),'--theoryPosWeight',str(theoryPosWeight),\
|
|
42 |
'--NBDefaultPriorWeight',str(NBDefaultPriorWeight),'--NBDefVal',str(NBDefVal),'--NBPosWeight',str(NBPosWeight)]
|
|
43 |
if learnTheories:
|
53100
|
44 |
args += ['--learnTheories']
|
50951
|
45 |
if sineFeatures:
|
|
46 |
args += ['--sineFeatures','--sineWeight',str(sineWeight)]
|
53100
|
47 |
if not predef == '':
|
|
48 |
args += ['--predef',predef]
|
|
49 |
if quit:
|
|
50 |
args += ['-q']
|
|
51 |
#print args
|
50951
|
52 |
mash(args)
|
|
53 |
|
|
54 |
# Get Results
|
|
55 |
IS = open(logFile,'r')
|
|
56 |
lines = IS.readlines()
|
|
57 |
tmpRes = lines[-1].split()
|
|
58 |
avgAuc = tmpRes[5]
|
53100
|
59 |
medianAuc = tmpRes[7]
|
|
60 |
avgRecall100 = tmpRes[11]
|
|
61 |
medianRecall100 = tmpRes[13]
|
50951
|
62 |
tmpTheoryRes = lines[-3].split()
|
53100
|
63 |
if learnTheories:
|
|
64 |
avgTheoryPrecision = tmpTheoryRes[5]
|
|
65 |
avgTheoryRecall100 = tmpTheoryRes[7]
|
|
66 |
avgTheoryRecall = tmpTheoryRes[9]
|
|
67 |
avgTheoryPredictedPercent = tmpTheoryRes[11]
|
|
68 |
else:
|
|
69 |
avgTheoryPrecision = 'NA'
|
|
70 |
avgTheoryRecall100 = 'NA'
|
|
71 |
avgTheoryRecall = 'NA'
|
|
72 |
avgTheoryPredictedPercent = 'NA'
|
50951
|
73 |
IS.close()
|
|
74 |
|
|
75 |
# Delete old models
|
|
76 |
os.remove(logFile)
|
|
77 |
os.remove(predictionFile)
|
53100
|
78 |
if learnTheories:
|
|
79 |
os.remove('../tmp/t'+runId)
|
50951
|
80 |
os.remove('../tmp/m'+runId)
|
|
81 |
os.remove('../tmp/d'+runId)
|
|
82 |
|
|
83 |
outFile = open('tester','a')
|
|
84 |
#print 'avgAuc %s avgRecall100 %s avgTheoryPrecision %s avgTheoryRecall100 %s avgTheoryRecall %s avgTheoryPredictedPercent %s'
|
53100
|
85 |
outFile.write('\t'.join([str(learnTheories),str(theoryDefValPos),str(theoryDefValNeg),str(theoryPosWeight),\
|
|
86 |
str(NBDefaultPriorWeight),str(NBDefVal),str(NBPosWeight),str(sineFeatures),str(sineWeight),\
|
|
87 |
str(avgAuc),str(medianAuc),str(avgRecall100),str(medianRecall100),str(avgTheoryPrecision),\
|
|
88 |
str(avgTheoryRecall100),str(avgTheoryRecall),str(avgTheoryPredictedPercent)])+'\n')
|
50951
|
89 |
outFile.close()
|
|
90 |
print learnTheories,'\t',theoryDefValPos,'\t',theoryDefValNeg,'\t',theoryPosWeight,'\t',\
|
|
91 |
NBDefaultPriorWeight,'\t',NBDefVal,'\t',NBPosWeight,'\t',\
|
|
92 |
sineFeatures,'\t',sineWeight,'\t',\
|
53100
|
93 |
avgAuc,'\t',medianAuc,'\t',avgRecall100,'\t',medianRecall100,'\t',\
|
|
94 |
avgTheoryPrecision,'\t',avgTheoryRecall100,'\t',avgTheoryRecall,'\t',avgTheoryPredictedPercent
|
50951
|
95 |
return learnTheories,theoryDefValPos,theoryDefValNeg,theoryPosWeight,\
|
|
96 |
NBDefaultPriorWeight,NBDefVal,NBPosWeight,\
|
|
97 |
sineFeatures,sineWeight,\
|
|
98 |
avgAuc,avgRecall100,avgTheoryPrecision,avgTheoryRecall100,avgTheoryRecall,avgTheoryPredictedPercent
|
|
99 |
|
|
100 |
def update_best_params(avgRecall100,bestAvgRecall100,\
|
|
101 |
bestNBDefaultPriorWeight,bestNBDefVal,bestNBPosWeight,bestSineFeatures,bestSineWeight,\
|
|
102 |
bestlearnTheories,besttheoryDefValPos,besttheoryDefValNeg,besttheoryPosWeight,\
|
|
103 |
NBDefaultPriorWeight,NBDefVal,NBPosWeight,sineFeatures,sineWeight,\
|
|
104 |
learnTheories,theoryDefValPos,theoryDefValNeg,theoryPosWeight):
|
|
105 |
if avgRecall100 > bestAvgRecall100:
|
|
106 |
bestAvgRecall100 = avgRecall100
|
|
107 |
bestNBDefaultPriorWeight = NBDefaultPriorWeight
|
|
108 |
bestNBDefVal = NBDefVal
|
|
109 |
bestNBPosWeight = NBPosWeight
|
|
110 |
bestSineFeatures = sineFeatures
|
|
111 |
bestSineWeight = sineWeight
|
|
112 |
return bestlearnTheories,besttheoryDefValPos,besttheoryDefValNeg,besttheoryPosWeight,bestNBDefaultPriorWeight,bestNBDefVal,bestNBPosWeight,bestSineFeatures,bestSineWeight
|
|
113 |
|
|
114 |
if __name__ == '__main__':
|
|
115 |
cores = cpu_count()
|
|
116 |
#cores = 1
|
|
117 |
# Options
|
|
118 |
depFile = 'mash_dependencies'
|
53100
|
119 |
predef = ''
|
50951
|
120 |
outputDir = '../tmp/'
|
53100
|
121 |
numberOfPredictions = 1024
|
50951
|
122 |
|
|
123 |
learnTheoriesRange = [True,False]
|
|
124 |
theoryDefValPosRange = [-x for x in range(1,20)]
|
|
125 |
theoryDefValNegRange = [-x for x in range(1,20)]
|
|
126 |
theoryPosWeightRange = [x for x in range(1,10)]
|
|
127 |
|
|
128 |
NBDefaultPriorWeightRange = [10*x for x in range(10)]
|
|
129 |
NBDefValRange = [-x for x in range(1,20)]
|
|
130 |
NBPosWeightRange = [10*x for x in range(1,10)]
|
|
131 |
sineFeaturesRange = [True,False]
|
|
132 |
sineWeightRange = [0.1,0.25,0.5,0.75,1.0]
|
|
133 |
|
53100
|
134 |
"""
|
50951
|
135 |
# Test 1
|
|
136 |
inputFile = '../data/20121227b/Auth/mash_commands'
|
|
137 |
inputDir = '../data/20121227b/Auth/'
|
|
138 |
predictionFile = '../tmp/auth.pred'
|
|
139 |
logFile = '../tmp/auth.log'
|
|
140 |
learnTheories = True
|
|
141 |
theoryDefValPos = -7.5
|
|
142 |
theoryDefValNeg = -15.0
|
|
143 |
theoryPosWeight = 10.0
|
|
144 |
NBDefaultPriorWeight = 20.0
|
|
145 |
NBDefVal =- 15.0
|
|
146 |
NBPosWeight = 10.0
|
|
147 |
sineFeatures = True
|
|
148 |
sineWeight = 0.5
|
|
149 |
|
|
150 |
task_queue = Queue()
|
|
151 |
done_queue = Queue()
|
|
152 |
|
|
153 |
runs = 0
|
|
154 |
for inputDir in ['../data/20121227b/Auth/']:
|
|
155 |
problemId = inputDir.split('/')[-2]
|
|
156 |
inputFile = os.path.join(inputDir,'mash_commands')
|
|
157 |
predictionFile = os.path.join('../tmp/',problemId+'.pred')
|
|
158 |
logFile = os.path.join('../tmp/',problemId+'.log')
|
|
159 |
learnTheories = True
|
|
160 |
theoryDefValPos = -7.5
|
|
161 |
theoryDefValNeg = -15.0
|
|
162 |
theoryPosWeight = 10.0
|
|
163 |
|
|
164 |
bestAvgRecall100 = 0.0
|
|
165 |
bestNBDefaultPriorWeight = 1.0
|
|
166 |
bestNBDefVal = 1.0
|
|
167 |
bestNBPosWeight = 1.0
|
|
168 |
bestSineFeatures = False
|
|
169 |
bestSineWeight = 0.0
|
|
170 |
bestlearnTheories = True
|
|
171 |
besttheoryDefValPos = 1.0
|
|
172 |
besttheoryDefValNeg = -15.0
|
|
173 |
besttheoryPosWeight = 5.0
|
|
174 |
for theoryPosWeight in theoryPosWeightRange:
|
|
175 |
for theoryDefValNeg in theoryDefValNegRange:
|
|
176 |
for NBDefaultPriorWeight in NBDefaultPriorWeightRange:
|
|
177 |
for NBDefVal in NBDefValRange:
|
|
178 |
for NBPosWeight in NBPosWeightRange:
|
|
179 |
for sineFeatures in sineFeaturesRange:
|
|
180 |
if sineFeatures:
|
|
181 |
for sineWeight in sineWeightRange:
|
|
182 |
localLogFile = logFile+str(runs)
|
|
183 |
task_queue.put((run_mash,(runs,inputDir, localLogFile, predictionFile, learnTheories, theoryDefValPos, theoryDefValNeg, theoryPosWeight, NBDefaultPriorWeight, NBDefVal, NBPosWeight, sineFeatures, sineWeight)))
|
|
184 |
runs += 1
|
|
185 |
else:
|
|
186 |
localLogFile = logFile+str(runs)
|
|
187 |
task_queue.put((run_mash,(runs,inputDir, localLogFile, predictionFile, learnTheories, theoryDefValPos, theoryDefValNeg, theoryPosWeight, NBDefaultPriorWeight, NBDefVal, NBPosWeight, sineFeatures, sineWeight)))
|
|
188 |
runs += 1
|
|
189 |
# Start worker processes
|
|
190 |
processes = []
|
|
191 |
for _i in range(cores):
|
|
192 |
process = Process(target=worker, args=(task_queue, done_queue))
|
|
193 |
process.start()
|
|
194 |
processes.append(process)
|
|
195 |
|
|
196 |
for _i in range(runs):
|
|
197 |
learnTheories,theoryDefValPos,theoryDefValNeg,theoryPosWeight,\
|
|
198 |
NBDefaultPriorWeight,NBDefVal,NBPosWeight,\
|
|
199 |
sineFeatures,sineWeight,\
|
|
200 |
avgAuc,avgRecall100,avgTheoryPrecision,avgTheoryRecall100,avgTheoryRecall,avgTheoryPredictedPercent = done_queue.get()
|
|
201 |
bestlearnTheories,besttheoryDefValPos,besttheoryDefValNeg,besttheoryPosWeight,bestNBDefaultPriorWeight,bestNBDefVal,bestNBPosWeight,bestSineFeatures,bestSineWeight = update_best_params(avgRecall100,bestAvgRecall100,\
|
|
202 |
bestNBDefaultPriorWeight,bestNBDefVal,bestNBPosWeight,bestSineFeatures,bestSineWeight,\
|
|
203 |
bestlearnTheories,besttheoryDefValPos,besttheoryDefValNeg,besttheoryPosWeight,\
|
|
204 |
NBDefaultPriorWeight,NBDefVal,NBPosWeight,sineFeatures,sineWeight,\
|
|
205 |
learnTheories,theoryDefValPos,theoryDefValNeg,theoryPosWeight)
|
|
206 |
print 'bestAvgRecall100 %s bestNBDefaultPriorWeight %s bestNBDefVal %s bestNBPosWeight %s bestSineFeatures %s bestSineWeight %s',bestAvgRecall100,bestNBDefaultPriorWeight,bestNBDefVal,bestNBPosWeight,bestSineFeatures,bestSineWeight
|
53100
|
207 |
|
|
208 |
"""
|
|
209 |
# Test 2
|
|
210 |
#inputDir = '../data/20130118/Jinja'
|
|
211 |
inputDir = '../data/notheory/Prob'
|
|
212 |
inputFile = inputDir+'/mash_commands'
|
|
213 |
#inputFile = inputDir+'/mash_prover_commands'
|
|
214 |
|
|
215 |
#depFile = 'mash_prover_dependencies'
|
|
216 |
depFile = 'mash_dependencies'
|
|
217 |
outputDir = '../tmp/'
|
|
218 |
numberOfPredictions = 1024
|
|
219 |
predictionFile = '../tmp/auth.pred'
|
|
220 |
logFile = '../tmp/auth.log'
|
|
221 |
learnTheories = False
|
|
222 |
theoryDefValPos = -7.5
|
|
223 |
theoryDefValNeg = -10.0
|
|
224 |
theoryPosWeight = 2.0
|
|
225 |
NBDefaultPriorWeight = 20.0
|
|
226 |
NBDefVal =- 15.0
|
|
227 |
NBPosWeight = 10.0
|
|
228 |
sineFeatures = False
|
|
229 |
sineWeight = 0.5
|
|
230 |
quiet = False
|
|
231 |
print inputDir
|
|
232 |
|
|
233 |
#predef = inputDir+'/mash_prover_suggestions'
|
|
234 |
predef = inputDir+'/mash_suggestions'
|
|
235 |
print 'Mash Isar'
|
|
236 |
run_mash(0,inputDir,logFile,predictionFile,predef,\
|
|
237 |
learnTheories,theoryDefValPos,theoryDefValNeg,theoryPosWeight,\
|
|
238 |
NBDefaultPriorWeight,NBDefVal,NBPosWeight,\
|
|
239 |
sineFeatures,sineWeight,quiet=quiet)
|
|
240 |
|
|
241 |
#"""
|
|
242 |
predef = inputDir+'/mesh_suggestions'
|
|
243 |
#predef = inputDir+'/mesh_prover_suggestions'
|
|
244 |
print 'Mesh Isar'
|
|
245 |
run_mash(0,inputDir,logFile,predictionFile,predef,\
|
|
246 |
learnTheories,theoryDefValPos,theoryDefValNeg,theoryPosWeight,\
|
|
247 |
NBDefaultPriorWeight,NBDefVal,NBPosWeight,\
|
|
248 |
sineFeatures,sineWeight,quiet=quiet)
|
|
249 |
#"""
|
|
250 |
predef = inputDir+'/mepo_suggestions'
|
|
251 |
print 'Mepo Isar'
|
|
252 |
run_mash(0,inputDir,logFile,predictionFile,predef,\
|
|
253 |
learnTheories,theoryDefValPos,theoryDefValNeg,theoryPosWeight,\
|
|
254 |
NBDefaultPriorWeight,NBDefVal,NBPosWeight,\
|
|
255 |
sineFeatures,sineWeight,quiet=quiet)
|
|
256 |
|
|
257 |
"""
|
|
258 |
inputFile = inputDir+'/mash_prover_commands'
|
|
259 |
depFile = 'mash_prover_dependencies'
|
|
260 |
|
|
261 |
predef = inputDir+'/mash_prover_suggestions'
|
|
262 |
print 'Mash Prover Isar'
|
|
263 |
run_mash(0,inputDir,logFile,predictionFile,predef,\
|
|
264 |
learnTheories,theoryDefValPos,theoryDefValNeg,theoryPosWeight,\
|
|
265 |
NBDefaultPriorWeight,NBDefVal,NBPosWeight,\
|
|
266 |
sineFeatures,sineWeight,quiet=quiet)
|
|
267 |
|
|
268 |
predef = inputDir+'/mesh_prover_suggestions'
|
|
269 |
print 'Mesh Prover Isar'
|
|
270 |
run_mash(0,inputDir,logFile,predictionFile,predef,\
|
|
271 |
learnTheories,theoryDefValPos,theoryDefValNeg,theoryPosWeight,\
|
|
272 |
NBDefaultPriorWeight,NBDefVal,NBPosWeight,\
|
|
273 |
sineFeatures,sineWeight,quiet=quiet)
|
|
274 |
|
|
275 |
predef = inputDir+'/mepo_suggestions'
|
|
276 |
print 'Mepo Prover Isar'
|
|
277 |
run_mash(0,inputDir,logFile,predictionFile,predef,\
|
|
278 |
learnTheories,theoryDefValPos,theoryDefValNeg,theoryPosWeight,\
|
|
279 |
NBDefaultPriorWeight,NBDefVal,NBPosWeight,\
|
|
280 |
sineFeatures,sineWeight,quiet=quiet)
|
|
281 |
#""" |