Skip to content

Instantly share code, notes, and snippets.

@fsndzomga
Created September 27, 2023 23:09
Show Gist options
  • Save fsndzomga/6bc910c1e7bf48b5aeb6d28c6b98cd3c to your computer and use it in GitHub Desktop.
Save fsndzomga/6bc910c1e7bf48b5aeb6d28c6b98cd3c to your computer and use it in GitHub Desktop.
import random
import spacy
from spacy.training.example import Example
from spacy.util import minibatch
# Initialize or load an NLP object and get the NER pipeline
nlp = spacy.blank("en")
nlp.add_pipe("ner")
# Initialize the optimizer
optimizer = nlp.begin_training()
# Train the model
for i in range(100): # Number of epochs
random.shuffle(TRAIN_DATA)
losses = {}
batches = minibatch(TRAIN_DATA, size=8)
for batch in batches:
texts, annotations = zip(*batch)
example = []
for i in range(len(texts)):
doc = nlp.make_doc(texts[i])
example.append(Example.from_dict(doc, annotations[i]))
nlp.update(
example,
drop=0.5, # Dropout rate
losses=losses,
)
print(losses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment