Installation and Usage Guide
🔨 Installation
To install BOAT (PyTorch version), we recommend using a virtual environment.
1. Create Environment
conda create -n boat python=3.12
conda activate boat
2. Create Environment
You can install the latest stable version from PyPI or the latest development version from GitHub:
# Install from PyPI
pip install boat-torch
# Or install from Source
git clone [https://github.com/callous-youth/BOAT.git](https://github.com/callous-youth/BOAT.git)
cd BOAT
pip install -e .
⚡ How to Use BOAT
1. Load Configuration Files
BOAT relies on two key configuration files:
boat_config.json: Specifies optimization strategies and dynamic/hyper-gradient operations.loss_config.json: Defines the loss functions for both levels of the BLO process.
import os
import json
import boat_torch as boat
# Load configuration files
with open("path_to_configs/boat_config.json", "r") as f:
boat_config = json.load(f)
with open("path_to_configs/loss_config.json", "r") as f:
loss_config = json.load(f)
2. Define Models and Optimizers
You need to specify both the upper-level and lower-level models along with their respective optimizers.
import torch
# Define models
upper_model = UpperModel(*args, **kwargs) # Replace with your upper-level model
lower_model = LowerModel(*args, **kwargs) # Replace with your lower-level model
# Define optimizers
upper_opt = torch.optim.Adam(upper_model.parameters(), lr=0.01)
lower_opt = torch.optim.SGD(lower_model.parameters(), lr=0.01)
3. Customize BOAT Configuration
Modify the boat_config to include your gradient mapping and numerical approximation opreation, as well as model and variable details.
# Example gradient mapping and numerical approximation opreation Combination.
gm_op = ["NGD", "DI", "GDA"] # Dynamic Methods (Demo Only)
na_op = ["RGT","RAD"] # Hyper-Gradient Methods (Demo Only)
# NOTE:
# - gm_op / na_op select valid GM-OL and NA-OL operator combinations.
# - The execution order is internally resolved by BOAT priority rules.
# - First-order methods (FO-OL), e.g., ["VSO", "VFO", "MESO", "PGDO"],
# are alternative optimization strategies and should generally not be
# enabled together with na_op.
# Add methods and model details to the configuration
boat_config["gm_op"] = gm_op
boat_config["na_op"] = na_op
boat_config["lower_level_model"] = lower_model
boat_config["upper_level_model"] = upper_model
boat_config["lower_level_opt"] = lower_opt
boat_config["upper_level_opt"] = upper_opt
boat_config["lower_level_var"] = list(lower_model.parameters())
boat_config["upper_level_var"] = list(upper_model.parameters())
# Initialize the BOAT core
b_optimizer = boat.Problem(boat_config, loss_config)
4. Initialize the BOAT Problem
Modify the boat_config to include your gradient mapping and numerical approximation opreation, as well as model and variable details.
# Initialize the problem
b_optimizer = boat.Problem(boat_config, loss_config)
# Build solvers for lower and upper levels
b_optimizer.build_ll_solver() # Lower-level solver
b_optimizer.build_ul_solver() # Upper-level solver
5. Define Data Feeds
Prepare the data feeds for both levels of the BLO process, which was further fed into the the upper-level and lower-level objective functions.
# Define data feeds (Demo Only)
ul_feed_dict = {"data": upper_level_data, "target": upper_level_target}
ll_feed_dict = {"data": lower_level_data, "target": lower_level_target}
6. Run the Optimization Loop
Execute the optimization loop, optionally customizing the solver strategy for gradient mapping operations.
# Set number of iterations
iterations = 1000
# Optimization loop (Demo Only)
for x_itr in range(iterations):
# Run a single optimization iteration
loss, run_time = b_optimizer.run_iter(ll_feed_dict, ul_feed_dict, current_iter=x_itr)