2019-06-07 13:58:42 +03:00
|
|
|
# coding: utf-8
|
2019-06-05 01:09:46 +03:00
|
|
|
from random import shuffle
|
|
|
|
|
2019-09-13 18:03:57 +03:00
|
|
|
import logging
|
2019-06-05 01:09:46 +03:00
|
|
|
import numpy as np
|
|
|
|
|
2020-01-29 19:06:46 +03:00
|
|
|
from thinc.model import Model
|
2019-06-05 01:09:46 +03:00
|
|
|
from thinc.api import chain
|
2020-01-29 19:06:46 +03:00
|
|
|
from thinc.loss import CosineDistance
|
|
|
|
from thinc.layers import Linear
|
|
|
|
|
|
|
|
from spacy.util import create_default_optimizer
|
2019-06-05 01:09:46 +03:00
|
|
|
|
2019-09-13 18:03:57 +03:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-06-05 01:09:46 +03:00
|
|
|
|
|
|
|
class EntityEncoder:
|
2019-06-18 19:38:09 +03:00
|
|
|
"""
|
|
|
|
Train the embeddings of entity descriptions to fit a fixed-size entity vector (e.g. 64D).
|
2019-06-28 09:29:31 +03:00
|
|
|
This entity vector will be stored in the KB, for further downstream use in the entity model.
|
2019-06-18 19:38:09 +03:00
|
|
|
"""
|
2019-06-05 01:09:46 +03:00
|
|
|
|
|
|
|
DROP = 0
|
|
|
|
BATCH_SIZE = 1000
|
|
|
|
|
2019-08-13 16:38:59 +03:00
|
|
|
# Set min. acceptable loss to avoid a 'mean of empty slice' warning by numpy
|
|
|
|
MIN_LOSS = 0.01
|
|
|
|
|
|
|
|
# Reasonable default to stop training when things are not improving
|
|
|
|
MAX_NO_IMPROVEMENT = 20
|
|
|
|
|
|
|
|
def __init__(self, nlp, input_dim, desc_width, epochs=5):
|
2019-06-05 01:09:46 +03:00
|
|
|
self.nlp = nlp
|
2019-06-06 20:51:27 +03:00
|
|
|
self.input_dim = input_dim
|
|
|
|
self.desc_width = desc_width
|
2019-08-13 16:38:59 +03:00
|
|
|
self.epochs = epochs
|
2020-01-29 19:06:46 +03:00
|
|
|
self.distance = CosineDistance(ignore_zeros=True, normalize=False)
|
2019-06-06 20:51:27 +03:00
|
|
|
|
|
|
|
def apply_encoder(self, description_list):
|
|
|
|
if self.encoder is None:
|
|
|
|
raise ValueError("Can not apply encoder before training it")
|
|
|
|
|
2019-06-10 22:25:26 +03:00
|
|
|
batch_size = 100000
|
2019-06-05 01:09:46 +03:00
|
|
|
|
2019-06-06 20:51:27 +03:00
|
|
|
start = 0
|
|
|
|
stop = min(batch_size, len(description_list))
|
|
|
|
encodings = []
|
2019-06-05 01:09:46 +03:00
|
|
|
|
2019-06-06 20:51:27 +03:00
|
|
|
while start < len(description_list):
|
|
|
|
docs = list(self.nlp.pipe(description_list[start:stop]))
|
|
|
|
doc_embeddings = [self._get_doc_embedding(doc) for doc in docs]
|
|
|
|
enc = self.encoder(np.asarray(doc_embeddings))
|
|
|
|
encodings.extend(enc.tolist())
|
2019-06-05 01:09:46 +03:00
|
|
|
|
2019-06-06 20:51:27 +03:00
|
|
|
start = start + batch_size
|
|
|
|
stop = min(stop + batch_size, len(description_list))
|
2019-10-14 13:28:53 +03:00
|
|
|
logger.info("Encoded: {} entities".format(stop))
|
2019-06-05 01:09:46 +03:00
|
|
|
|
2019-06-06 20:51:27 +03:00
|
|
|
return encodings
|
2019-06-05 19:29:18 +03:00
|
|
|
|
2019-06-06 20:51:27 +03:00
|
|
|
def train(self, description_list, to_print=False):
|
|
|
|
processed, loss = self._train_model(description_list)
|
|
|
|
if to_print:
|
2019-09-13 18:03:57 +03:00
|
|
|
logger.info(
|
|
|
|
"Trained entity descriptions on {} ".format(processed) +
|
2019-10-14 13:28:53 +03:00
|
|
|
"(non-unique) descriptions across {} ".format(self.epochs) +
|
2019-09-13 18:03:57 +03:00
|
|
|
"epochs"
|
2019-08-13 16:38:59 +03:00
|
|
|
)
|
2019-09-13 18:03:57 +03:00
|
|
|
logger.info("Final loss: {}".format(loss))
|
2019-06-06 20:51:27 +03:00
|
|
|
|
|
|
|
def _train_model(self, description_list):
|
2019-08-13 16:38:59 +03:00
|
|
|
best_loss = 1.0
|
|
|
|
iter_since_best = 0
|
2019-06-06 20:51:27 +03:00
|
|
|
self._build_network(self.input_dim, self.desc_width)
|
2019-06-05 01:09:46 +03:00
|
|
|
|
|
|
|
processed = 0
|
|
|
|
loss = 1
|
2019-08-13 16:38:59 +03:00
|
|
|
# copy this list so that shuffling does not affect other functions
|
|
|
|
descriptions = description_list.copy()
|
|
|
|
to_continue = True
|
2019-06-05 01:09:46 +03:00
|
|
|
|
2019-08-13 16:38:59 +03:00
|
|
|
for i in range(self.epochs):
|
2019-06-06 20:51:27 +03:00
|
|
|
shuffle(descriptions)
|
2019-06-05 01:09:46 +03:00
|
|
|
|
|
|
|
batch_nr = 0
|
|
|
|
start = 0
|
2019-06-06 20:51:27 +03:00
|
|
|
stop = min(self.BATCH_SIZE, len(descriptions))
|
2019-06-05 01:09:46 +03:00
|
|
|
|
2019-08-13 16:38:59 +03:00
|
|
|
while to_continue and start < len(descriptions):
|
2019-06-05 01:09:46 +03:00
|
|
|
batch = []
|
2019-06-06 20:51:27 +03:00
|
|
|
for descr in descriptions[start:stop]:
|
2019-06-05 01:09:46 +03:00
|
|
|
doc = self.nlp(descr)
|
|
|
|
doc_vector = self._get_doc_embedding(doc)
|
|
|
|
batch.append(doc_vector)
|
|
|
|
|
2019-06-06 20:51:27 +03:00
|
|
|
loss = self._update(batch)
|
2019-08-13 16:38:59 +03:00
|
|
|
if batch_nr % 25 == 0:
|
2019-09-13 18:03:57 +03:00
|
|
|
logger.info("loss: {} ".format(loss))
|
2019-06-05 01:09:46 +03:00
|
|
|
processed += len(batch)
|
|
|
|
|
2019-08-13 16:38:59 +03:00
|
|
|
# in general, continue training if we haven't reached our ideal min yet
|
|
|
|
to_continue = loss > self.MIN_LOSS
|
|
|
|
|
|
|
|
# store the best loss and track how long it's been
|
|
|
|
if loss < best_loss:
|
|
|
|
best_loss = loss
|
|
|
|
iter_since_best = 0
|
|
|
|
else:
|
|
|
|
iter_since_best += 1
|
|
|
|
|
|
|
|
# stop learning if we haven't seen improvement since the last few iterations
|
|
|
|
if iter_since_best > self.MAX_NO_IMPROVEMENT:
|
|
|
|
to_continue = False
|
|
|
|
|
2019-06-05 01:09:46 +03:00
|
|
|
batch_nr += 1
|
|
|
|
start = start + self.BATCH_SIZE
|
2019-06-06 20:51:27 +03:00
|
|
|
stop = min(stop + self.BATCH_SIZE, len(descriptions))
|
2019-06-05 01:09:46 +03:00
|
|
|
|
|
|
|
return processed, loss
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _get_doc_embedding(doc):
|
|
|
|
indices = np.zeros((len(doc),), dtype="i")
|
|
|
|
for i, word in enumerate(doc):
|
|
|
|
if word.orth in doc.vocab.vectors.key2row:
|
|
|
|
indices[i] = doc.vocab.vectors.key2row[word.orth]
|
|
|
|
else:
|
|
|
|
indices[i] = 0
|
|
|
|
word_vectors = doc.vocab.vectors.data[indices]
|
2019-06-28 09:29:31 +03:00
|
|
|
doc_vector = np.mean(word_vectors, axis=0)
|
2019-06-05 01:09:46 +03:00
|
|
|
return doc_vector
|
|
|
|
|
|
|
|
def _build_network(self, orig_width, hidden_with):
|
|
|
|
with Model.define_operators({">>": chain}):
|
2019-06-18 19:38:09 +03:00
|
|
|
# very simple encoder-decoder model
|
2020-01-29 19:06:46 +03:00
|
|
|
self.encoder = Linear(hidden_with, orig_width)
|
|
|
|
# TODO: removed the zero_init here - is oK?
|
|
|
|
self.model = self.encoder >> Linear(orig_width, hidden_with)
|
|
|
|
self.sgd = create_default_optimizer()
|
2019-06-05 01:09:46 +03:00
|
|
|
|
2019-06-06 20:51:27 +03:00
|
|
|
def _update(self, vectors):
|
2020-01-29 19:06:46 +03:00
|
|
|
truths = self.model.ops.asarray(vectors)
|
2019-08-13 16:38:59 +03:00
|
|
|
predictions, bp_model = self.model.begin_update(
|
2020-01-29 19:06:46 +03:00
|
|
|
truths, drop=self.DROP
|
2019-08-13 16:38:59 +03:00
|
|
|
)
|
2020-01-29 19:06:46 +03:00
|
|
|
d_scores, loss = self.distance(predictions, truths)
|
2019-06-05 01:09:46 +03:00
|
|
|
bp_model(d_scores, sgd=self.sgd)
|
|
|
|
return loss / len(vectors)
|
|
|
|
|