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-13 15:26:04 +03:00
|
|
|
from random import shuffle
|
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-10 13:53:14 +03:00
|
|
|
from spacy._ml import SpacyVectors, create_default_optimizer, zero_init
|
2019-05-09 18:23:19 +03:00
|
|
|
|
2019-05-14 09:37:52 +03:00
|
|
|
from thinc.api import chain, flatten_add_lengths, with_getitem, clone
|
|
|
|
from thinc.v2v import Model, Softmax, Maxout, Affine, ReLu
|
2019-05-09 18:23:19 +03:00
|
|
|
from thinc.t2v import Pooling, sum_pool, mean_pool
|
2019-05-14 09:37:52 +03:00
|
|
|
from thinc.t2t import ParametricAttention
|
|
|
|
from thinc.misc import Residual
|
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-09 18:23:19 +03:00
|
|
|
class EL_Model():
|
|
|
|
|
|
|
|
labels = ["MATCH", "NOMATCH"]
|
|
|
|
name = "entity_linker"
|
|
|
|
|
|
|
|
def __init__(self, kb, nlp):
|
|
|
|
run_el._prepare_pipeline(nlp, kb)
|
|
|
|
self.nlp = nlp
|
|
|
|
self.kb = kb
|
|
|
|
|
2019-05-10 13:53:14 +03:00
|
|
|
self.entity_encoder = self._simple_encoder(in_width=300, out_width=96)
|
|
|
|
self.article_encoder = self._simple_encoder(in_width=300, out_width=96)
|
2019-05-09 18:23:19 +03:00
|
|
|
|
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-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,
|
|
|
|
to_print)
|
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,
|
|
|
|
to_print)
|
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()
|
|
|
|
|
|
|
|
self.sgd_entity = self.begin_training(self.entity_encoder)
|
|
|
|
self.sgd_article = self.begin_training(self.article_encoder)
|
|
|
|
|
2019-05-13 15:26:04 +03:00
|
|
|
self._test_dev(dev_instances, dev_pos, dev_neg, dev_doc)
|
|
|
|
|
2019-05-09 18:23:19 +03:00
|
|
|
losses = {}
|
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
instance_count = 0
|
|
|
|
|
|
|
|
for article_id, inst_cluster_set in train_instances.items():
|
|
|
|
article_doc = train_doc[article_id]
|
|
|
|
pos_ex_list = list()
|
|
|
|
neg_exs_list = list()
|
|
|
|
for inst_cluster in inst_cluster_set:
|
|
|
|
instance_count += 1
|
|
|
|
pos_ex_list.append(train_pos.get(inst_cluster))
|
|
|
|
neg_exs_list.append(train_neg.get(inst_cluster, []))
|
|
|
|
|
|
|
|
self.update(article_doc, pos_ex_list, neg_exs_list, losses=losses)
|
|
|
|
p, r, fscore = self._test_dev(dev_instances, dev_pos, dev_neg, dev_doc)
|
|
|
|
print(round(fscore, 1))
|
|
|
|
|
|
|
|
if to_print:
|
|
|
|
print("Trained on", instance_count, "instance clusters")
|
|
|
|
|
2019-05-13 15:26:04 +03:00
|
|
|
def _test_dev(self, dev_instances, dev_pos, dev_neg, dev_doc):
|
|
|
|
predictions = list()
|
|
|
|
golds = list()
|
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
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, [])
|
|
|
|
ex_to_id = dict()
|
2019-05-13 15:26:04 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
if pos_ex and neg_exs:
|
|
|
|
ex_to_id[pos_ex] = pos_ex._.entity_id
|
|
|
|
for neg_ex in neg_exs:
|
|
|
|
ex_to_id[neg_ex] = neg_ex._.entity_id
|
2019-05-13 15:26:04 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
article = inst_cluster.split(sep="_")[0]
|
|
|
|
entity_id = inst_cluster.split(sep="_")[1]
|
|
|
|
article_doc = dev_doc[article]
|
2019-05-13 15:26:04 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
examples = list(neg_exs)
|
|
|
|
examples.append(pos_ex)
|
|
|
|
shuffle(examples)
|
2019-05-13 15:26:04 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
best_entity, lowest_mse = self._predict(examples, article_doc)
|
|
|
|
predictions.append(ex_to_id[best_entity])
|
|
|
|
golds.append(ex_to_id[pos_ex])
|
2019-05-13 15:26:04 +03:00
|
|
|
|
|
|
|
# TODO: use lowest_mse and combine with prior probability
|
|
|
|
p, r, F = run_el.evaluate(predictions, golds, to_print=False)
|
|
|
|
return p, r, F
|
|
|
|
|
|
|
|
def _predict(self, entities, article_doc):
|
|
|
|
doc_encoding = self.article_encoder([article_doc])
|
|
|
|
|
|
|
|
lowest_mse = None
|
|
|
|
best_entity = None
|
|
|
|
|
|
|
|
for entity in entities:
|
|
|
|
entity_encoding = self.entity_encoder([entity])
|
|
|
|
mse, _ = self._calculate_similarity(doc_encoding, entity_encoding)
|
|
|
|
if not best_entity or mse < lowest_mse:
|
|
|
|
lowest_mse = mse
|
|
|
|
best_entity = entity
|
|
|
|
|
|
|
|
return best_entity, lowest_mse
|
|
|
|
|
2019-05-10 13:53:14 +03:00
|
|
|
def _simple_encoder(self, in_width, out_width):
|
|
|
|
conv_depth = 1
|
|
|
|
cnn_maxout_pieces = 3
|
|
|
|
with Model.define_operators({">>": chain, "**": clone}):
|
2019-05-09 18:23:19 +03:00
|
|
|
encoder = SpacyVectors \
|
2019-05-14 09:37:52 +03:00
|
|
|
>> flatten_add_lengths \
|
|
|
|
>> ParametricAttention(in_width)\
|
|
|
|
>> Pooling(mean_pool) \
|
|
|
|
>> Residual(zero_init(Maxout(in_width, in_width))) \
|
|
|
|
>> zero_init(Affine(out_width, in_width, drop_factor=0.0))
|
|
|
|
# encoder = SpacyVectors \
|
|
|
|
# >> flatten_add_lengths \
|
|
|
|
# >> with_getitem(0, Affine(in_width, in_width)) \
|
|
|
|
# >> ParametricAttention(in_width) \
|
|
|
|
# >> Pooling(sum_pool) \
|
|
|
|
# >> Residual(ReLu(in_width, in_width)) ** conv_depth \
|
|
|
|
# >> zero_init(Affine(out_width, in_width, drop_factor=0.0))
|
2019-05-10 13:53:14 +03:00
|
|
|
|
|
|
|
# >> zero_init(Affine(nr_class, width, drop_factor=0.0))
|
|
|
|
# >> logistic
|
|
|
|
|
|
|
|
# convolution = Residual(
|
|
|
|
# ExtractWindow(nW=1)
|
|
|
|
# >> LN(Maxout(width, width * 3, pieces=cnn_maxout_pieces))
|
|
|
|
# )
|
|
|
|
|
|
|
|
# embed = SpacyVectors >> LN(Maxout(width, width, pieces=3))
|
|
|
|
|
|
|
|
# encoder = SpacyVectors >> flatten_add_lengths >> convolution ** conv_depth
|
|
|
|
# encoder = with_flatten(embed >> convolution ** conv_depth, pad=conv_depth)
|
2019-05-09 18:23:19 +03:00
|
|
|
|
|
|
|
return encoder
|
|
|
|
|
|
|
|
def begin_training(self, model):
|
|
|
|
# TODO ? link_vectors_to_models(self.vocab)
|
|
|
|
sgd = create_default_optimizer(model.ops)
|
|
|
|
return sgd
|
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
def update(self, article_doc, true_entity_list, false_entities_list, drop=0., losses=None):
|
|
|
|
# TODO: one call only to begin_update ?
|
|
|
|
|
|
|
|
entity_diffs = None
|
|
|
|
doc_diffs = None
|
|
|
|
|
2019-05-09 18:23:19 +03:00
|
|
|
doc_encoding, article_bp = self.article_encoder.begin_update([article_doc], drop=drop)
|
2019-05-07 17:03:42 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
for i, true_entity in enumerate(true_entity_list):
|
|
|
|
false_entities = false_entities_list[i]
|
|
|
|
|
|
|
|
true_entity_encoding, true_entity_bp = self.entity_encoder.begin_update([true_entity], drop=drop)
|
|
|
|
# print("encoding dim", len(true_entity_encoding[0]))
|
2019-05-10 13:53:14 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
consensus_encoding = self._calculate_consensus(doc_encoding, true_entity_encoding)
|
2019-05-10 13:53:14 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
doc_mse, doc_diff = self._calculate_similarity(doc_encoding, consensus_encoding)
|
2019-05-10 13:53:14 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
entity_mses = list()
|
2019-05-10 13:53:14 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
true_mse, true_diffs = self._calculate_similarity(true_entity_encoding, consensus_encoding)
|
|
|
|
# print("true_mse", true_mse)
|
|
|
|
# print("true_diffs", true_diffs)
|
|
|
|
entity_mses.append(true_mse)
|
|
|
|
# true_exp = np.exp(true_entity_encoding.dot(consensus_encoding_t))
|
|
|
|
# print("true_exp", true_exp)
|
2019-05-10 13:53:14 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
# false_exp_sum = 0
|
2019-05-10 13:53:14 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
if doc_diffs is not None:
|
|
|
|
doc_diffs += doc_diff
|
|
|
|
entity_diffs += true_diffs
|
|
|
|
else:
|
|
|
|
doc_diffs = doc_diff
|
|
|
|
entity_diffs = true_diffs
|
2019-05-10 13:53:14 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
for false_entity in false_entities:
|
|
|
|
false_entity_encoding, false_entity_bp = self.entity_encoder.begin_update([false_entity], drop=drop)
|
|
|
|
false_mse, false_diffs = self._calculate_similarity(false_entity_encoding, consensus_encoding)
|
|
|
|
# print("false_mse", false_mse)
|
|
|
|
# false_exp = np.exp(false_entity_encoding.dot(consensus_encoding_t))
|
|
|
|
# print("false_exp", false_exp)
|
|
|
|
# print("false_diffs", false_diffs)
|
|
|
|
entity_mses.append(false_mse)
|
|
|
|
# if false_mse > true_mse:
|
|
|
|
# true_diffs = true_diffs - false_diffs ???
|
|
|
|
# false_exp_sum += false_exp
|
2019-05-10 13:53:14 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
# prob = true_exp / false_exp_sum
|
|
|
|
# print("prob", prob)
|
2019-05-10 13:53:14 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
entity_mses = sorted(entity_mses)
|
|
|
|
# mse_sum = sum(entity_mses)
|
|
|
|
# entity_probs = [1 - x/mse_sum for x in entity_mses]
|
|
|
|
# print("entity_mses", entity_mses)
|
|
|
|
# print("entity_probs", entity_probs)
|
|
|
|
true_index = entity_mses.index(true_mse)
|
|
|
|
# print("true index", true_index)
|
|
|
|
# print("true prob", entity_probs[true_index])
|
2019-05-07 17:03:42 +03:00
|
|
|
|
2019-05-13 18:02:34 +03:00
|
|
|
# print("training loss", true_mse)
|
|
|
|
|
|
|
|
# print()
|
2019-05-07 17:03:42 +03:00
|
|
|
|
2019-05-09 18:23:19 +03:00
|
|
|
# TODO: proper backpropagation taking ranking of elements into account ?
|
|
|
|
# TODO backpropagation also for negative examples
|
2019-05-13 18:02:34 +03:00
|
|
|
|
|
|
|
if doc_diffs is not None:
|
|
|
|
doc_diffs = doc_diffs / len(true_entity_list)
|
|
|
|
|
|
|
|
true_entity_bp(entity_diffs, sgd=self.sgd_entity)
|
|
|
|
article_bp(doc_diffs, sgd=self.sgd_article)
|
2019-05-09 18:23:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
# TODO delete ?
|
|
|
|
def _simple_cnn_model(self, internal_dim):
|
|
|
|
nr_class = len(self.labels)
|
|
|
|
with Model.define_operators({">>": chain}):
|
|
|
|
model_entity = SpacyVectors >> flatten_add_lengths >> Pooling(mean_pool) # entity encoding
|
|
|
|
model_doc = SpacyVectors >> flatten_add_lengths >> Pooling(mean_pool) # doc encoding
|
|
|
|
output_layer = Softmax(nr_class, internal_dim*2)
|
|
|
|
model = (model_entity | model_doc) >> output_layer
|
|
|
|
# model.tok2vec = chain(tok2vec, flatten)
|
|
|
|
model.nO = nr_class
|
|
|
|
return model
|
|
|
|
|
|
|
|
def predict(self, entity_doc, article_doc):
|
|
|
|
entity_encoding = self.entity_encoder(entity_doc)
|
|
|
|
doc_encoding = self.article_encoder(article_doc)
|
|
|
|
|
|
|
|
print("entity_encodings", len(entity_encoding), entity_encoding)
|
|
|
|
print("doc_encodings", len(doc_encoding), doc_encoding)
|
|
|
|
mse, diffs = self._calculate_similarity(entity_encoding, doc_encoding)
|
|
|
|
print("mse", mse)
|
|
|
|
|
|
|
|
return mse
|
|
|
|
|
2019-05-10 13:53:14 +03:00
|
|
|
# TODO: expand to more than 2 vectors
|
|
|
|
def _calculate_consensus(self, vector1, vector2):
|
|
|
|
if len(vector1) != len(vector2):
|
|
|
|
raise ValueError("To calculate consenus, both vectors should be of equal length")
|
|
|
|
|
|
|
|
avg = (vector2 + vector1) / 2
|
|
|
|
return avg
|
|
|
|
|
2019-05-09 18:23:19 +03:00
|
|
|
def _calculate_similarity(self, vector1, vector2):
|
|
|
|
if len(vector1) != len(vector2):
|
|
|
|
raise ValueError("To calculate similarity, both vectors should be of equal length")
|
|
|
|
|
2019-05-10 13:53:14 +03:00
|
|
|
diffs = (vector1 - vector2)
|
2019-05-09 19:11:49 +03:00
|
|
|
error_sum = (diffs ** 2).sum()
|
2019-05-09 18:23:19 +03:00
|
|
|
mean_square_error = error_sum / len(vector1)
|
|
|
|
return float(mean_square_error), diffs
|
|
|
|
|
|
|
|
def _get_labels(self):
|
|
|
|
return tuple(self.labels)
|
|
|
|
|
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 15:26:04 +03:00
|
|
|
|
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
|