forked from lukovnikov/teafacto
/
fbmfqalearn.py
193 lines (175 loc) · 6.74 KB
/
fbmfqalearn.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
from teafacto.blocks.kgraph.fbencdec import FBSeqCompositeEncDec, FBSeqCompositeEncMemDec, FBMemMatch, FBSeqCompEncMemDecAtt
from teafacto.blocks.memory import LinearGateMemAddr, GeneralDotMemAddr
from teafacto.blocks.lang.wordvec import Glove
from teafacto.feed.freebasefeeders import getentdict, getglovedict, FreebaseSeqFeedMaker, FreebaseSeqFeedMakerEntidxs
from teafacto.feed.langtransform import WordToWordCharTransform
from teafacto.util import argprun, ticktock
import numpy as np
from IPython import embed
def loaddata(glovepath, fbentdicp, fblexpath, wordoffset, numwords, numchars):
tt = ticktock("fblexdataloader") ; tt.tick()
gd, vocnumwords = getglovedict(glovepath, offset=wordoffset)
tt.tock("loaded %d worddic" % len(gd)).tick()
ed, vocnuments = getentdict(fbentdicp, offset=0)
tt.tock("loaded %d entdic" % len(ed)).tick()
indata = FreebaseSeqFeedMakerEntidxs(fblexpath, gd, ed, numwords=numwords, numchars=numchars, unkwordid=wordoffset - 1)
datanuments = np.max(indata.goldfeed)+1
tt.tick()
indata.trainfeed[0:9000]
tt.tock("transformed")
#embed()
traindata = indata.trainfeed
golddata = indata.goldfeed + 1 # no entity = id 0
return traindata, golddata, vocnuments, vocnumwords, datanuments+1, ed, gd
def shiftdata(x, right=1):
if isinstance(x, np.ndarray):
return np.concatenate([np.zeros_like(x[:, 0:right]), x[:, :-right]], axis=1)
else:
raise Exception("can not shift this")
def load_lex_data(lexp, maxid, worddic): # load the provided (id-based) lexical data up to maxid
sftrans = WordToWordCharTransform(worddic, unkwordid=1, numwords=20, numchars=30)
procsf = lambda x: FreebaseSeqFeedMakerEntidxs._process_sf(x, 20, 30)
c = 0
with open(lexp) as f:
coll = [[None]*20]
entids = [0]
for line in f:
line = line.lower()
idx, sf = line[:-1].split("\t")
idx = int(idx)+1
if idx >= maxid:
break
entids.append(idx)
coll.append(procsf(sf))
c += 1
ret = sftrans.transform(np.asarray(coll))
ret[0, :, :] = 0
return np.asarray(entids), ret
def run(
epochs=100,
lr=0.01,
wreg=0.0001,
numbats=10,
fbdatapath="../../data/mfqa/mfqa.tsv.sample.small",
fblexpath="../../data/mfqa/mfqa.labels.idx.map",
glovepath="../../data/glove/glove.6B.50d.txt",
fbentdicp="../../data/mfqa/mfqa.dic.map",
numwords=20,
numchars=30,
wordembdim=50,
wordencdim=100,
entembdim=100,
innerdim=200,
attdim=200,
wordoffset=1,
validinter=1,
gradnorm=1.0,
validsplit=1,
vocnumwordsres=50e3,
model="mem",
):
tt = ticktock("fblextransrun")
traindata, golddata, vocnuments, vocnumwords, datanuments, entdic, worddic = \
loaddata(glovepath, fbentdicp, fbdatapath, wordoffset, numwords, numchars)
outdata = shiftdata(golddata)
tt.tock("made data").tick()
entids, lexdata = load_lex_data(fblexpath, datanuments, worddic)
if "mem" in model:
print lexdata.shape
print datanuments
#embed()
if "att" in model:
print "model with attention AND memory"
m = FBSeqCompEncMemDecAtt(
wordembdim=wordembdim,
wordencdim=wordencdim,
entembdim=entembdim,
innerdim=innerdim,
outdim=datanuments,
numchars=128, # ASCII
numwords=vocnumwords,
memdata=[entids, lexdata],
attdim=attdim,
memaddr=GeneralDotMemAddr,
)
else:
m = FBSeqCompositeEncMemDec(
wordembdim=wordembdim,
wordencdim=wordencdim,
entembdim=entembdim,
innerdim=innerdim,
outdim=datanuments,
numchars=128, # ASCII
numwords=vocnumwords,
memdata=[entids, lexdata],
attdim=attdim,
memaddr=LinearGateMemAddr,
)
elif model=="lex": # for testing purposes
print lexdata.shape
print datanuments
#vocnumwords = 4000
#exit()
#embed()
m = FBMemMatch(
wordembdim=wordembdim,
wordencdim=wordencdim,
entembdim=entembdim,
innerdim=innerdim,
outdim=datanuments,
numchars=128,
numwords=vocnumwords,
memdata=[entids, lexdata],
attdim=attdim,
)
elif model=="nomem":
m = FBSeqCompositeEncDec( # compiles, errors go down
wordembdim=wordembdim,
wordencdim=wordencdim,
entembdim=entembdim,
innerdim=innerdim,
outdim=datanuments,
numchars=128,
numwords=vocnumwords
)
else:
m = None
print "no such model"
reventdic = {}
for k, v in entdic.items():
reventdic[v] = k
#wenc = WordEncoderPlusGlove(numchars=numchars, numwords=vocnumwords, encdim=wordencdim, embdim=wordembdim)
tt.tock("model defined")
if model == "lex": # for testing purposes
tt.tick("predicting")
print lexdata[1:5].shape, entids[1:5].shape
#print lexdata[1:5]
print entids[1:5]
pred = m.predict(lexdata[1:5])
print pred.shape
print np.argmax(pred, axis=1)-1
print np.vectorize(lambda x: reventdic[x] if x in reventdic else None)(np.argmax(pred, axis=1)-1)
tt.tock("predicted sample")
tt.tick("training")
m.train([lexdata[1:151]], entids[1:151]).adagrad(lr=lr).cross_entropy().grad_total_norm(0.5)\
.split_validate(5, random=True).validinter(validinter).accuracy()\
.train(numbats, epochs)
else:
#embed()
tt.tick("predicting")
print traindata[:5].shape, outdata[:5].shape
pred = m.predict(traindata[:5], outdata[:5])
print np.argmax(pred, axis=2)-1
print np.vectorize(lambda x: reventdic[x])(np.argmax(pred, axis=2)-1)
tt.tock("predicted sample")
tt.tick("training")
m.train([traindata, outdata], golddata).adagrad(lr=lr).grad_total_norm(gradnorm).seq_cross_entropy()\
.split_validate(splits=5, random=False).validinter(validinter).seq_accuracy().seq_cross_entropy()\
.train(numbats, epochs)
#embed()
tt.tock("trained").tick("predicting")
pred = m.predict(traindata[:50], outdata[:50])
print np.vectorize(lambda x: reventdic[x])(np.argmax(pred, axis=2)-1)
tt.tock("predicted sample")
if __name__ == "__main__":
argprun(run, model="mem att")