Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building a training loop - update example from spacy101 v2.x tutorial #123

Open
avoutsas67 opened this issue Feb 21, 2022 · 0 comments
Open

Comments

@avoutsas67
Copy link

avoutsas67 commented Feb 21, 2022

Hello, can you please indicate how to change the code below to align with Spacy's current v3.2.x?

Thanks for your support,

Achilleas

import spacy
import random
import json
   
with open("exercises/en/gadgets.json") as f:
    TRAINING_DATA = json.loads(f.read())

nlp = spacy.blank("en")
ner = nlp.add_pipe('ner')
ner.add_label("GADGET")

nlp.vocab.vectors.name = 'example'

# Start the training
nlp.begin_training()

# Loop for 10 iterations
for itn in range(10):
    # Shuffle the training data
    random.shuffle(TRAINING_DATA)
    losses = {}

    for batch in spacy.util.minibatch(TRAINING_DATA, size=2):
        # Batch the examples and iterate over them
        texts = [text for text, annotation in batch]
        annotations = [annotation for text, annotation in batch]
        # Update the model
        nlp.update(texts, annotations)
        print("{0:.10f}".format(losses['ner']) )
@avoutsas67 avoutsas67 changed the title Building a training loop example from spacy101 v2.x tutorial Building a training loop - update example from spacy101 v2.x tutorial Feb 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant