example of custom logger

This commit is contained in:
svlandeg 2020-08-31 14:24:41 +02:00
parent 2c90a06fee
commit 13ee742fb4
2 changed files with 46 additions and 5 deletions

View File

@ -270,7 +270,7 @@ def train_while_improving(
epoch (int): How many passes over the data have been completed. epoch (int): How many passes over the data have been completed.
step (int): How many steps have been completed. step (int): How many steps have been completed.
score (float): The main score form the last evaluation. score (float): The main score from the last evaluation.
other_scores: : The other scores from the last evaluation. other_scores: : The other scores from the last evaluation.
losses: The accumulated losses throughout training. losses: The accumulated losses throughout training.
checkpoints: A list of previous results, where each result is a checkpoints: A list of previous results, where each result is a

View File

@ -614,14 +614,55 @@ dictionary providing the following information:
| -------------- | ---------------------------------------------------------------------------------------------- | | -------------- | ---------------------------------------------------------------------------------------------- |
| `epoch` | How many passes over the data have been completed. ~~int~~ | | `epoch` | How many passes over the data have been completed. ~~int~~ |
| `step` | How many steps have been completed. ~~int~~ | | `step` | How many steps have been completed. ~~int~~ |
| `score` | The main score form the last evaluation, measured on the dev set. ~~float~~ | | `score` | The main score from the last evaluation, measured on the dev set. ~~float~~ |
| `other_scores` | The other scores from the last evaluation, measured on the dev set. ~~Dict[str, Any]~~ | | `other_scores` | The other scores from the last evaluation, measured on the dev set. ~~Dict[str, Any]~~ |
| `losses` | The accumulated training losses. ~~Dict[str, float]~~ | | `losses` | The accumulated training losses, keyed by component name. ~~Dict[str, float]~~ |
| `checkpoints` | A list of previous results, where each result is a (score, step, epoch) tuple. ~~List[Tuple]~~ | | `checkpoints` | A list of previous results, where each result is a (score, step, epoch) tuple. ~~List[Tuple]~~ |
By default, these results are written to the console with the [`ConsoleLogger`](/api/top-level#ConsoleLogger) By default, these results are written to the console with the
[`ConsoleLogger`](/api/top-level#ConsoleLogger). There is also built-in support
for writing the log files to [Weights & Biases](https://www.wandb.com/) with
the [`WandbLogger`](/api/top-level#WandbLogger). But you can easily implement
your own logger as well, for instance to write the tabular results to file:
# TODO ```python
### functions.py
from typing import Tuple, Callable, Dict, Any
import spacy
from pathlib import Path
@spacy.registry.loggers("my_custom_logger.v1")
def custom_logger(log_path):
def setup_logger(nlp: "Language") -> Tuple[Callable, Callable]:
with Path(log_path).open("w") as file_:
file_.write("step\t")
file_.write("score\t")
for pipe in nlp.pipe_names:
file_.write(f"loss_{pipe}\t")
file_.write("\n")
def log_step(info: Dict[str, Any]):
with Path(log_path).open("a") as file_:
file_.write(f"{info['step']}\t")
file_.write(f"{info['score']}\t")
for pipe in nlp.pipe_names:
file_.write(f"{info['losses'][pipe]}\t")
file_.write("\n")
def finalize():
pass
return log_step, finalize
return setup_logger
```
```ini
### config.cfg (excerpt)
[training.logger]
@loggers = "my_custom_logger.v1"
file_path = "my_file.tab"
```
#### Example: Custom batch size schedule {#custom-code-schedule} #### Example: Custom batch size schedule {#custom-code-schedule}