2019-05-07 17:03:42 +03:00
|
|
|
# coding: utf-8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import os
|
|
|
|
import datetime
|
|
|
|
from os import listdir
|
2019-05-14 23:55:56 +03:00
|
|
|
import numpy as np
|
2019-05-16 19:25:34 +03:00
|
|
|
import random
|
|
|
|
from thinc.neural._classes.convolution import ExtractWindow
|
2019-05-07 17:03:42 +03:00
|
|
|
|
|
|
|
from examples.pipeline.wiki_entity_linking import run_el, training_set_creator, kb_creator
|
2019-05-09 18:23:19 +03:00
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
from spacy._ml import SpacyVectors, create_default_optimizer, zero_init, logistic
|
2019-05-09 18:23:19 +03:00
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
from thinc.api import chain, concatenate, flatten_add_lengths, with_getitem, clone, with_flatten
|
2019-05-14 23:55:56 +03:00
|
|
|
from thinc.neural.util import get_array_module
|
2019-05-14 09:37:52 +03:00
|
|
|
from thinc.v2v import Model, Softmax, Maxout, Affine, ReLu
|
2019-05-16 19:25:34 +03:00
|
|
|
from thinc.t2v import Pooling, sum_pool, mean_pool, max_pool
|
2019-05-14 09:37:52 +03:00
|
|
|
from thinc.t2t import ParametricAttention
|
|
|
|
from thinc.misc import Residual
|
2019-05-16 19:25:34 +03:00
|
|
|
from thinc.misc import LayerNorm as LN
|
2019-05-07 17:03:42 +03:00
|
|
|
|
2019-05-13 15:26:04 +03:00
|
|
|
from spacy.tokens import Doc
|
|
|
|
|
2019-05-07 17:03:42 +03:00
|
|
|
""" TODO: this code needs to be implemented in pipes.pyx"""
|
|
|
|
|
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
class EL_Model:
|
2019-05-09 18:23:19 +03:00
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
PRINT_LOSS = True
|
2019-05-14 23:55:56 +03:00
|
|
|
PRINT_F = True
|
2019-05-15 03:23:08 +03:00
|
|
|
EPS = 0.0000000005
|
2019-05-16 19:25:34 +03:00
|
|
|
CUTOFF = 0.5
|
|
|
|
|
|
|
|
INPUT_DIM = 300
|
|
|
|
ENTITY_WIDTH = 64
|
|
|
|
ARTICLE_WIDTH = 64
|
|
|
|
HIDDEN_1_WIDTH = 256
|
|
|
|
HIDDEN_2_WIDTH = 64
|
2019-05-14 23:55:56 +03:00
|
|
|
|
2019-05-09 18:23:19 +03:00
|
|
|
name = "entity_linker"
|
|
|
|
|
|
|
|
def __init__(self, kb, nlp):
|
|
|
|
run_el._prepare_pipeline(nlp, kb)
|
|
|
|
self.nlp = nlp
|
|
|
|
self.kb = kb
|
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
self._build_cnn(hidden_entity_width=self.ENTITY_WIDTH, hidden_article_width=self.ARTICLE_WIDTH)
|
|
|
|
|
2019-05-14 09:37:52 +03:00
|
|
|
def train_model(self, training_dir, entity_descr_output, trainlimit=None, devlimit=None, to_print=True):
|
2019-05-16 19:25:34 +03:00
|
|
|
# raise errors instead of runtime warnings in case of int/float overflow
|
|
|
|
np.seterr(all='raise')
|
|
|
|
|
2019-05-13 15:26:04 +03:00
|
|
|
Doc.set_extension("entity_id", default=None)
|
|
|
|
|
|
|
|
train_instances, train_pos, train_neg, train_doc = self._get_training_data(training_dir,
|
|
|
|
entity_descr_output,
|
|
|
|
False,
|
2019-05-14 09:37:52 +03:00
|
|
|
trainlimit,
|
2019-05-16 19:25:34 +03:00
|
|
|
to_print=False)
|
2019-05-13 15:26:04 +03:00
|
|
|
|
|
|
|
dev_instances, dev_pos, dev_neg, dev_doc = self._get_training_data(training_dir,
|
|
|
|
entity_descr_output,
|
|
|
|
True,
|
2019-05-14 09:37:52 +03:00
|
|
|
devlimit,
|
2019-05-16 19:25:34 +03:00
|
|
|
to_print=False)
|
|
|
|
self._begin_training()
|
|
|
|
|
|
|
|
if self.PRINT_F:
|
2019-05-16 19:36:15 +03:00
|
|
|
_, _, f_avg_train = -3.42, -3.42, -3.42 # self._test_dev(train_instances, train_pos, train_neg, train_doc, avg=True)
|
2019-05-16 19:25:34 +03:00
|
|
|
_, _, f_nonavg_train = self._test_dev(train_instances, train_pos, train_neg, train_doc, avg=False)
|
|
|
|
_, _, f_random_train = self._test_dev(train_instances, train_pos, train_neg, train_doc, calc_random=True)
|
2019-05-16 19:36:15 +03:00
|
|
|
_, _, f_avg_dev = -3.42, -3.42, -3.42 # self._test_dev(dev_instances, dev_pos, dev_neg, dev_doc, avg=True)
|
2019-05-16 19:25:34 +03:00
|
|
|
_, _, f_nonavg_dev = self._test_dev(dev_instances, dev_pos, dev_neg, dev_doc, avg=False)
|
|
|
|
_, _, f_random_dev = self._test_dev(dev_instances, dev_pos, dev_neg, dev_doc, calc_random=True)
|
|
|
|
|
|
|
|
print("random F train", round(f_random_train, 1))
|
|
|
|
print("random F dev", round(f_random_dev, 1))
|
|
|
|
print()
|
|
|
|
print("avg/nonavg F train", round(f_avg_train, 1), round(f_nonavg_train, 1))
|
|
|
|
print("avg/nonavg F dev", round(f_avg_dev, 1), round(f_nonavg_dev, 1))
|
|
|
|
print()
|
|
|
|
|
|
|
|
instance_pos_count = 0
|
|
|
|
instance_neg_count = 0
|
2019-05-09 18:23:19 +03:00
|
|
|
|
|
|
|
if to_print:
|
2019-05-13 18:02:34 +03:00
|
|
|
print("Training on", len(train_instances.values()), "articles")
|
|
|
|
print("Dev test on", len(dev_instances.values()), "articles")
|
2019-05-09 18:23:19 +03:00
|
|
|
print()
|
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
article_docs = list()
|
|
|
|
entities = list()
|
|
|
|
golds = list()
|
2019-05-13 18:02:34 +03:00
|
|
|
for article_id, inst_cluster_set in train_instances.items():
|
|
|
|
for inst_cluster in inst_cluster_set:
|
2019-05-16 19:25:34 +03:00
|
|
|
article_docs.append(train_doc[article_id])
|
|
|
|
entities.append(train_pos.get(inst_cluster))
|
|
|
|
golds.append(float(1.0))
|
|
|
|
instance_pos_count += 1
|
|
|
|
for neg_entity in train_neg.get(inst_cluster, []):
|
|
|
|
article_docs.append(train_doc[article_id])
|
|
|
|
entities.append(neg_entity)
|
|
|
|
golds.append(float(0.0))
|
|
|
|
instance_neg_count += 1
|
|
|
|
|
|
|
|
for x in range(10):
|
|
|
|
print("Updating", x)
|
|
|
|
self.update(article_docs=article_docs, entities=entities, golds=golds)
|
|
|
|
|
|
|
|
# eval again
|
2019-05-14 23:55:56 +03:00
|
|
|
if self.PRINT_F:
|
2019-05-16 19:36:15 +03:00
|
|
|
_, _, f_avg_train = -3.42, -3.42, -3.42 # self._test_dev(train_instances, train_pos, train_neg, train_doc, avg=True)
|
2019-05-16 19:25:34 +03:00
|
|
|
_, _, f_nonavg_train = self._test_dev(train_instances, train_pos, train_neg, train_doc, avg=False)
|
2019-05-16 19:36:15 +03:00
|
|
|
_, _, f_avg_dev = -3.42, -3.42, -3.42 # self._test_dev(dev_instances, dev_pos, dev_neg, dev_doc, avg=True)
|
2019-05-16 19:25:34 +03:00
|
|
|
_, _, f_nonavg_dev = self._test_dev(dev_instances, dev_pos, dev_neg, dev_doc, avg=False)
|
|
|
|
|
|
|
|
print("avg/nonavg F train", round(f_avg_train, 1), round(f_nonavg_train, 1))
|
|
|
|
print("avg/nonavg F dev", round(f_avg_dev, 1), round(f_nonavg_dev, 1))
|
|
|
|
print()
|
2019-05-13 18:02:34 +03:00
|
|
|
|
|
|
|
if to_print:
|
2019-05-16 19:25:34 +03:00
|
|
|
print("Trained on", instance_pos_count, "/", instance_neg_count, "instances pos/neg")
|
2019-05-13 18:02:34 +03:00
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
def _test_dev(self, dev_instances, dev_pos, dev_neg, dev_doc, avg=False, calc_random=False):
|
|
|
|
predictions = list()
|
|
|
|
golds = list()
|
|
|
|
|
|
|
|
for article_id, inst_cluster_set in dev_instances.items():
|
|
|
|
for inst_cluster in inst_cluster_set:
|
|
|
|
pos_ex = dev_pos.get(inst_cluster)
|
|
|
|
neg_exs = dev_neg.get(inst_cluster, [])
|
|
|
|
|
|
|
|
article = inst_cluster.split(sep="_")[0]
|
|
|
|
entity_id = inst_cluster.split(sep="_")[1]
|
|
|
|
article_doc = dev_doc[article]
|
|
|
|
|
|
|
|
if calc_random:
|
|
|
|
prediction = self._predict_random(entity=pos_ex)
|
|
|
|
else:
|
|
|
|
prediction = self._predict(article_doc=article_doc, entity=pos_ex, avg=avg)
|
|
|
|
predictions.append(prediction)
|
|
|
|
golds.append(float(1.0))
|
|
|
|
|
|
|
|
for neg_ex in neg_exs:
|
|
|
|
if calc_random:
|
|
|
|
prediction = self._predict_random(entity=neg_ex)
|
|
|
|
else:
|
|
|
|
prediction = self._predict(article_doc=article_doc, entity=neg_ex, avg=avg)
|
|
|
|
predictions.append(prediction)
|
|
|
|
golds.append(float(0.0))
|
|
|
|
|
|
|
|
# TODO: use lowest_mse and combine with prior probability
|
|
|
|
p, r, f = run_el.evaluate(predictions, golds, to_print=False)
|
|
|
|
return p, r, f
|
2019-05-13 15:26:04 +03:00
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
def _predict(self, article_doc, entity, avg=False, apply_threshold=True):
|
|
|
|
if avg:
|
|
|
|
with self.sgd.use_params(self.model.averages):
|
|
|
|
doc_encoding = self.article_encoder([article_doc])
|
|
|
|
entity_encoding = self.entity_encoder([entity])
|
|
|
|
return self.model(np.append(entity_encoding, doc_encoding)) # TODO list
|
|
|
|
|
|
|
|
doc_encoding = self.article_encoder([article_doc])[0]
|
|
|
|
entity_encoding = self.entity_encoder([entity])[0]
|
|
|
|
concat_encoding = list(entity_encoding) + list(doc_encoding)
|
|
|
|
np_array = np.asarray([concat_encoding])
|
|
|
|
prediction = self.model(np_array)
|
|
|
|
if not apply_threshold:
|
|
|
|
return float(prediction)
|
|
|
|
if prediction > self.CUTOFF:
|
|
|
|
return float(1.0)
|
|
|
|
return float(0.0)
|
|
|
|
|
|
|
|
def _predict_random(self, entity, apply_threshold=True):
|
|
|
|
r = random.uniform(0, 1)
|
|
|
|
if not apply_threshold:
|
|
|
|
return r
|
|
|
|
if r > self.CUTOFF:
|
|
|
|
return float(1.0)
|
|
|
|
return float(0.0)
|
|
|
|
|
|
|
|
def _build_cnn(self, hidden_entity_width, hidden_article_width):
|
|
|
|
with Model.define_operators({">>": chain, "|": concatenate, "**": clone}):
|
|
|
|
self.entity_encoder = self._encoder(in_width=self.INPUT_DIM, hidden_width=hidden_entity_width) # entity encoding
|
|
|
|
self.article_encoder = self._encoder(in_width=self.INPUT_DIM, hidden_width=hidden_article_width) # doc encoding
|
|
|
|
|
|
|
|
hidden_input_with = hidden_entity_width + hidden_article_width
|
|
|
|
hidden_output_with = self.HIDDEN_1_WIDTH
|
|
|
|
|
|
|
|
convolution_2 = Residual((ExtractWindow(nW=1) >> LN(Maxout(hidden_output_with, hidden_output_with * 3))))
|
|
|
|
|
|
|
|
self.model = Affine(hidden_output_with, hidden_input_with) \
|
|
|
|
>> LN(Maxout(hidden_output_with, hidden_output_with)) \
|
|
|
|
>> convolution_2 \
|
|
|
|
>> Affine(self.HIDDEN_2_WIDTH, hidden_output_with) \
|
|
|
|
>> Affine(1, self.HIDDEN_2_WIDTH) \
|
|
|
|
>> logistic
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _encoder(in_width, hidden_width):
|
|
|
|
with Model.define_operators({">>": chain}):
|
|
|
|
encoder = SpacyVectors \
|
|
|
|
>> flatten_add_lengths \
|
|
|
|
>> ParametricAttention(in_width)\
|
|
|
|
>> Pooling(mean_pool) \
|
|
|
|
>> Residual(zero_init(Maxout(in_width, in_width))) \
|
|
|
|
>> zero_init(Affine(hidden_width, in_width, drop_factor=0.0))
|
|
|
|
|
|
|
|
return encoder
|
|
|
|
|
|
|
|
def _begin_training(self):
|
|
|
|
self.sgd = create_default_optimizer(self.model.ops)
|
|
|
|
|
|
|
|
def update(self, article_docs, entities, golds, drop=0.):
|
|
|
|
doc_encodings, bp_doc = self.article_encoder.begin_update(article_docs, drop=drop)
|
|
|
|
entity_encodings, bp_encoding = self.entity_encoder.begin_update(entities, drop=drop)
|
|
|
|
concat_encodings = [list(entity_encodings[i]) + list(doc_encodings[i]) for i in range(len(entities))]
|
2019-05-14 23:55:56 +03:00
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
predictions, bp_model = self.model.begin_update(np.asarray(concat_encodings), drop=drop)
|
|
|
|
|
|
|
|
predictions = self.model.ops.flatten(predictions)
|
|
|
|
golds = self.model.ops.asarray(golds)
|
|
|
|
|
|
|
|
# print("predictions", predictions)
|
|
|
|
# print("golds", golds)
|
|
|
|
|
|
|
|
d_scores = (predictions - golds) # / predictions.shape[0]
|
|
|
|
# print("d_scores (1)", d_scores)
|
|
|
|
|
|
|
|
loss = (d_scores ** 2).sum()
|
|
|
|
|
|
|
|
if self.PRINT_LOSS:
|
|
|
|
print("loss train", round(loss, 5))
|
2019-05-10 13:53:14 +03:00
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
d_scores = d_scores.reshape((-1, 1))
|
|
|
|
d_scores = d_scores.astype(np.float32)
|
|
|
|
# print("d_scores (2)", d_scores)
|
2019-05-10 13:53:14 +03:00
|
|
|
|
2019-05-16 19:25:34 +03:00
|
|
|
model_gradient = bp_model(d_scores, sgd=self.sgd)
|
|
|
|
|
|
|
|
doc_gradient = [x[0:self.ARTICLE_WIDTH] for x in model_gradient]
|
|
|
|
entity_gradient = [x[self.ARTICLE_WIDTH:] for x in model_gradient]
|
|
|
|
|
|
|
|
bp_doc(doc_gradient)
|
|
|
|
bp_encoding(entity_gradient)
|
|
|
|
|
2019-05-13 15:26:04 +03:00
|
|
|
def _get_training_data(self, training_dir, entity_descr_output, dev, limit, to_print):
|
2019-05-09 18:23:19 +03:00
|
|
|
id_to_descr = kb_creator._get_id_to_description(entity_descr_output)
|
|
|
|
|
|
|
|
correct_entries, incorrect_entries = training_set_creator.read_training_entities(training_output=training_dir,
|
|
|
|
collect_correct=True,
|
|
|
|
collect_incorrect=True)
|
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
instance_by_doc = dict()
|
2019-05-09 18:23:19 +03:00
|
|
|
local_vectors = list() # TODO: local vectors
|
|
|
|
doc_by_article = dict()
|
2019-05-09 19:11:49 +03:00
|
|
|
pos_entities = dict()
|
|
|
|
neg_entities = dict()
|
2019-05-09 18:23:19 +03:00
|
|
|
|
|
|
|
cnt = 0
|
|
|
|
for f in listdir(training_dir):
|
|
|
|
if not limit or cnt < limit:
|
2019-05-13 15:26:04 +03:00
|
|
|
if dev == run_el.is_dev(f):
|
2019-05-09 18:23:19 +03:00
|
|
|
article_id = f.replace(".txt", "")
|
|
|
|
if cnt % 500 == 0 and to_print:
|
2019-05-13 18:02:34 +03:00
|
|
|
print(datetime.datetime.now(), "processed", cnt, "files in the training dataset")
|
2019-05-09 18:23:19 +03:00
|
|
|
cnt += 1
|
|
|
|
if article_id not in doc_by_article:
|
|
|
|
with open(os.path.join(training_dir, f), mode="r", encoding='utf8') as file:
|
|
|
|
text = file.read()
|
|
|
|
doc = self.nlp(text)
|
|
|
|
doc_by_article[article_id] = doc
|
2019-05-13 18:02:34 +03:00
|
|
|
instance_by_doc[article_id] = set()
|
2019-05-07 17:03:42 +03:00
|
|
|
|
2019-05-09 19:11:49 +03:00
|
|
|
for mention, entity_pos in correct_entries[article_id].items():
|
2019-05-07 17:03:42 +03:00
|
|
|
descr = id_to_descr.get(entity_pos)
|
|
|
|
if descr:
|
2019-05-13 18:02:34 +03:00
|
|
|
instance_by_doc[article_id].add(article_id + "_" + mention)
|
2019-05-09 19:11:49 +03:00
|
|
|
doc_descr = self.nlp(descr)
|
2019-05-13 15:26:04 +03:00
|
|
|
doc_descr._.entity_id = entity_pos
|
2019-05-09 19:11:49 +03:00
|
|
|
pos_entities[article_id + "_" + mention] = doc_descr
|
2019-05-07 17:03:42 +03:00
|
|
|
|
2019-05-09 19:11:49 +03:00
|
|
|
for mention, entity_negs in incorrect_entries[article_id].items():
|
2019-05-07 17:03:42 +03:00
|
|
|
for entity_neg in entity_negs:
|
|
|
|
descr = id_to_descr.get(entity_neg)
|
|
|
|
if descr:
|
2019-05-09 19:11:49 +03:00
|
|
|
doc_descr = self.nlp(descr)
|
2019-05-13 15:26:04 +03:00
|
|
|
doc_descr._.entity_id = entity_neg
|
2019-05-09 19:11:49 +03:00
|
|
|
descr_list = neg_entities.get(article_id + "_" + mention, [])
|
|
|
|
descr_list.append(doc_descr)
|
|
|
|
neg_entities[article_id + "_" + mention] = descr_list
|
2019-05-07 17:03:42 +03:00
|
|
|
|
2019-05-09 18:23:19 +03:00
|
|
|
if to_print:
|
|
|
|
print()
|
2019-05-13 18:02:34 +03:00
|
|
|
print("Processed", cnt, "training articles, dev=" + str(dev))
|
2019-05-09 18:23:19 +03:00
|
|
|
print()
|
2019-05-13 18:02:34 +03:00
|
|
|
return instance_by_doc, pos_entities, neg_entities, doc_by_article
|