Prerequisites
Install W&B Python SDK
Install the W&B Python SDK. You can do this usingpip:
Store API keys in environment variables
If you have not done so already, store your W&B API key. Run thewandb login CLI command and follow the prompts to log in to your W&B account:
wandb login reference documenation for more information on how W&B searches for credentials.
Copy the training script and dependencies
PyTorch training model script
PyTorch training model script
Copy and paste the following code into a file named Copy and paste the following code into a YAML file named Copy and paste the following code into a file named
requirements.txt. This file contains the dependencies for the training script.requirements.txt
hyperparameters.yaml. This file contains the hyperparameters for the training script.hyperparameters.yaml
train.py. This script trains a simple PyTorch model on the UCI Zoo dataset and saves the trained model to a file named zoo_wandb.pth.train.py
Create the sandbox and run the training script
The following code snippet shows how to create a sandbox, copy the training script and dependencies into it, run the training script, and download the generated model file. The next section provides a line-by-line explanation of the code. Copy and paste the following code into a Python file and run it. Save it in the same directory as thetrain.py, requirements.txt, and hyperparameters.yaml files you created in the previous step.
train_in_sandbox.py
- (Lines 6 - 9) Mount files to the sandbox at startup. The
mounted_filesparameter inSandbox.run()allows you to specify a list of files to mount into the sandbox at startup. Each file is represented as a dictionary with two keys:mount_path, which specifies the path where the file will be mounted inside the sandbox, andfile_content, which contains the content of the file as bytes. In this example, you mounttrain.pyandrequirements.txtinto the sandbox. - (Line 12) Start the sandbox with the specified configuration. You use a context manager (
withstatement) to ensure that the sandbox is properly stopped after use. The sandbox is configured to use thepython:3.13container image, have internet access, and a maximum lifetime of 3600 seconds (1 hour). - (Line 18) Write the
hyperparameters.yamlfile to the sandbox. You read the content of thehyperparameters.yamlfile and write it to the sandbox using thewrite_file()method. This allows the training script to access the hyperparameters when it runs. - (Line 22) Install dependencies. You run the command
pip install -r requirements.txtinside the sandbox to install the necessary dependencies for the training script. - (Line 26) Run the training script. You execute the command
python train.py --config hyperparameters.yamlinside the sandbox to start the training process. The script trains a simple PyTorch model on the UCI Zoo dataset and saves the trained model to a file namedzoo_wandb.pth. - (Lines 29-31) Print the output and exit code. After the training script finishes executing, you print the standard output, standard error, and exit code to the console for debugging and verification purposes.
- (Lines 35-36) Download the generated model file. You read the
zoo_wandb.pthfile from the sandbox using theread_file()method and save it locally.