Skip to content

Fix checkpoint history saving to file-like objects - #1179

Closed
mottopanikeiku wants to merge 1 commit into
skorch-dev:masterfrom
mottopanikeiku:fix/checkpoint-history-file-like
Closed

mottopanikeiku wants to merge 1 commit into
skorch-dev:masterfrom
mottopanikeiku:fix/checkpoint-history-file-like

Conversation

@mottopanikeiku

Copy link
Copy Markdown

Checkpoint's f_history is documented to accept a file-like object, but Checkpoint.f_history_ only special-cases None and otherwise does os.path.join(self.dirname, self.fn_prefix + self.f_history). With an open file this raises TypeError: can only concatenate str (not "_io.TextIOWrapper") to str during fit; TrainEndCheckpoint fails the same way at the end of training. I found this by reading and testing the checkpoint code.

I return non-string targets unchanged, the same way _format_target already treats f_params, f_optimizer and the other targets. String names still get dirname and fn_prefix, and None still means no history file.

from pathlib import Path
from tempfile import TemporaryDirectory

import numpy as np
from torch import nn
from skorch import NeuralNetRegressor
from skorch.callbacks import Checkpoint, TrainEndCheckpoint

X = np.zeros((10, 1), dtype=np.float32)
y = np.zeros((10, 1), dtype=np.float32)

with TemporaryDirectory() as dirname:
    for callback_cls in (Checkpoint, TrainEndCheckpoint):
        history_path = Path(dirname) / "history.json"
        with history_path.open("w") as history_file:
            callback = callback_cls(
                f_params=None, f_optimizer=None, f_criterion=None, f_history=history_file
            )
            net = NeuralNetRegressor(
                nn.Linear, module__in_features=1, module__out_features=1,
                callbacks=[callback], max_epochs=1, verbose=0,
            )
            try:
                net.fit(X, y)
                net.load_params(f_history=history_path)
                print(callback_cls.__name__, "saved and reloaded", len(net.history), "epoch")
            except Exception as exc:
                print(callback_cls.__name__, type(exc).__name__, exc)
Callback Before After
Checkpoint(f_history=<open file>) TypeError during fit history saved and reloads
TrainEndCheckpoint(f_history=<open file>) TypeError at train end history saved and reloads

Tests: test_save_history_to_file_like for both TestCheckpoint and TestTrainEndCheckpoint, with and without fn_prefix. They fit a small net, check the callback keeps the given file object, and reload the saved history with load_params. All 4 fail on master and pass with the fix; the whole skorch/tests/callbacks/test_training.py passes (88). flake8 reports nothing on the changed lines. I also added an entry to CHANGES.md.

Related: #870 touches the same property (callable directory names) but doesn't handle file-like targets.

I made this PR and directed its development, with AI assistance from Claude and GPT-6 Astra.

I found that checkpoint history saving treated file-like objects as filename strings and raised a TypeError. I now preserve non-string history targets so both Checkpoint and TrainEndCheckpoint can write to them. I added regression tests for both callbacks with and without filename prefixes.

Assisted-by: Claude
Assisted-by: GPT-6 Astra
Signed-off-by: mottopanikeiku <176798723+mottopanikeiku@users.noreply.github.com>
@BenjaminBossan

Copy link
Copy Markdown
Collaborator

Thanks for the PR. As maintainers of skorch, we decided to update the status of the skorch project. As a consequence, we will close this PR. Your effort is still appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants