saealib.TorchSurrogate#
- class saealib.TorchSurrogate(model, optimizer_cls=None, optimizer_kwargs=None, loss_fn=None, epochs=100)[source]#
Bases:
RegressionSurrogateSurrogate adapter for PyTorch
nn.Modulemodels.Wraps any
torch.nn.Modulethat maps(n_samples, n_features)to(n_samples, n_obj). The model is trained from scratch on eachfitcall by resetting to the weights captured at construction time, so this adapter is safe to use withLocalSurrogateManager(which re-fits per candidate).- Parameters:
model (torch.nn.Module) – Neural network model. Its output shape must be
(n_samples, n_obj).optimizer_cls (type or None) – Optimizer class (e.g.
torch.optim.Adam). Defaults totorch.optim.Adam.optimizer_kwargs (dict or None) – Keyword arguments forwarded to the optimizer constructor. Defaults to
{}.loss_fn (callable or None) – Loss function. Defaults to
torch.nn.MSELoss().epochs (int) – Number of training epochs per
fitcall. Default: 100.
- Raises:
ImportError – If PyTorch is not installed.
Methods
Return the surrogate model contract. |
|
Fit the surrogate model. |
|
Post-fit lifecycle hook; override to inject custom processing. |
|
Predict using the surrogate model. |
|
Return a copy of this surrogate with |
Method Details
- TorchSurrogate.__init__(model, optimizer_cls=None, optimizer_kwargs=None, loss_fn=None, epochs=100)[source]#
- Parameters:
model (torch.nn.Module)
optimizer_cls (type | None)
optimizer_kwargs (dict | None)
loss_fn (Callable[..., Any] | None)
epochs (int)
- Return type:
None
- TorchSurrogate.contract()#
Return the surrogate model contract.
- Return type:
- TorchSurrogate.fit(train_x, train_y)[source]#
Fit the surrogate model.
Resets model weights to their initial values before training.
- Parameters:
train_x (np.ndarray) – Training input data. shape: (n_samples, n_features)
train_y (np.ndarray) – Training output data. shape: (n_samples, n_obj) or (n_samples,).
- Return type:
None
- TorchSurrogate.post_fit(train_x, train_y, ctx=None)#
Post-fit lifecycle hook; override to inject custom processing.
- Parameters:
train_x (np.ndarray) – Training input data used for this fit. shape: (n_samples, n_features)
train_y (np.ndarray) – Training output data used for this fit. shape: (n_samples, n_obj)
ctx (OptimizationState or None, optional) – Current optimization context.
- Return type:
None