Optimization¶
anfis_toolbox.optim.base.BaseTrainer ¶
Bases: ABC
Shared training loop for ANFIS trainers.
compute_loss
abstractmethod
¶
Compute loss for the provided data without mutating the model.
fit ¶
fit(
model: ModelLike,
X: ndarray,
y: ndarray,
*,
validation_data: tuple[ndarray, ndarray] | None = None,
validation_frequency: int = 1,
) -> TrainingHistory
Train model on (X, y) and optionally evaluate on validation data.
Returns a dictionary containing the per-epoch training losses and, when
validation_data is provided, the validation losses (aligned with the
training epochs; epochs without validation are recorded as None).
Source code in anfis_toolbox/optim/base.py
init_state
abstractmethod
¶
Initialize and return any optimizer-specific state.
Called once before training begins. Trainers that don't require state may return None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
ModelLike
|
The model to be trained. |
required |
X
|
ndarray
|
The full training inputs. |
required |
y
|
ndarray
|
The full training targets. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Optimizer state (or None) to be threaded through |
Source code in anfis_toolbox/optim/base.py
train_step
abstractmethod
¶
Perform a single training step on a batch and return (loss, new_state).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
ModelLike
|
The model to be trained. |
required |
Xb
|
ndarray
|
A batch of inputs. |
required |
yb
|
ndarray
|
A batch of targets. |
required |
state
|
Any
|
Optimizer state produced by |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, Any]
|
tuple[float, Any]: The batch loss and the updated optimizer state. |
Source code in anfis_toolbox/optim/base.py
anfis_toolbox.optim.hybrid.HybridTrainer
dataclass
¶
HybridTrainer(
learning_rate: float = 0.01,
epochs: int = 100,
verbose: bool = False,
_loss_fn: MSELoss = MSELoss(),
)
Bases: BaseTrainer
Original Jang (1993) hybrid training: LSM for consequents + GD for antecedents.
Notes
This trainer assumes a single-output regression head. It is not compatible with
:class:~anfis_toolbox.model.TSKANFISClassifier or the high-level
:class:~anfis_toolbox.classifier.ANFISClassifier facade.
compute_loss ¶
Compute the hybrid MSE loss on prepared data without side effects.
Source code in anfis_toolbox/optim/hybrid.py
init_state ¶
Hybrid trainer doesn't maintain optimizer state; returns None.
train_step ¶
Perform one hybrid step on a batch and return (loss, state).
Equivalent to one iteration of the hybrid algorithm on the given batch.
Source code in anfis_toolbox/optim/hybrid.py
anfis_toolbox.optim.hybrid_adam.HybridAdamTrainer
dataclass
¶
HybridAdamTrainer(
learning_rate: float = 0.001,
beta1: float = 0.9,
beta2: float = 0.999,
epsilon: float = 1e-08,
epochs: int = 100,
verbose: bool = False,
_loss_fn: MSELoss = MSELoss(),
)
Bases: BaseTrainer
Hybrid training: LSM for consequents + Adam for antecedents.
Notes
This variant also targets the regression ANFIS. It is not compatible with the
classification head (:class:~anfis_toolbox.model.TSKANFISClassifier) or
:class:~anfis_toolbox.classifier.ANFISClassifier.
compute_loss ¶
Evaluate mean squared error on provided data without updates.
Source code in anfis_toolbox/optim/hybrid_adam.py
init_state ¶
Initialize Adam moment tensors for membership parameters.
Source code in anfis_toolbox/optim/hybrid_adam.py
train_step ¶
train_step(
model: ModelLike,
Xb: ndarray,
yb: ndarray,
state: dict[str, Any],
) -> tuple[float, dict[str, Any]]
Execute one hybrid iteration combining LSM and Adam updates.
Source code in anfis_toolbox/optim/hybrid_adam.py
anfis_toolbox.optim.sgd.SGDTrainer
dataclass
¶
SGDTrainer(
learning_rate: float = 0.01,
epochs: int = 100,
batch_size: None | int = None,
shuffle: bool = True,
verbose: bool = False,
loss: LossFunction | str | None = None,
)
Bases: BaseTrainer
Stochastic gradient descent trainer for ANFIS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
Step size for gradient descent. |
0.01
|
epochs
|
int
|
Number of passes over the data. |
100
|
batch_size
|
None | int
|
Mini-batch size; if None uses full batch. |
None
|
shuffle
|
bool
|
Whether to shuffle data each epoch. |
True
|
verbose
|
bool
|
Whether to log progress (delegated to model logging settings). |
False
|
Notes
Uses the configurable loss provided via loss (defaults to mean squared error).
The selected loss is responsible for adapting target shapes via prepare_targets.
When used with ANFISClassifier and loss="cross_entropy" it trains on logits with the
appropriate softmax gradient.
anfis_toolbox.optim.adam.AdamTrainer
dataclass
¶
AdamTrainer(
learning_rate: float = 0.001,
beta1: float = 0.9,
beta2: float = 0.999,
epsilon: float = 1e-08,
epochs: int = 100,
batch_size: None | int = None,
shuffle: bool = True,
verbose: bool = False,
loss: LossFunction | str | None = None,
)
Bases: BaseTrainer
Adam optimizer-based trainer for ANFIS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
Base step size (alpha). |
0.001
|
beta1
|
float
|
Exponential decay rate for the first moment estimates. |
0.9
|
beta2
|
float
|
Exponential decay rate for the second moment estimates. |
0.999
|
epsilon
|
float
|
Small constant for numerical stability. |
1e-08
|
epochs
|
int
|
Number of passes over the dataset. |
100
|
batch_size
|
None | int
|
If None, use full-batch; otherwise mini-batches of this size. |
None
|
shuffle
|
bool
|
Whether to shuffle the data at each epoch when using mini-batches. |
True
|
verbose
|
bool
|
Unused here; kept for API parity. |
False
|
Notes
Supports configurable losses via the loss parameter. Defaults to mean squared error for
regression, but can minimize other differentiable objectives such as categorical
cross-entropy when used with ANFISClassifier.
compute_loss ¶
Evaluate the configured loss on (X, y) without updating parameters.
Source code in anfis_toolbox/optim/adam.py
init_state ¶
Initialize Adam's first and second moments and time step.
Returns a dict with keys: params, m, v, t.
Source code in anfis_toolbox/optim/adam.py
train_step ¶
train_step(
model: ModelLike,
Xb: ndarray,
yb: ndarray,
state: dict[str, Any],
) -> tuple[float, dict[str, Any]]
One Adam step on a batch; returns (loss, updated_state).
Source code in anfis_toolbox/optim/adam.py
anfis_toolbox.optim.rmsprop.RMSPropTrainer
dataclass
¶
RMSPropTrainer(
learning_rate: float = 0.001,
rho: float = 0.9,
epsilon: float = 1e-08,
epochs: int = 100,
batch_size: None | int = None,
shuffle: bool = True,
verbose: bool = False,
loss: LossFunction | str | None = None,
)
Bases: BaseTrainer
RMSProp optimizer-based trainer for ANFIS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
Base step size (alpha). |
0.001
|
rho
|
float
|
Exponential decay rate for the squared gradient moving average. |
0.9
|
epsilon
|
float
|
Small constant for numerical stability. |
1e-08
|
epochs
|
int
|
Number of passes over the dataset. |
100
|
batch_size
|
None | int
|
If None, use full-batch; otherwise mini-batches of this size. |
None
|
shuffle
|
bool
|
Whether to shuffle the data at each epoch when using mini-batches. |
True
|
verbose
|
bool
|
Unused here; kept for API parity. |
False
|
Notes
Supports configurable losses via the loss parameter. Defaults to mean squared error for
regression tasks but can be switched to other differentiable objectives such as categorical
cross-entropy when training ANFISClassifier models.
compute_loss ¶
Return the current loss value for (X, y) without modifying state.
Source code in anfis_toolbox/optim/rmsprop.py
init_state ¶
Initialize RMSProp caches for consequents and membership scalars.
Source code in anfis_toolbox/optim/rmsprop.py
train_step ¶
train_step(
model: ModelLike,
Xb: ndarray,
yb: ndarray,
state: dict[str, Any],
) -> tuple[float, dict[str, Any]]
One RMSProp step on a batch; returns (loss, updated_state).
Source code in anfis_toolbox/optim/rmsprop.py
anfis_toolbox.optim.pso.PSOTrainer
dataclass
¶
PSOTrainer(
swarm_size: int = 20,
inertia: float = 0.7,
cognitive: float = 1.5,
social: float = 1.5,
epochs: int = 100,
init_sigma: float = 0.1,
clamp_velocity: None | tuple[float, float] = None,
clamp_position: None | tuple[float, float] = None,
random_state: None | int = None,
verbose: bool = False,
loss: LossFunction | str | None = None,
)
Bases: BaseTrainer
Particle Swarm Optimization (PSO) trainer for ANFIS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
swarm_size
|
int
|
Number of particles. |
20
|
inertia
|
float
|
Inertia weight (w). |
0.7
|
cognitive
|
float
|
Cognitive coefficient (c1). |
1.5
|
social
|
float
|
Social coefficient (c2). |
1.5
|
epochs
|
int
|
Number of iterations of the swarm update. |
100
|
init_sigma
|
float
|
Std-dev for initializing particle positions around current params. |
0.1
|
clamp_velocity
|
None | tuple[float, float]
|
Optional (min, max) to clip velocities element-wise. |
None
|
clamp_position
|
None | tuple[float, float]
|
Optional (min, max) to clip positions element-wise. |
None
|
random_state
|
None | int
|
Seed for RNG to ensure determinism. |
None
|
verbose
|
bool
|
Unused here; kept for API parity. |
False
|
Notes
Optimizes the loss specified by loss (defaulting to mean squared error) by searching
directly in parameter space without gradients. With ANFISClassifier you can set
loss="cross_entropy" to optimize categorical cross-entropy on logits.
compute_loss ¶
Evaluate the swarm's current parameters on (X, y) without mutation.
init_state ¶
Initialize PSO swarm state and return as a dict.
Source code in anfis_toolbox/optim/pso.py
train_step ¶
train_step(
model: ModelLike,
Xb: ndarray,
yb: ndarray,
state: dict[str, Any],
) -> tuple[float, dict[str, Any]]
Perform one PSO iteration over the swarm on a batch and return (best_loss, state).