Fix pickling error with CSVLogger (#10388)

* Don't store csv.Dictwriter in ExperimentWriter

* Add test for pickle after .save()

* Add entry in changelog
This commit is contained in:
Espen Haugsdal 2021-11-08 11:36:35 +01:00 committed by GitHub
parent c58f84c176
commit 89e1360e75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 3 deletions

View File

@ -98,6 +98,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed failure when `DataLoader(batch_size=None)` is passed ([#10345](https://github.com/PyTorchLightning/pytorch-lightning/issues/10345))
- Fixed issue with pickling `CSVLogger` after a call to `CSVLogger.save` ([#10388](https://github.com/PyTorchLightning/pytorch-lightning/pull/10388))
-

View File

@ -95,9 +95,9 @@ class ExperimentWriter:
metrics_keys = list(last_m.keys())
with open(self.metrics_file_path, "w", newline="") as f:
self.writer = csv.DictWriter(f, fieldnames=metrics_keys)
self.writer.writeheader()
self.writer.writerows(self.metrics)
writer = csv.DictWriter(f, fieldnames=metrics_keys)
writer.writeheader()
writer.writerows(self.metrics)
class CSVLogger(LightningLoggerBase):

View File

@ -263,6 +263,10 @@ def _test_loggers_pickle(tmpdir, monkeypatch, logger_class):
# the logger needs to remove it from the state before pickle
_ = logger.experiment
# logger also has to avoid adding un-picklable attributes to self in .save
logger.log_metrics({"a": 1})
logger.save()
# test pickling loggers
pickle.dumps(logger)