Skip to content

Using tempfile in PyTorch checkpoint example #299

@not522

Description

@not522

The PyTorch checkpoint example uses temporary files, which can be replaced with tempfile.

download_artifact(
artifact_store=artifact_store,
file_path=f"./tmp_model_{trial.number}.pt",
artifact_id=artifact_id,
)
checkpoint = torch.load(f"./tmp_model_{trial.number}.pt")
os.remove(f"./tmp_model_{trial.number}.pt")

torch.save(
{
"epoch": epoch,
"model_state_dict": model.state_dict(),
"optimizer_state_dict": optimizer.state_dict(),
"accuracy": accuracy,
},
f"./tmp_model_{trial.number}.pt",
)
artifact_id = upload_artifact(
artifact_store=artifact_store,
file_path=f"./tmp_model_{trial.number}.pt",
study_or_trial=trial,
)
trial.set_user_attr("artifact_id", artifact_id)
os.remove(f"./tmp_model_{trial.number}.pt")

Metadata

Metadata

Assignees

No one assigned

    Labels

    code-fixChange that does not change the behavior, such as code refactoring.staleExempt from stale bot labeling.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions