-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment.py
79 lines (68 loc) · 3.01 KB
/
experiment.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
import time
from output import outputGenerator
from loggers import logs
from synch.synchronization import HedgedActiveSync, NoSyncOperator
import copy
time_format = '%Y-%m-%d %H-%M-%S'
current_timestamp = ""
def outputStartTime():
start_time = time.time()
timestamp = time.strftime(time_format, time.localtime(start_time))
print "Experiment started at %s" % (timestamp)
return start_time
def outputEndTime(start_time):
end_time = time.time()
print "Experiment ended at %s" % (time.strftime(time_format, time.localtime(end_time)))
seconds = end_time - start_time
minutes = int(seconds) / 60
rest = int(seconds - minutes * 60)
print "Duration: %d minutes and %d seconds" % ((int(minutes)), int(rest))
def setCurrentTimestamp(start_time):
global current_timestamp
current_timestamp = time.strftime(time_format, time.localtime(start_time))
def run(input_stream, envs, stopping_condition, report_at = 10000):
global current_round, current_timestamp
start_time = outputStartTime()
setCurrentTimestamp(start_time)
outputGenerator.create_experiment_folder_structure(current_timestamp)
outputGenerator.experiment_summary(current_timestamp, input_stream, envs)
while(not stopping_condition(input_stream)):
example = input_stream.generate_example()
for env in envs:
env.process_example(example)
logs.process_round()
outputEndTime(start_time)
outputGenerator.experiment_number_of_rounds(current_timestamp,input_stream.current_round-1)
logs.close_handles()
outputGenerator.write_experiment_result_file(current_timestamp, envs)
outputGenerator.output_generate_chart_script(current_timestamp, envs)
def runParameterEvaluation(origInputStream, envs, numberOfExamples):
inputStream = copy.deepcopy(origInputStream)
print "Start parameter evaluation:"
for env in envs:
paramEvalEnvironment = env.clone()
print paramEvalEnvironment, " ",
updateRule = paramEvalEnvironment.updateRule
possParams = updateRule.getPossParams()
errors = {}
for idx in xrange(len(possParams)):
exampleCount = 0
print ".",
params = possParams[idx]
paramEvalEnvironment.updateRule.setParams(params)
while (exampleCount <= numberOfExamples):
example = inputStream.generate_example()
paramEvalEnvironment.processExampleForParamEval(example)
exampleCount += 1
errors[idx] = paramEvalEnvironment.total_error
paramEvalEnvironment.total_error = 0.0
inputStream.reset()
bestParamIdx = min(errors, key=errors.get)
bestParam = possParams[bestParamIdx]
paramStr = "{"
for param in bestParam:
paramStr += str(param) + ":" + str(bestParam[param]) + ", "
paramStr = paramStr[:-2] + "}"
print ""
print " best params: ", paramStr
env.updateRule.setParams(bestParam)