In this tutorial, you train a PyTorch model in a Serverless Sandbox environment. To do this, you start a sandbox with the appropriate environment variables, install the necessary dependencies, and run a Python script. The script trains a neural network on the UCI Zoo dataset. By the end of this tutorial, you have a trained PyTorch model file saved locally. This demonstrates how to use a Serverless Sandbox to run isolated ML training workloads without configuring local infrastructure. This tutorial is intended for ML practitioners and developers who want to evaluate Serverless Sandboxes for reproducible training jobs.Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-style-guide-sandboxes-20260526-215517.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
Before you get started, complete the following setup steps.Install the W&B Python SDK
The W&B Python SDK provides theSandbox interface you use later to create and interact with the Serverless Sandbox. Install it using pip:
Log in and authenticate with W&B
W&B Serverless Sandboxes run under your W&B account, so you must authenticate before you create one. Use thewandb login CLI command and follow the prompts to log in:
wandb login reference documentation for more information about how W&B searches for credentials.
Copy the training script and dependencies
Prepare the three files required for this tutorial: a requirements file, a hyperparameters file, and a training script. Expand the following dropdown, then copy each code sample into a separate file in the same directory as this tutorial. In the next section, you run a script that reads these files and trains a PyTorch model in a W&B Serverless Sandbox.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 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
With your training files in place, create and manage a W&B Serverless Sandbox from a single Python script. The following code snippet creates a sandbox, copies the training script and dependencies into it, runs the training script, and downloads the generated model file. The next section explains the code line by line. 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) List 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 lets the training script (train.py) access the hyperparameters when it runs. - (Line 22) Install dependencies. The command
pip install -r requirements.txtruns inside the sandbox to install the necessary dependencies for the training script. - (Line 26) Run the training script. The command
python train.py --config hyperparameters.yamlruns inside 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, the standard output, standard error, and exit code are printed to the console for debugging and verification.
- (Lines 33-34) Download the generated model file. The
read_file()method readszoo_wandb.pthfrom the sandbox, and the script saves it locally.
zoo_wandb.pth in your working directory. The sandbox that produced it is created, used, and torn down on demand.