-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust code to train a spaCy model to spaCy version 3.0
the old code was not running anymore for the new version. There must be a problem with the new code as well however, since the training loss is always 0. I was unable to figure out why until now. I followed the instructions in the official spaCy entity linking tutorial here: https://github.com/explosion/projects/blob/v3/tutorials/nel_emerson/notebooks/notebook_video.ipynb Relevant to #14
- Loading branch information
Showing
8 changed files
with
72 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,66 @@ | ||
import argparse | ||
import log | ||
import sys | ||
import spacy | ||
from spacy.kb import KnowledgeBase | ||
from spacy.language import Language | ||
|
||
from src import settings | ||
from src.helpers.entity_database_reader import EntityDatabaseReader | ||
from src.helpers.label_generator import LabelGenerator | ||
|
||
|
||
def save_model(model: Language, model_name: str): | ||
path = settings.SPACY_MODEL_DIRECTORY + model_name | ||
model_bytes = model.to_bytes() | ||
with open(path, "wb") as f: | ||
f.write(model_bytes) | ||
logger.info("Saved model to %s" % path) | ||
|
||
|
||
PRINT_EVERY = 1 | ||
SAVE_EVERY = 10000 | ||
|
||
|
||
def main(args): | ||
# make pipeline: | ||
if args.kb_name == "0": | ||
vocab_path = settings.VOCAB_DIRECTORY | ||
kb_path = settings.KB_FILE | ||
else: | ||
load_path = settings.KB_DIRECTORY + args.kb_name + "/" | ||
vocab_path = load_path + "vocab" | ||
kb_path = load_path + "kb" | ||
def train(): | ||
load_path = settings.KB_DIRECTORY + "wikipedia/" | ||
vocab_path = load_path + "vocab" | ||
kb_path = load_path | ||
|
||
logger.info("Loading model ...") | ||
nlp = spacy.load(settings.LARGE_MODEL_NAME) | ||
nlp.vocab.from_disk(vocab_path) | ||
# kb = KnowledgeBase(vocab=nlp.vocab, entity_vector_length=300) | ||
# kb.from_disk(kb_path) | ||
# entity_linker = nlp.create_pipe("entity_linker", {"incl_prior": args.prior}) | ||
|
||
def create_kb(vocab): | ||
kb = KnowledgeBase(vocab=vocab, entity_vector_length=300) | ||
kb.from_disk(kb_path) | ||
logger.info("Knowledge base contains %d entities." % kb.get_size_entities()) | ||
logger.info("Knowledge base contains %d aliases." % kb.get_size_aliases()) | ||
return kb | ||
|
||
# create entity linker with the knowledge base and add it to the pipeline: | ||
entity_linker = nlp.add_pipe("entity_linker", config={"incl_prior": False}, last=True) | ||
logger.info("Loading knowledge base ...") | ||
entity_linker = nlp.create_pipe("entity_linker", {"incl_prior": args.prior}) | ||
kb = KnowledgeBase(vocab=nlp.vocab) | ||
kb.load_bulk(kb_path) | ||
logger.info("Knowledge base contains %d entities." % kb.get_size_entities()) | ||
logger.info("Knowledge base contains %d aliases." % kb.get_size_aliases()) | ||
entity_linker.set_kb(kb) | ||
nlp.add_pipe(entity_linker, last=True) | ||
|
||
pipe_exceptions = ["entity_linker", "trf_wordpiecer", "trf_tok2vec"] | ||
other_pipes = [pipe for pipe in nlp.pipe_names if pipe not in pipe_exceptions] | ||
|
||
# initialize model: | ||
optimizer = nlp.begin_training() | ||
|
||
# initialize label generator: | ||
entity_linker.set_kb(create_kb) | ||
kb = nlp.get_pipe("entity_linker").kb | ||
mapping = EntityDatabaseReader.get_wikipedia_to_wikidata_db() | ||
generator = LabelGenerator(nlp, kb, mapping) | ||
|
||
# iterate over training examples (batch size 1): | ||
logger.info("Training ...") | ||
n_batches = 0 | ||
n_articles = 0 | ||
n_entities = 0 | ||
loss_sum = 0 | ||
if args.n_batches != 0: | ||
for doc, labels in generator.read_examples(): | ||
batch_docs = [doc] | ||
batch_labels = [labels] | ||
generator = LabelGenerator(kb, mapping) | ||
example = next(generator.read_examples()) | ||
entity_linker.initialize(get_examples=lambda: [example]) | ||
print(f"hyperparameters: {entity_linker.cfg}") | ||
|
||
from spacy.util import minibatch, compounding | ||
|
||
with nlp.select_pipes(enable=["entity_linker"]): # train only the entity_linker | ||
optimizer = nlp.resume_training() | ||
print(nlp.get_pipe("entity_linker").cfg) | ||
for itn in range(1): | ||
batches = minibatch(generator.read_examples(n=100), size=1) | ||
losses = {} | ||
with nlp.disable_pipes(*other_pipes): | ||
for batch in batches: | ||
nlp.update( | ||
batch_docs, | ||
batch_labels, | ||
batch, | ||
drop=0.2, # prevent overfitting | ||
losses=losses, | ||
sgd=optimizer, | ||
losses=losses | ||
) | ||
n_batches += 1 | ||
n_articles += len(batch_docs) | ||
n_entities += len(labels["links"]) | ||
loss = losses["entity_linker"] | ||
loss_sum += loss | ||
if n_batches % PRINT_EVERY == 0: | ||
loss_mean = loss_sum / n_batches | ||
print("\r%i batches\t%i articles\t%i entities\tloss: %f\tmean: %f" % | ||
(n_batches, n_articles, n_entities, loss, loss_mean), end='') | ||
if n_batches == args.n_batches: | ||
break | ||
elif n_batches % SAVE_EVERY == 0: | ||
print() | ||
save_model(nlp, args.name) | ||
print() | ||
save_model(nlp, args.name) | ||
if itn % 10 == 0: | ||
print(itn, "Losses", losses) # print the training loss | ||
print(itn, "Losses", losses) | ||
entity_linker.to_disk(settings.SPACY_MODEL_DIRECTORY + "spacy_batch1_model") | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, | ||
description=__doc__) | ||
|
||
parser.add_argument("name", type=str, | ||
help="Linker name.") | ||
parser.add_argument("n_batches", type=int, | ||
help="Number of batches.") | ||
parser.add_argument("kb_name", type=str, | ||
help="KB name.") | ||
parser.add_argument("-p", "--prior", type=str, action="store_true", | ||
help="Use prior probabilities.") | ||
|
||
logger = log.setup_logger(sys.argv[0]) | ||
logger.debug(' '.join(sys.argv)) | ||
|
||
main(parser.parse_args()) | ||
train() |