Created
September 27, 2023 23:09
-
-
Save fsndzomga/6bc910c1e7bf48b5aeb6d28c6b98cd3c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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