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
|
|
|
|
|
|
|
|
from spacy._ml import zero_init, create_default_optimizer
|
|
|
|
from spacy.cli.pretrain import get_cossim_loss
|
|
|
|
|
|
|
|
from thinc.v2v import Model
|
|
|
|
from thinc.api import chain
|
|
|
|
from thinc.neural._classes.affine import Affine
|
|
|
|
|
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
|
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-09-13 18:03:57 +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) +
|
|
|
|
"(non-unique) entities across {} ".format(self.epochs) +
|
|
|
|
"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
|
2019-08-13 16:38:59 +03:00
|
|
|
self.encoder = Affine(hidden_with, orig_width)
|
|
|
|
self.model = self.encoder >> zero_init(
|
|
|
|
Affine(orig_width, hidden_with, drop_factor=0.0)
|
2019-06-05 01:09:46 +03:00
|
|
|
)
|
|
|
|
self.sgd = create_default_optimizer(self.model.ops)
|
|
|
|
|
2019-06-06 20:51:27 +03:00
|
|
|
def _update(self, vectors):
|
2019-08-13 16:38:59 +03:00
|
|
|
predictions, bp_model = self.model.begin_update(
|
|
|
|
np.asarray(vectors), drop=self.DROP
|
|
|
|
)
|
2019-06-06 20:51:27 +03:00
|
|
|
loss, d_scores = self._get_loss(scores=predictions, golds=np.asarray(vectors))
|
2019-06-05 01:09:46 +03:00
|
|
|
bp_model(d_scores, sgd=self.sgd)
|
|
|
|
return loss / len(vectors)
|
|
|
|
|
|
|
|
@staticmethod
|
2019-06-06 20:51:27 +03:00
|
|
|
def _get_loss(golds, scores):
|
2019-06-05 01:09:46 +03:00
|
|
|
loss, gradients = get_cossim_loss(scores, golds)
|
|
|
|
return loss, gradients
|