Perch is the user-facing entry point for configuring and running Pyperch. It provides a builder interface that constructs models, optimizers, data loaders, and training configuration.
A complete experiment usually follows this pattern:
- Define a model
- Select optimizers (meta and/or gradient)
- Configure metrics
- Provide data
- Train
perch = (
Perch()
.model(SimpleMLP, input_dim=12, hidden=[32], output_dim=2)
.optimizer("rhc", step_size=0.05)
.metrics("accuracy")
.data(X, y, batch_size=64, valid_split=0.2)
)
trainer, history = perch.train(max_epochs=250, seed=42)- Model construction
- Dataset splitting and loader creation
- Optimizer configuration (RHC, SA, GA, Adam, hybrids)
- Layer-level optimization modes (freeze / grad / meta)
- Metric instantiation
- Trainer creation and execution
Define the model class and its constructor arguments.
.model(
SimpleMLP,
input_dim=12,
hidden=[32],
output_dim=2,
activation="relu",
loss_fn=nn.CrossEntropyLoss(),
)Notes:
- The model class must be a
torch.nn.Module loss_fnis optional; defaults are inferred when omitted
Configure the meta-optimizer (RHC, SA, GA).
.optimizer("sa", t=2.0, cooling=0.995, step_size=0.05)Supported optimizers include:
"rhc""sa""ga"
Only keyword arguments supported by OptimizerConfig are kept.
Configure a PyTorch optimizer for gradient-based layers.
.torch_optimizer("adam", lr=1e-3)This is optional and only used when .grad_opt(...) is applied.
Specify metrics by name, class, or instance.
.metrics("accuracy", F1())Metrics are automatically instantiated for both training and validation.
Provide raw arrays for automatic loader construction.
.data(
X, y,
batch_size=64,
valid_split=0.2,
stratify=True,
)Common options include:
batch_sizevalid_splitshufflestratifynormalize
Use pre-built PyTorch data loaders instead of raw arrays.
Layer behavior is controlled using parameter name patterns.
.freeze("net.0.weight", "net.0.bias")Frozen layers:
- Are excluded from both gradient and meta optimization
- Remain unchanged throughout training
.grad_opt("net.2.weight", "net.2.bias")These layers are optimized using the configured PyTorch optimizer.
.meta_opt("net.4.weight", "net.4.bias")These layers are optimized using the configured meta-optimizer (RHC / SA / GA).
perch = (
Perch()
.model(SimpleMLP, input_dim=10, hidden=[32, 16], output_dim=2)
.freeze("net.0.weight", "net.0.bias")
.grad_opt("net.2.weight", "net.2.bias")
.meta_opt("net.4.weight", "net.4.bias")
.optimizer("rhc", step_size=0.5)
.torch_optimizer("adam", lr=1e-3)
.metrics("accuracy")
.data(X, y, batch_size=32, valid_split=0.2)
)trainer, history = perch.train(
max_epochs=200,
seed=42,
optimizer_mode="per_batch",
)Returns:
Trainer- fully configured trainer instancehistory- dictionary of losses and metrics over time