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-14 23:55:56 +03:00
|
|
|
import numpy as np
|
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
|
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-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():
|
|
|
|
|
2019-05-14 23:55:56 +03:00
|
|
|
INPUT_DIM = 300
|
|
|
|
OUTPUT_DIM = 5 # 96
|
|
|
|
PRINT_LOSS = True
|
|
|
|
PRINT_F = True
|
|
|
|
|
2019-05-09 18:23:19 +03:00
|
|
|
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-14 23:55:56 +03:00
|
|
|
self.entity_encoder = self._simple_encoder(in_width=self.INPUT_DIM, out_width=self.OUTPUT_DIM)
|
|
|
|
self.article_encoder = self._simple_encoder(in_width=self.INPUT_DIM, out_width=self.OUTPUT_DIM)
|
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():
|
2019-05-14 23:55:56 +03:00
|
|
|
print("article", article_id)
|
2019-05-13 18:02:34 +03:00
|
|
|
article_doc = train_doc[article_id]
|
|
|
|
pos_ex_list = list()
|
|
|
|
neg_exs_list = list()
|
|
|
|
for inst_cluster in inst_cluster_set:
|
2019-05-14 23:55:56 +03:00
|
|
|
print("inst_cluster", inst_cluster)
|
2019-05-13 18:02:34 +03:00
|
|
|
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)
|
2019-05-14 23:55:56 +03:00
|
|
|
if self.PRINT_F:
|
|
|
|
print(round(fscore, 1))
|
2019-05-13 18:02:34 +03:00
|
|
|
|
|
|
|
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-14 23:55:56 +03:00
|
|
|
best_entity, highest_prob = self._predict(examples, article_doc)
|
2019-05-13 18:02:34 +03:00
|
|
|
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])
|
|
|
|
|
2019-05-14 23:55:56 +03:00
|
|
|
highest_prob = None
|
2019-05-13 15:26:04 +03:00
|
|
|
best_entity = None
|
|
|
|
|
2019-05-14 23:55:56 +03:00
|
|
|
entity_to_vector = dict()
|
|
|
|
for entity in entities:
|
|
|
|
entity_to_vector[entity] = self.entity_encoder([entity])
|
|
|
|
|
2019-05-13 15:26:04 +03:00
|
|
|
for entity in entities:
|
2019-05-14 23:55:56 +03:00
|
|
|
entity_encoding = entity_to_vector[entity]
|
|
|
|
prob = self._calculate_probability(doc_encoding, entity_encoding, entity_to_vector.values())
|
|
|
|
if not best_entity or prob > highest_prob:
|
|
|
|
highest_prob = prob
|
2019-05-13 15:26:04 +03:00
|
|
|
best_entity = entity
|
|
|
|
|
2019-05-14 23:55:56 +03:00
|
|
|
return best_entity, highest_prob
|
2019-05-13 15:26:04 +03:00
|
|
|
|
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):
|
|
|
|
|
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):
|
2019-05-14 23:55:56 +03:00
|
|
|
for cnt in range(10):
|
|
|
|
#try:
|
|
|
|
false_vectors = list()
|
|
|
|
false_entities = false_entities_list[i]
|
|
|
|
if len(false_entities) > 0:
|
|
|
|
# TODO: batch per doc
|
|
|
|
doc_encoding, article_bp = self.article_encoder.begin_update([article_doc], drop=drop)
|
|
|
|
doc_encoding = doc_encoding[0]
|
|
|
|
print()
|
|
|
|
print(cnt)
|
|
|
|
print("doc", doc_encoding)
|
|
|
|
|
|
|
|
for false_entity in false_entities:
|
|
|
|
# TODO: one call only to begin_update ?
|
|
|
|
false_entity_encoding, false_entity_bp = self.entity_encoder.begin_update([false_entity], drop=drop)
|
|
|
|
false_entity_encoding = false_entity_encoding[0]
|
|
|
|
false_vectors.append(false_entity_encoding)
|
|
|
|
|
|
|
|
true_entity_encoding, true_entity_bp = self.entity_encoder.begin_update([true_entity], drop=drop)
|
|
|
|
true_entity_encoding = true_entity_encoding[0]
|
|
|
|
|
|
|
|
all_vectors = [true_entity_encoding]
|
|
|
|
all_vectors.extend(false_vectors)
|
|
|
|
|
|
|
|
# consensus_encoding = self._calculate_consensus(doc_encoding, true_entity_encoding)
|
|
|
|
|
|
|
|
true_prob = self._calculate_probability(doc_encoding, true_entity_encoding, all_vectors)
|
|
|
|
print("true", true_prob, true_entity_encoding)
|
|
|
|
|
|
|
|
all_probs = [true_prob]
|
|
|
|
for false_vector in false_vectors:
|
|
|
|
false_prob = self._calculate_probability(doc_encoding, false_vector, all_vectors)
|
|
|
|
print("false", false_prob, false_vector)
|
|
|
|
all_probs.append(false_prob)
|
|
|
|
|
|
|
|
loss = self._calculate_loss(true_prob, all_probs).astype(np.float32)
|
|
|
|
if self.PRINT_LOSS:
|
|
|
|
print("loss", round(loss, 5))
|
|
|
|
|
|
|
|
doc_gradient = self._calculate_doc_gradient(loss, doc_encoding, true_entity_encoding, false_vectors)
|
|
|
|
print("doc_gradient", doc_gradient)
|
|
|
|
article_bp([doc_gradient.astype(np.float32)], sgd=self.sgd_article)
|
|
|
|
#except Exception as e:
|
|
|
|
#pass
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: FIX
|
2019-05-10 13:53:14 +03:00
|
|
|
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-14 23:55:56 +03:00
|
|
|
def _calculate_probability(self, vector1, vector2, allvectors):
|
|
|
|
""" Make sure that vector2 is included in allvectors """
|
2019-05-09 18:23:19 +03:00
|
|
|
if len(vector1) != len(vector2):
|
|
|
|
raise ValueError("To calculate similarity, both vectors should be of equal length")
|
|
|
|
|
2019-05-14 23:55:56 +03:00
|
|
|
vector1_t = vector1.transpose()
|
|
|
|
e = self._calculate_dot_exp(vector2, vector1_t)
|
|
|
|
e_sum = 0
|
|
|
|
for v in allvectors:
|
|
|
|
e_sum += self._calculate_dot_exp(v, vector1_t)
|
|
|
|
|
|
|
|
return float(e / e_sum)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _calculate_loss(true_prob, all_probs):
|
|
|
|
""" all_probs should include true_prob ! """
|
|
|
|
return -1 * np.log(true_prob / sum(all_probs))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _calculate_doc_gradient(loss, doc_vector, true_vector, false_vectors):
|
|
|
|
gradient = np.zeros(len(doc_vector))
|
|
|
|
for i in range(len(doc_vector)):
|
|
|
|
min_false = min(x[i] for x in false_vectors)
|
|
|
|
max_false = max(x[i] for x in false_vectors)
|
|
|
|
|
|
|
|
if true_vector[i] > max_false:
|
|
|
|
if doc_vector[i] > 0:
|
|
|
|
gradient[i] = 0
|
|
|
|
else:
|
|
|
|
gradient[i] = -loss
|
|
|
|
elif true_vector[i] < min_false:
|
|
|
|
if doc_vector[i] > 0:
|
|
|
|
gradient[i] = loss
|
|
|
|
if doc_vector[i] < 0:
|
|
|
|
gradient[i] = 0
|
|
|
|
else:
|
|
|
|
target = 0 # non-distinctive vector positions should convert to 0
|
|
|
|
gradient[i] = doc_vector[i] - target
|
|
|
|
|
|
|
|
return gradient
|
2019-05-09 18:23:19 +03:00
|
|
|
|
2019-05-14 23:55:56 +03:00
|
|
|
@staticmethod
|
|
|
|
def _calculate_dot_exp(vector1, vector2_transposed):
|
|
|
|
e = np.exp(vector1.dot(vector2_transposed))
|
|
|
|
return e
|
2019-05-09 18:23:19 +03:00
|
|
|
|
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
|