-
Notifications
You must be signed in to change notification settings - Fork 1
/
learner.py
235 lines (201 loc) · 9.99 KB
/
learner.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
import logging
import math
import random
import sys
from optparse import OptionParser
from copy import deepcopy as copy
from automaton import Automaton
from quantizer import AbstractQuantizer
from corpus import read_corpus, normalize_corpus
from aut_dist_cache import DistanceCache
class Learner(object):
def __init__(self, automaton, corpus, checkpoint, pref_prob, distfp,
turns_for_each, factors, temperatures):
"""
Learner class implements a simulated annealing method and
is capable of applying downhill simplex as a preference instead
of random changing if needed.
@param corpus: needs to be normalized with corpus.normalize_corpus()
@param automaton: initialized Automaton instance
@param pref_prob: probability of using downhill preference
@param distfp: distance function pointer (kullback, l1err, sqerr)
@param turns_for_each: iterations at one temperature
@param factors: list of factors, length of list is how many bursts/
epochs we want to run, and every element is a float
which is the factor of the actual epoch
@param temperatures: same length list as factors and the elements are
the tolerance parameters for the simmulated
annealing
"""
self.turns_for_each = turns_for_each
self.preference_probability = pref_prob
self.automaton = automaton
self.corpus = corpus
self.dist_cache = DistanceCache(automaton, corpus)
self.distfp = getattr(Automaton, distfp)
if len(temperatures) != len(factors):
print temperatures
print factors
raise Exception("temperatures has to have the same length as " +
"factors when creating a Learner")
self.factors = factors
self.temps = temperatures
self.checkpoint = checkpoint
self.previous_change_options = None
#self.preferred_node_pair = None
#self.preferred_direction = None
#self.disallowed_node_pair = None
@staticmethod
def create_from_options(automaton, corpus, options):
return Learner(automaton, corpus, checkpoint=None,
pref_prob=options.downhill_factor,
distfp=options.distfp, turns_for_each=options.iter,
factors=options.factors, temperatures=options.temps)
def change_automaton(self, options=None, revert=False):
if not revert:
edge = options["edge"]
factor = options["factor"]
if self.automaton.quantizer is not None:
s1 = edge[0]
s2 = edge[1]
factor = self.automaton.quantizer.shift(
self.automaton.m[s1][s2], factor) - self.automaton.m[s1][s2]
self.automaton.boost_edge(edge, factor)
else:
src, transitions = self.previous_change_options["transitions"]
self.automaton.m[src] = copy(transitions)
def randomize_automaton_change(self, factor):
change_options = {}
s1 = None
s2 = None
states = self.automaton.m.keys()
while True:
s1 = random.choice(states)
if len(self.automaton.m[s1]) < 2:
continue
s2 = random.choice(self.automaton.m[s1].keys())
if self.previous_change_options is None:
break
else:
if not (self.previous_change_options["result"] == False and
(s1, s2) == self.previous_change_options["edge"]):
break
change_options["edge"] = (s1, s2)
factor = (factor if random.random() > 0.5 else -factor)
change_options["factor"] = factor
change_options["transitions"] = (s1, copy(self.automaton.m[s1]))
return change_options
def choose_change_options(self, change_options_random, *args):
if (random.random() < self.preference_probability
and self.previous_change_options is not None):
# downhill
change_options = self.previous_change_options
else:
change_options = change_options_random(*args)
return change_options
def simulated_annealing(self, compute_energy, change_something,
change_back, option_randomizer):
energy = compute_energy()
for factor, temperature in zip(self.factors, self.temps):
for turn_count in xrange(self.turns_for_each):
#if turn_count == 0:
#logging.info("SA e={0}".format(energy) +
#" f={0} t={1}".format(factor, temperature))
if turn_count * 10 % self.turns_for_each == 0:
logging.info("SA tc ={0} e={1}".format(turn_count, energy) +
" f={0} t={1}".format(factor, temperature))
change_options = self.choose_change_options(option_randomizer,
factor)
change_something(change_options)
new_energy = compute_energy()
energy_change = new_energy - energy
self.previous_change_options = change_options
if energy_change < 0:
accept = True
else:
still_accepting_probability = random.random()
accept = (still_accepting_probability <
math.exp(-energy_change/temperature))
if accept:
energy = new_energy
self.previous_change_options["result"] = True
logging.debug("Accepting {0} energy change".format(
energy_change))
else:
self.previous_change_options["result"] = False
change_back()
if self.checkpoint:
self.checkpoint(factor, temperature)
def main(self):
compute_energy = lambda: self.dist_cache.distance(self.distfp)
change_something = lambda x: self.change_automaton(x, False)
change_back = lambda: self.change_automaton(None, True)
option_randomizer = lambda x: self.randomize_automaton_change(x)
self.simulated_annealing(compute_energy, change_something,
change_back, option_randomizer)
def csl2l_callback(option, opt, value, parser):
setattr(parser.values, option.dest, [float(_) for _ in value.split(',')])
def optparser():
parser = OptionParser()
parser.add_option("-f", "--factors", dest="factors", help="comma " +
"separated list of change factors " +
"[default=%default]", default="2,1",
type="string", action="callback",
callback=csl2l_callback)
parser.add_option("-t", "--temps", dest="temps", default="1e-5,1e-6",
type="string", help="temperatures " +
"[default=%default]", action="callback",
callback=csl2l_callback)
parser.add_option("-i", "--iter",dest="iter", type="int", default=500,
help="number of iterations per temperature " +
"[default=%default]", metavar="I")
parser.add_option("-d", "--distance", dest="distfp", type="choice",
metavar="I", help="distance method",
choices=["kullback", "l1err", "squarerr"])
parser.add_option("-D", "--downhill-factor", dest="downhill_factor",
metavar="PROBABILITY", default=0.0, type="float",
help="in random parameter selection prefer the one " +
"which improved the result in the previous iteration " +
"with PROBABILITY [default=%default]")
parser.add_option("-o", "--output", dest="output", metavar="FILE",
type="str", default=None, help="File containing the " +
"dump of the learnt automaton [default=stdout]")
parser.add_option("-a", "--automaton-file", dest="automaton_file",
metavar="FILE", type="str", default=None,
help="File containing the dump of the input automaton")
parser.add_option("-q", "--quantizer", dest="quantizer", type="str",
default=None, metavar="FILE",
help="store parameters using a quantizer")
parser.add_option("-c", "--corpus", dest="corpus",
default=None, type="str", help="optimize automaton " +
"on given corpus. Can be used together with -s. " +
"[default=stdin]. See tst/corpus.sample",
metavar="FILENAME")
parser.add_option("-s", "--separator",dest="separator", type="str",
help="separator of letters in corpus (allows using " +
" complex letters, ie. labels)", metavar="SEPARATOR",
default="")
(options, args) = parser.parse_args()
return options
def main(options):
if not options.automaton_file:
raise Exception("Automaton \"option\" (-a) is mandatory")
automaton = Automaton.create_from_dump(open(options.automaton_file))
if options.quantizer:
automaton.quantizer = AbstractQuantizer.read(open(options.quantizer))
automaton.round_and_normalize()
input_ = sys.stdin
if options.corpus:
input_ = open(options.corpus)
corpus = read_corpus(input_, options.separator)
corpus = normalize_corpus(corpus)
learner = Learner.create_from_options(automaton, corpus, options)
learner.main()
output = sys.stdout
if options.output:
output = open(options.output, "w")
learner.automaton.dump(output)
if __name__ == "__main__":
options = optparser()
logging.basicConfig(level=logging.INFO, format="%(asctime)s : %(module)s (%(lineno)s) - %(levelname)s - %(message)s")
main(options)