@plac.annotations(<EOL>lang=("<STR_LIT>", "<STR_LIT>", None, str),<EOL>output_path=("<STR_LIT>", "<STR_LIT>", None, Path),<EOL>train_path=("<STR_LIT>", "<STR_LIT>", None, Path),<EOL>dev_path=("<STR_LIT>", "<STR_LIT>", None, Path),<EOL>raw_text=(<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>Path,<EOL>),<EOL>base_model=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:b>", str),<EOL>pipeline=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:p>", str),<EOL>vectors=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:v>", str),<EOL>n_iter=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:n>", int),<EOL>n_early_stopping=(<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>int,<EOL>),<EOL>n_examples=("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", int),<EOL>use_gpu=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:g>", int),<EOL>version=("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", str),<EOL>meta_path=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:m>", Path),<EOL>init_tok2vec=(<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>Path,<EOL>),<EOL>parser_multitasks=(<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>str,<EOL>),<EOL>entity_multitasks=(<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>str,<EOL>),<EOL>noise_level=("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", float),<EOL>eval_beam_widths=("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", str),<EOL>gold_preproc=("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", bool),<EOL>learn_tokens=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:T>", bool),<EOL>verbose=("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", bool),<EOL>debug=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:D>", bool),<EOL>)<EOL>def train(<EOL>lang,<EOL>output_path,<EOL>train_path,<EOL>dev_path,<EOL>raw_text=None,<EOL>base_model=None,<EOL>pipeline="<STR_LIT>",<EOL>vectors=None,<EOL>n_iter=<NUM_LIT:30>,<EOL>n_early_stopping=None,<EOL>n_examples=<NUM_LIT:0>,<EOL>use_gpu=-<NUM_LIT:1>,<EOL>version="<STR_LIT>",<EOL>meta_path=None,<EOL>init_tok2vec=None,<EOL>parser_multitasks="<STR_LIT>",<EOL>entity_multitasks="<STR_LIT>",<EOL>noise_level=<NUM_LIT:0.0>,<EOL>eval_beam_widths="<STR_LIT>",<EOL>gold_preproc=False,<EOL>learn_tokens=False,<EOL>verbose=False,<EOL>debug=False,<EOL>):
msg = Printer()<EOL>util.fix_random_seed()<EOL>util.set_env_log(verbose)<EOL>train_path = util.ensure_path(train_path)<EOL>dev_path = util.ensure_path(dev_path)<EOL>meta_path = util.ensure_path(meta_path)<EOL>output_path = util.ensure_path(output_path)<EOL>if raw_text is not None:<EOL><INDENT>raw_text = list(srsly.read_jsonl(raw_text))<EOL><DEDENT>if not train_path or not train_path.exists():<EOL><INDENT>msg.fail("<STR_LIT>", train_path, exits=<NUM_LIT:1>)<EOL><DEDENT>if not dev_path or not dev_path.exists():<EOL><INDENT>msg.fail("<STR_LIT>", dev_path, exits=<NUM_LIT:1>)<EOL><DEDENT>if meta_path is not None and not meta_path.exists():<EOL><INDENT>msg.fail("<STR_LIT>", meta_path, exits=<NUM_LIT:1>)<EOL><DEDENT>meta = srsly.read_json(meta_path) if meta_path else {}<EOL>if output_path.exists() and [p for p in output_path.iterdir() if p.is_dir()]:<EOL><INDENT>msg.warn(<EOL>"<STR_LIT>",<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>",<EOL>)<EOL><DEDENT>if not output_path.exists():<EOL><INDENT>output_path.mkdir()<EOL><DEDENT>dropout_rates = util.decaying(<EOL>util.env_opt("<STR_LIT>", <NUM_LIT>),<EOL>util.env_opt("<STR_LIT>", <NUM_LIT>),<EOL>util.env_opt("<STR_LIT>", <NUM_LIT:0.0>),<EOL>)<EOL>batch_sizes = util.compounding(<EOL>util.env_opt("<STR_LIT>", <NUM_LIT>),<EOL>util.env_opt("<STR_LIT>", <NUM_LIT>),<EOL>util.env_opt("<STR_LIT>", <NUM_LIT>),<EOL>)<EOL>if not eval_beam_widths:<EOL><INDENT>eval_beam_widths = [<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>eval_beam_widths = [int(bw) for bw in eval_beam_widths.split("<STR_LIT:U+002C>")]<EOL>if <NUM_LIT:1> not in eval_beam_widths:<EOL><INDENT>eval_beam_widths.append(<NUM_LIT:1>)<EOL><DEDENT>eval_beam_widths.sort()<EOL><DEDENT>has_beam_widths = eval_beam_widths != [<NUM_LIT:1>]<EOL>pipeline = [p.strip() for p in pipeline.split("<STR_LIT:U+002C>")]<EOL>msg.text("<STR_LIT>".format(pipeline))<EOL>if base_model:<EOL><INDENT>msg.text("<STR_LIT>".format(base_model))<EOL>nlp = util.load_model(base_model)<EOL>if nlp.lang != lang:<EOL><INDENT>msg.fail(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(nlp.lang, lang),<EOL>exits=<NUM_LIT:1>,<EOL>)<EOL><DEDENT>other_pipes = [pipe for pipe in nlp.pipe_names if pipe not in pipeline]<EOL>nlp.disable_pipes(*other_pipes)<EOL>for pipe in pipeline:<EOL><INDENT>if pipe not in nlp.pipe_names:<EOL><INDENT>nlp.add_pipe(nlp.create_pipe(pipe))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>msg.text("<STR_LIT>".format(lang))<EOL>lang_cls = util.get_lang_class(lang)<EOL>nlp = lang_cls()<EOL>for pipe in pipeline:<EOL><INDENT>nlp.add_pipe(nlp.create_pipe(pipe))<EOL><DEDENT><DEDENT>if learn_tokens:<EOL><INDENT>nlp.add_pipe(nlp.create_pipe("<STR_LIT>"))<EOL><DEDENT>if vectors:<EOL><INDENT>msg.text("<STR_LIT>".format(vectors))<EOL>_load_vectors(nlp, vectors)<EOL><DEDENT>multitask_options = [("<STR_LIT>", parser_multitasks), ("<STR_LIT>", entity_multitasks)]<EOL>for pipe_name, multitasks in multitask_options:<EOL><INDENT>if multitasks:<EOL><INDENT>if pipe_name not in pipeline:<EOL><INDENT>msg.fail(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(pipe_name)<EOL>)<EOL><DEDENT>pipe = nlp.get_pipe(pipe_name)<EOL>for objective in multitasks.split("<STR_LIT:U+002C>"):<EOL><INDENT>pipe.add_multitask_objective(objective)<EOL><DEDENT><DEDENT><DEDENT>msg.text("<STR_LIT>".format(n_examples))<EOL>corpus = GoldCorpus(train_path, dev_path, limit=n_examples)<EOL>n_train_words = corpus.count_train()<EOL>if base_model:<EOL><INDENT>optimizer = create_default_optimizer(Model.ops)<EOL><DEDENT>else:<EOL><INDENT>optimizer = nlp.begin_training(lambda: corpus.train_tuples, device=use_gpu)<EOL><DEDENT>nlp._optimizer = None<EOL>if init_tok2vec is not None:<EOL><INDENT>components = _load_pretrained_tok2vec(nlp, init_tok2vec)<EOL>msg.text("<STR_LIT>".format(components))<EOL><DEDENT>row_head = ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT>"]<EOL>row_widths = [<NUM_LIT:3>, <NUM_LIT:10>, <NUM_LIT:10>, <NUM_LIT:7>, <NUM_LIT:7>, <NUM_LIT:7>, <NUM_LIT:7>, <NUM_LIT:7>, <NUM_LIT:7>, <NUM_LIT:7>, <NUM_LIT:7>]<EOL>if has_beam_widths:<EOL><INDENT>row_head.insert(<NUM_LIT:1>, "<STR_LIT>")<EOL>row_widths.insert(<NUM_LIT:1>, <NUM_LIT:7>)<EOL><DEDENT>row_settings = {"<STR_LIT>": row_widths, "<STR_LIT>": tuple(["<STR_LIT:r>" for i in row_head]), "<STR_LIT>": <NUM_LIT:2>}<EOL>print("<STR_LIT>")<EOL>msg.row(row_head, **row_settings)<EOL>msg.row(["<STR_LIT:->" * width for width in row_settings["<STR_LIT>"]], **row_settings)<EOL>try:<EOL><INDENT>iter_since_best = <NUM_LIT:0><EOL>best_score = <NUM_LIT:0.0><EOL>for i in range(n_iter):<EOL><INDENT>train_docs = corpus.train_docs(<EOL>nlp, noise_level=noise_level, gold_preproc=gold_preproc, max_length=<NUM_LIT:0><EOL>)<EOL>if raw_text:<EOL><INDENT>random.shuffle(raw_text)<EOL>raw_batches = util.minibatch(<EOL>(nlp.make_doc(rt["<STR_LIT:text>"]) for rt in raw_text), size=<NUM_LIT:8><EOL>)<EOL><DEDENT>words_seen = <NUM_LIT:0><EOL>with tqdm.tqdm(total=n_train_words, leave=False) as pbar:<EOL><INDENT>losses = {}<EOL>for batch in util.minibatch_by_words(train_docs, size=batch_sizes):<EOL><INDENT>if not batch:<EOL><INDENT>continue<EOL><DEDENT>docs, golds = zip(*batch)<EOL>nlp.update(<EOL>docs,<EOL>golds,<EOL>sgd=optimizer,<EOL>drop=next(dropout_rates),<EOL>losses=losses,<EOL>)<EOL>if raw_text:<EOL><INDENT>raw_batch = list(next(raw_batches))<EOL>nlp.rehearse(raw_batch, sgd=optimizer, losses=losses)<EOL><DEDENT>if not int(os.environ.get("<STR_LIT>", <NUM_LIT:0>)):<EOL><INDENT>pbar.update(sum(len(doc) for doc in docs))<EOL><DEDENT>words_seen += sum(len(doc) for doc in docs)<EOL><DEDENT><DEDENT>with nlp.use_params(optimizer.averages):<EOL><INDENT>util.set_env_log(False)<EOL>epoch_model_path = output_path / ("<STR_LIT>" % i)<EOL>nlp.to_disk(epoch_model_path)<EOL>nlp_loaded = util.load_model_from_path(epoch_model_path)<EOL>for beam_width in eval_beam_widths:<EOL><INDENT>for name, component in nlp_loaded.pipeline:<EOL><INDENT>if hasattr(component, "<STR_LIT>"):<EOL><INDENT>component.cfg["<STR_LIT>"] = beam_width<EOL><DEDENT><DEDENT>dev_docs = list(<EOL>corpus.dev_docs(nlp_loaded, gold_preproc=gold_preproc)<EOL>)<EOL>nwords = sum(len(doc_gold[<NUM_LIT:0>]) for doc_gold in dev_docs)<EOL>start_time = timer()<EOL>scorer = nlp_loaded.evaluate(dev_docs, debug)<EOL>end_time = timer()<EOL>if use_gpu < <NUM_LIT:0>:<EOL><INDENT>gpu_wps = None<EOL>cpu_wps = nwords / (end_time - start_time)<EOL><DEDENT>else:<EOL><INDENT>gpu_wps = nwords / (end_time - start_time)<EOL>with Model.use_device("<STR_LIT>"):<EOL><INDENT>nlp_loaded = util.load_model_from_path(epoch_model_path)<EOL>for name, component in nlp_loaded.pipeline:<EOL><INDENT>if hasattr(component, "<STR_LIT>"):<EOL><INDENT>component.cfg["<STR_LIT>"] = beam_width<EOL><DEDENT><DEDENT>dev_docs = list(<EOL>corpus.dev_docs(nlp_loaded, gold_preproc=gold_preproc)<EOL>)<EOL>start_time = timer()<EOL>scorer = nlp_loaded.evaluate(dev_docs)<EOL>end_time = timer()<EOL>cpu_wps = nwords / (end_time - start_time)<EOL><DEDENT><DEDENT>acc_loc = output_path / ("<STR_LIT>" % i) / "<STR_LIT>"<EOL>srsly.write_json(acc_loc, scorer.scores)<EOL>meta["<STR_LIT>"] = nlp.lang<EOL>meta["<STR_LIT>"] = nlp.pipe_names<EOL>meta["<STR_LIT>"] = "<STR_LIT>" % about.__version__<EOL>if beam_width == <NUM_LIT:1>:<EOL><INDENT>meta["<STR_LIT>"] = {<EOL>"<STR_LIT>": nwords,<EOL>"<STR_LIT>": cpu_wps,<EOL>"<STR_LIT>": gpu_wps,<EOL>}<EOL>meta["<STR_LIT>"] = scorer.scores<EOL><DEDENT>else:<EOL><INDENT>meta.setdefault("<STR_LIT>", {})<EOL>meta.setdefault("<STR_LIT>", {})<EOL>meta["<STR_LIT>"][beam_width] = scorer.scores<EOL>meta["<STR_LIT>"][beam_width] = {<EOL>"<STR_LIT>": nwords,<EOL>"<STR_LIT>": cpu_wps,<EOL>"<STR_LIT>": gpu_wps,<EOL>}<EOL><DEDENT>meta["<STR_LIT>"] = {<EOL>"<STR_LIT:width>": nlp.vocab.vectors_length,<EOL>"<STR_LIT>": len(nlp.vocab.vectors),<EOL>"<STR_LIT>": nlp.vocab.vectors.n_keys,<EOL>"<STR_LIT:name>": nlp.vocab.vectors.name,<EOL>}<EOL>meta.setdefault("<STR_LIT:name>", "<STR_LIT>" % i)<EOL>meta.setdefault("<STR_LIT:version>", version)<EOL>meta_loc = output_path / ("<STR_LIT>" % i) / "<STR_LIT>"<EOL>srsly.write_json(meta_loc, meta)<EOL>util.set_env_log(verbose)<EOL>progress = _get_progress(<EOL>i,<EOL>losses,<EOL>scorer.scores,<EOL>beam_width=beam_width if has_beam_widths else None,<EOL>cpu_wps=cpu_wps,<EOL>gpu_wps=gpu_wps,<EOL>)<EOL>msg.row(progress, **row_settings)<EOL><DEDENT>if n_early_stopping is not None:<EOL><INDENT>current_score = _score_for_model(meta)<EOL>if current_score < best_score:<EOL><INDENT>iter_since_best += <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>iter_since_best = <NUM_LIT:0><EOL>best_score = current_score<EOL><DEDENT>if iter_since_best >= n_early_stopping:<EOL><INDENT>msg.text(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(i - iter_since_best)<EOL>)<EOL>msg.text(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(best_score, current_score)<EOL>)<EOL>break<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>with nlp.use_params(optimizer.averages):<EOL><INDENT>final_model_path = output_path / "<STR_LIT>"<EOL>nlp.to_disk(final_model_path)<EOL><DEDENT>msg.good("<STR_LIT>", final_model_path)<EOL>with msg.loading("<STR_LIT>"):<EOL><INDENT>best_model_path = _collate_best_model(meta, output_path, nlp.pipe_names)<EOL><DEDENT>msg.good("<STR_LIT>", best_model_path)<EOL><DEDENT>
Train or update a spaCy model. Requires data to be formatted in spaCy's
JSON format. To convert data from other formats, use the `spacy convert`
command.