Prerequisites
Before you get started, make sure you have the following:Install W&B Python SDK
Install the W&B Python SDK. You can do this usingpip:
Log in and authenticate with W&B
If you have not done so already, log in to W&B. Use thewandb login CLI command and follow the prompts to log in to your W&B account:
wandb login reference documentation for more information on how W&B searches for credentials.
Copy the training script and dependencies
Expand the following dropdown to acess the code required for this tutorial. Copy and paste the code into three separate files in the same directory as this tutorial. In the next section, you will run a script that reads in these files and trains a PyTorch model within a W&B Sandbox environment.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) Lists the files to mount to the sandbox:
train.pyandrequirements.txt. - (Line 12) Start the sandbox. The sandbox is configured to use the
python:3.13container image, have internet access, and a maximum lifetime of 3600 seconds (1 hour). - (Line 18) Write the
hyperparameters.yamlfile to the sandbox. This allows the training script (train.py) 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 PyTorch model on the UCI Zoo dataset and saves the trained model to a file namedzoo_wandb.pth. - (Lines 27-29) 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 33-34) Download the generated model file. You read the
zoo_wandb.pthfile from the sandbox using theread_file()method and save it locally.