summaryrefslogtreecommitdiff
path: root/gi/posterior-regularisation/train_pr_parallel.py
blob: 3b9cefed3048043c7ad8d6d461880955ff1ae227 (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import sys
import scipy.optimize
from numpy import *
from numpy.random import random, seed

#
# Step 1: load the concordance counts
# 

edges_phrase_to_context = []
edges_context_to_phrase = []
types = {}
context_types = {}
num_edges = 0

for line in sys.stdin:
    phrase, rest = line.strip().split('\t')
    parts = rest.split('|||')
    edges_phrase_to_context.append((phrase, []))
    for i in range(0, len(parts), 2):
        context, count = parts[i:i+2]

        ctx = tuple(filter(lambda x: x != '<PHRASE>', context.split()))
        cnt = int(count.strip()[2:])
        edges_phrase_to_context[-1][1].append((ctx, cnt))

        cid = context_types.get(ctx, len(context_types))
        if cid == len(context_types):
            context_types[ctx] = cid
            edges_context_to_phrase.append((ctx, []))
        edges_context_to_phrase[cid][1].append((phrase, cnt))

        for token in ctx:
            types.setdefault(token, len(types))
        for token in phrase.split():
            types.setdefault(token, len(types))

        num_edges += 1

#
# Step 2: initialise the model parameters
#

num_tags = 25
num_types = len(types)
num_phrases = len(edges_phrase_to_context)
num_contexts = len(edges_context_to_phrase)
delta = float(sys.argv[1])
assert sys.argv[2] in ('local', 'global')
local = sys.argv[2] == 'local'
if len(sys.argv) >= 2:
     seed(int(sys.argv[3]))

print 'Read in', num_edges, 'edges', num_phrases, 'phrases', num_contexts, 'contexts and', len(types), 'word types'

def normalise(a):
    return a / float(sum(a))

# Pr(tag | phrase)
tagDist = [normalise(random(num_tags)+1) for p in range(num_phrases)]
# Pr(context at pos i = w | tag) indexed by i, tag, word
contextWordDist = [[normalise(random(num_types)+1) for t in range(num_tags)] for i in range(4)]

#
# Step 3: expectation maximisation
#

class GlobalDualObjective:
    """
    Objective, log(z), for all phrases s.t. lambda >= 0, sum_c lambda_pct <= scale 
    """

    def __init__(self, scale):
        self.scale = scale
        self.posterior = zeros((num_edges, num_tags))
        self.q = zeros((num_edges, num_tags))
        self.llh = 0

        index = 0
        for j, (phrase, edges) in enumerate(edges_phrase_to_context):
            for context, count in edges:
                for t in range(num_tags):
                    prob = tagDist[j][t]
                    for k, token in enumerate(context):
                        prob *= contextWordDist[k][t][types[token]]
                    self.posterior[index,t] = prob
                z = sum(self.posterior[index,:])
                self.posterior[index,:] /= z
                self.llh += log(z) * count
                index += 1

    def objective(self, ls):
        ls = ls.reshape((num_edges, num_tags))
        logz = 0

        index = 0
        for j, (phrase, edges) in enumerate(edges_phrase_to_context):
            for context, count in edges:
                for t in range(num_tags):
                    self.q[index,t] = self.posterior[index,t] * exp(-ls[index,t])
                local_z = sum(self.q[index,:])
                self.q[index,:] /= local_z
                logz += log(local_z) * count
                index += 1

        return logz

    # FIXME: recomputes q many more times than necessary

    def gradient(self, ls):
        ls = ls.reshape((num_edges, num_tags))
        gradient = zeros((num_edges, num_tags))

        index = 0
        for j, (phrase, edges) in enumerate(edges_phrase_to_context):
            for context, count in edges:
                for t in range(num_tags):
                    self.q[index,t] = self.posterior[index,t] * exp(-ls[index,t])
                local_z = sum(self.q[index,:])
                self.q[index,:] /= local_z
                for t in range(num_tags):
                    gradient[index,t] -= self.q[index,t] * count
                index += 1

        return gradient.ravel()

    def constraints(self, ls):
        ls = ls.reshape((num_edges, num_tags))
        cons = ones((num_phrases, num_tags)) * self.scale
        index = 0
        for j, (phrase, edges) in enumerate(edges_phrase_to_context):
            for i, (context, count) in enumerate(edges):
                for t in range(num_tags):
                    cons[j,t] -= ls[index,t] * count
                index += 1
        return cons.ravel()

    def constraints_gradient(self, ls):
        ls = ls.reshape((num_edges, num_tags))
        gradient = zeros((num_phrases, num_tags, num_edges, num_tags))
        index = 0
        for j, (phrase, edges) in enumerate(edges_phrase_to_context):
            for i, (context, count) in enumerate(edges):
                for t in range(num_tags):
                    gradient[j,t,index,t] -= count
                index += 1
        return gradient.reshape((num_phrases*num_tags, num_edges*num_tags))

    def optimize(self):
        ls = zeros(num_edges * num_tags)
        #print '\tpre lambda optimisation dual', self.objective(ls) #, 'primal', primal(lamba)
        ls = scipy.optimize.fmin_slsqp(self.objective, ls,
                                bounds=[(0, self.scale)] * num_edges * num_tags,
                                f_ieqcons=self.constraints,
                                fprime=self.gradient,
                                fprime_ieqcons=self.constraints_gradient,
                                iprint=0) # =2 for verbose
        #print '\tpost lambda optimisation dual', self.objective(ls) #, 'primal', primal(lamba)

        # returns llh, kl and l1lmax contribution
        l1lmax = 0
        index = 0
        for j, (phrase, edges) in enumerate(edges_phrase_to_context):
            for t in range(num_tags):
                lmax = None
                for i, (context, count) in enumerate(edges):
                    lmax = max(lmax, self.q[index+i,t])
                l1lmax += lmax
            index += len(edges)

        return self.llh, -self.objective(ls) + dot(ls, self.gradient(ls)), l1lmax

class LocalDualObjective:
    """
    Local part of objective, log(z) relevant to lambda_p**.
    Optimised subject to lambda >= 0, sum_c lambda_pct <= scale forall t 
    """

    def __init__(self, phraseId, scale):
        self.phraseId = phraseId
        self.scale = scale
        edges = edges_phrase_to_context[self.phraseId][1]
        self.posterior = zeros((len(edges), num_tags))
        self.q = zeros((len(edges), num_tags))
        self.llh = 0

        for i, (context, count) in enumerate(edges):
            for t in range(num_tags):
                prob = tagDist[phraseId][t]
                for j, token in enumerate(context):
                    prob *= contextWordDist[j][t][types[token]]
                self.posterior[i,t] = prob
            z = sum(self.posterior[i,:])
            self.posterior[i,:] /= z
            self.llh += log(z) * count

    def objective(self, ls):
        edges = edges_phrase_to_context[self.phraseId][1]
        ls = ls.reshape((len(edges), num_tags))
        logz = 0

        for i, (context, count) in enumerate(edges):
            for t in range(num_tags):
                self.q[i,t] = self.posterior[i,t] * exp(-ls[i,t])
            local_z = sum(self.q[i,:])
            self.q[i,:] /= local_z
            logz += log(local_z) * count

        return logz

    # FIXME: recomputes q many more times than necessary

    def gradient(self, ls):
        edges = edges_phrase_to_context[self.phraseId][1]
        ls = ls.reshape((len(edges), num_tags))
        gradient = zeros((len(edges), num_tags))

        for i, (context, count) in enumerate(edges):
            for t in range(num_tags):
                self.q[i,t] = self.posterior[i,t] * exp(-ls[i,t])
            local_z = sum(self.q[i,:])
            self.q[i,:] /= local_z
            for t in range(num_tags):
                gradient[i,t] -= self.q[i,t] * count

        return gradient.ravel()

    def constraints(self, ls):
        edges = edges_phrase_to_context[self.phraseId][1]
        ls = ls.reshape((len(edges), num_tags))
        cons = ones(num_tags) * self.scale
        for t in range(num_tags):
            for i, (context, count) in enumerate(edges):
                cons[t] -= ls[i,t] * count
        return cons

    def constraints_gradient(self, ls):
        edges = edges_phrase_to_context[self.phraseId][1]
        ls = ls.reshape((len(edges), num_tags))
        gradient = zeros((num_tags, len(edges), num_tags))
        for t in range(num_tags):
            for i, (context, count) in enumerate(edges):
                gradient[t,i,t] -= count
        return gradient.reshape((num_tags, len(edges)*num_tags))

    def optimize(self, ls=None):
        edges = edges_phrase_to_context[self.phraseId][1]
        if ls == None:
            ls = zeros(len(edges) * num_tags)
        #print '\tpre lambda optimisation dual', self.objective(ls) #, 'primal', primal(lamba)
        ls = scipy.optimize.fmin_slsqp(self.objective, ls,
                                bounds=[(0, self.scale)] * len(edges) * num_tags,
                                f_ieqcons=self.constraints,
                                fprime=self.gradient,
                                fprime_ieqcons=self.constraints_gradient,
                                iprint=0) # =2 for verbose
        #print '\tlambda', list(ls)
        #print '\tpost lambda optimisation dual', self.objective(ls) #, 'primal', primal(lamba)

        # returns llh, kl and l1lmax contribution
        l1lmax = 0
        for t in range(num_tags):
            lmax = None
            for i, (context, count) in enumerate(edges):
                lmax = max(lmax, self.q[i,t])
            l1lmax += lmax

        return self.llh, -self.objective(ls) + dot(ls, self.gradient(ls)), l1lmax, ls

ls = [None] * num_phrases
for iteration in range(20):
    tagCounts = [zeros(num_tags) for p in range(num_phrases)]
    contextWordCounts = [[zeros(num_types) for t in range(num_tags)] for i in range(4)]

    # E-step
    llh = kl = l1lmax = 0
    if local:
        for p in range(num_phrases):
            o = LocalDualObjective(p, delta)
            #print '\toptimising lambda for phrase', p, '=', edges_phrase_to_context[p][0]
            #print '\toptimising lambda for phrase', p, 'ls', ls[p]
            obj = o.optimize(ls[p])
            #print '\tphrase', p, 'deltas', obj
            llh += obj[0]
            kl += obj[1]
            l1lmax += obj[2]
            ls[p] = obj[3]

            edges = edges_phrase_to_context[p][1]
            for j, (context, count) in enumerate(edges):
                for t in range(num_tags):
                    tagCounts[p][t] += count * o.q[j,t]
                for i in range(4):
                    for t in range(num_tags):
                        contextWordCounts[i][t][types[context[i]]] += count * o.q[j,t]

        #print 'iteration', iteration, 'LOCAL objective', (llh + kl + delta * l1lmax), 'llh', llh, 'kl', kl, 'l1lmax', l1lmax
    else:
        o = GlobalDualObjective(delta)
        obj = o.optimize()
        llh, kl, l1lmax = o.optimize()

        index = 0
        for p, (phrase, edges) in enumerate(edges_phrase_to_context):
            for context, count in edges:
                for t in range(num_tags):
                    tagCounts[p][t] += count * o.q[index,t]
                for i in range(4):
                    for t in range(num_tags):
                        contextWordCounts[i][t][types[context[i]]] += count * o.q[index,t]
                index += 1

    print 'iteration', iteration, 'objective', (llh - kl - delta * l1lmax), 'llh', llh, 'kl', kl, 'l1lmax', l1lmax

    # M-step
    for p in range(num_phrases):
        tagDist[p] = normalise(tagCounts[p])
    for i in range(4):
        for t in range(num_tags):
            contextWordDist[i][t] = normalise(contextWordCounts[i][t])

for p, (phrase, ccs) in enumerate(edges_phrase_to_context):
    for context, count in ccs:
        conditionals = zeros(num_tags)
        for t in range(num_tags):
            prob = tagDist[p][t]
            for i in range(4):
                prob *= contextWordDist[i][t][types[context[i]]]
            conditionals[t] = prob
        cz = sum(conditionals)
        conditionals /= cz

        print '%s\t%s ||| C=%d |||' % (phrase, context, argmax(conditionals)), conditionals