<jupyter_start><jupyter_text>Chapter 4: Training a neural network modelIn this lesson, you'll learn about training and updating spaCy's neural network models and the data you need for it. How training works1. Initialize the model weights randomly with nlp.begin_training    2. Predict a few examples with the current weights by calling nlp.update3. Compare prediction with true labels4. Calculate how to change weights to improve predictions  5. Update weights slightly6. Go back to 2.<jupyter_code># Dowloan the example file from Github
!mkdir -p exercises/en/
!curl https://raw.githubusercontent.com/ines/spacy-course/master/exercises/en/gadgets.json -o exercises/en/gadgets.json
# install spacy
!pip install spacy
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.create_pipe("ner")
nlp.add_pipe(ner)
ner.add_label("GADGET")

# Start the training
nlp.begin_training()

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

    # Batch the examples and iterate over them
    for batch in spacy.util.minibatch(TRAINING_DATA, size=2):
        texts = [text for text, entities in batch]
        annotations = [entities for text, entities in batch]

        # Update the model
        nlp.update(texts, annotations, losses=losses)
    print(losses)<jupyter_output>{'ner': 33.292569041252136}
{'ner': 20.41395816206932}
{'ner': 11.164878673385829}
{'ner': 6.79573207370413}
{'ner': 8.442645727438503}
{'ner': 6.194359917637485}
{'ner': 2.9376985493918255}
{'ner': 0.7380687986748882}
{'ner': 0.04670791909826022}
{'ner': 0.0027360357821990533}
{'fraction_non_alphanumeric': 0.10538506079907353, 'fraction_numerical': 0.10538506079907353, 'mean_word_length': 4.556270096463023, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '555811', 'license': 'MIT', 'n_tokens_mistral': 702, 'n_tokens_neox': 578, 'n_words': 175, 'path': 'SpacyLecture4_NER.ipynb', 'repo_name': 'timgluz/colab_notebooks', 'size': '1727'}