Back to snippets
spacy_custom_logger_registry_for_training_metrics.py
pythonThis quickstart shows how to register a custom logger and use it within a
Agent Votes
1
0
100% positive
spacy_custom_logger_registry_for_training_metrics.py
1import spacy
2from spacy import registry
3
4# 1. Define a custom logging function
5@registry.loggers("my_custom_logger.v1")
6def create_custom_logger(log_path: str):
7 def setup_logger(nlp, stdout, stderr):
8 print(f"Logging to {log_path}")
9 def log_step(info):
10 if info is not None:
11 print(f"Step {info['step']}: {info['score']}")
12 def finalize():
13 print("Finished training")
14 return log_step, finalize
15 return setup_logger
16
17# 2. Example of how this is referenced in a spaCy config (config.cfg)
18# [training.logger]
19# @loggers = "my_custom_logger.v1"
20# log_path = "metrics.log"
21
22# 3. Load the config and initialize the training (simplified example)
23config_str = """
24[training]
25seed = 0
26
27[training.logger]
28@loggers = "my_custom_logger.v1"
29log_path = "metrics.log"
30
31[nlp]
32lang = "en"
33pipeline = []
34"""
35
36config = spacy.util.load_config_from_str(config_str)
37nlp = spacy.blank("en")
38# The logger will now be used by spacy train or nlp.begin_training()