Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using tempfile in PyTorch checkpoint example #299

Open
not522 opened this issue Jan 29, 2025 · 0 comments
Open

Using tempfile in PyTorch checkpoint example #299

not522 opened this issue Jan 29, 2025 · 0 comments
Labels
code-fix Change that does not change the behavior, such as code refactoring.

Comments

@not522
Copy link
Member

not522 commented Jan 29, 2025

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")

@not522 not522 added the code-fix Change that does not change the behavior, such as code refactoring. label Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-fix Change that does not change the behavior, such as code refactoring.
Projects
None yet
Development

No branches or pull requests

1 participant