code/Chapter_7.py
def main(model=None, output_dir=None, n_iter=5):
    """Load the model, set up the pipeline and train the parser."""
    if model is not None:
        nlp = spacy.load(model)  # load existing spaCy model
        print("Loaded model '%s'" % model)
    else:
        nlp = spacy.blank('en')  # create blank Language class
        print("Created blank 'en' model")

    # We'll use the built-in dependency parser class, but we want to create a
    # fresh instance – just in case.
    if 'parser' in nlp.pipe_names:
        nlp.remove_pipe('parser')
    parser = nlp.create_pipe('parser')
    nlp.add_pipe(parser, first=True)

    for text, annotations in TRAIN_DATA:
        for dep in annotations.get('deps', []):
            parser.add_label(dep)

    other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'parser']
    with nlp.disable_pipes(*other_pipes):  # only train parser
        optimizer = nlp.begin_training()
        for itn in range(n_iter):
            random.shuffle(TRAIN_DATA)
            losses = {}
            for text, annotations in TRAIN_DATA:
                nlp.update([text], [annotations], sgd=optimizer, losses=losses)
            print(losses)

    # test the trained model
    test_model(nlp)

    # save model to output directory
    if output_dir is not None:
        output_dir = Path(output_dir)
        if not output_dir.exists():
            output_dir.mkdir()
        nlp.to_disk(output_dir)
        print("Saved model to", output_dir)

        # test the saved model
        print("Loading from", output_dir)
        nlp2 = spacy.load(output_dir)
        test_model(nlp2)
Load the model, set up the pipeline and train the parser.
Load the model, set up the pipeline and train the parser.
Load the model set up the pipeline and train the parser .
def main(model=None, output_dir=None, n_iter=5):
    if model is not None:
        nlp = spacy.load(model)  
        print("Loaded model '%s'" % model)
    else:
        nlp = spacy.blank('en')  
        print("Created blank 'en' model")
    if 'parser' in nlp.pipe_names:
        nlp.remove_pipe('parser')
    parser = nlp.create_pipe('parser')
    nlp.add_pipe(parser, first=True)
    for text, annotations in TRAIN_DATA:
        for dep in annotations.get('deps', []):
            parser.add_label(dep)
    other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'parser']
    with nlp.disable_pipes(*other_pipes):  
        optimizer = nlp.begin_training()
        for itn in range(n_iter):
            random.shuffle(TRAIN_DATA)
            losses = {}
            for text, annotations in TRAIN_DATA:
                nlp.update([text], [annotations], sgd=optimizer, losses=losses)
            print(losses)
    test_model(nlp)
    if output_dir is not None:
        output_dir = Path(output_dir)
        if not output_dir.exists():
            output_dir.mkdir()
        nlp.to_disk(output_dir)
        print("Saved model to", output_dir)
        print("Loading from", output_dir)
        nlp2 = spacy.load(output_dir)
        test_model(nlp2)
def main ( model = None , output_dir = None , n_iter = 5 ) : if model is not None : nlp = spacy . load ( model ) print ( "Loaded model '%s'" % model ) else : nlp = spacy . blank ( 'en' ) print ( "Created blank 'en' model" ) if 'parser' in nlp . pipe_names : nlp . remove_pipe ( 'parser' ) parser = nlp . create_pipe ( 'parser' ) nlp . add_pipe ( parser , first = True ) for text , annotations in TRAIN_DATA : for dep in annotations . get ( 'deps' , [ ] ) : parser . add_label ( dep ) other_pipes = [ pipe for pipe in nlp . pipe_names if pipe != 'parser' ] with nlp . disable_pipes ( * other_pipes ) : optimizer = nlp . begin_training ( ) for itn in range ( n_iter ) : random . shuffle ( TRAIN_DATA ) losses = { } for text , annotations in TRAIN_DATA : nlp . update ( [ text ] , [ annotations ] , sgd = optimizer , losses = losses ) print ( losses ) test_model ( nlp ) if output_dir is not None : output_dir = Path ( output_dir ) if not output_dir . exists ( ) : output_dir . mkdir ( ) nlp . to_disk ( output_dir ) print ( "Saved model to" , output_dir ) print ( "Loading from" , output_dir ) nlp2 = spacy . load ( output_dir ) test_model ( nlp2 )
Load the model, set up the pipeline and train the parser.
Load the model set up the pipeline and train the parser .
"""Load the model, set up the pipeline and train the parser.""" # load existing spaCy model # create blank Language class # We'll use the built-in dependency parser class, but we want to create a # fresh instance – just in case. # only train parser # test the trained model # save model to output directory # test the saved model
model
output_dir
n_iter