gpt-4.1-mini-2025-04-14· openai
from WildChat-4.8M
how to keep archives used in colab like checkpoints?
- User
how to keep archives used in colab like checkpoints?
- Assistant
In Google Colab, the environment is ephemeral, meaning any files you create or modify during a session are lost once the session ends. To keep archives or checkpoints persistently in Colab, you need to save them externally, typically by:
- Saving to Google Drive.
- Downloading files locally.
- Uploading to external storage (e.g., AWS S3, GCP Storage).
Method 1: Save archives/checkpoints to Google Drive
You can mount your Google Drive and save files directly there so they persist beyond the Colab session.
Steps:
- Mount Google Drive:
pythonfrom google.colab import drive drive.mount('/content/drive')- Write your checkpoint/archive files to a directory inside the mounted Drive:
python# Example: Save checkpoint as a tar.gz archive import tarfile archive_path = '/content/drive/MyDrive/checkpoints/archive.tar.gz' with tarfile.open(archive_path, 'w:gz') as archive: archive.add('folder_to_archive', arcname='folder_to_archive')- Later you can reload files from your Drive directly.
Method 2: Download archives directly
If your checkpoint is small and you prefer to download manually:
pythonfrom google.colab import files # After creating archive (e.g., archive.tar.gz) files.download('archive.tar.gz')
Bonus: Automatic checkpointing example (saving periodically to Drive)
Example function saving checkpoints during training:
pythonimport time def save_checkpoint(checkpoint_data, filename='/content/drive/MyDrive/checkpoints/model.ckpt'): import torch # example for PyTorch torch.save(checkpoint_data, filename) print(f"Checkpoint saved to {filename}") # Somewhere in your training loop: for epoch in range(num_epochs): # train model # ... if epoch % 5 == 0: # every 5 epochs save_checkpoint(model.state_dict())