Base
Base TSK model that factors the common antecedent-defuzzification pipeline.
This module defines BaseTSK, the abstract foundation for all TSK fuzzy
models in highFIS. It factors out the shared antecedent pipeline,
defuzzifier, and training loop so concrete subclasses can focus on
task-specific consequent layers and loss criteria.
The forward pipeline executes four sequential steps:
highfis.layers.MembershipLayer— evaluates membership functions for each input feature.highfis.layers.RuleLayer— computes rule firing strengths via a configurable rule base and T-norm.- Defuzzifier — normalizes firing strengths to probability-like weights
(default:
highfis.defuzzifiers.SoftmaxLogDefuzzifier). - ConsequentLayer — produces the final output from the inputs and the normalized rule weights.
Concrete subclasses must implement:
BaseTSK._build_consequent_layer— return the task-specific consequent module.BaseTSK._default_criterion— return the default loss function.
Optional overridable hooks:
BaseTSK._compute_loss— customize target preparation or loss composition.BaseTSK._evaluate_validation— customize the validation metric used for early stopping.
BaseTSK
Bases: nn.Module
Abstract base for TSK fuzzy models.
Subclasses must implement :meth:_build_consequent_layer and
:meth:_default_criterion. Optionally override :meth:_compute_loss
and :meth:_evaluate_validation for task-specific logic.
Initialize the TSK pipeline layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_mfs
|
Mapping[str, Sequence[MembershipFunction]]
|
Mapping from feature names to sequences of
:class: |
required |
rule_base
|
str
|
Rule-base construction strategy. Supported values:
|
'cartesian'
|
t_norm
|
str | TNormFn
|
T-norm name or callable. Common string values: |
'gmean'
|
rules
|
Sequence[Sequence[int]] | None
|
Explicit rule index sequences. Required when
rule_base is |
None
|
defuzzifier
|
Defuzzifier | None
|
Normalization module applied to raw rule firing
strengths. Defaults to
:class: |
None
|
consequent_batch_norm
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If input_mfs is empty. |
Source code in highfis/base.py
fit
Train the model with optional early stopping.
When x_val and y_val are provided the model evaluates a
task-specific metric (via :meth:_evaluate_validation) after every
epoch and applies early stopping when the metric has not improved for
patience consecutive epochs.
By default the best model weights from validation are restored when
restore_best=True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Training features of shape |
required |
y
|
Tensor
|
Training targets of shape |
required |
epochs
|
int
|
Maximum number of training epochs. |
200
|
learning_rate
|
float
|
Learning rate for the default AdamW optimizer. |
0.001
|
criterion
|
Callable[[Tensor, Tensor], Tensor] | None
|
Optional loss function. Defaults to
:meth: |
None
|
optimizer
|
torch.optim.Optimizer | None
|
Optional pre-built optimizer. When |
None
|
batch_size
|
int | None
|
Mini-batch size. |
None
|
shuffle
|
bool
|
If |
True
|
ur_weight
|
float
|
Non-negative weight for the uniform rule
regularization term. |
0.0
|
ur_target
|
float | None
|
Target uniform activation for UR. Must be in
|
None
|
verbose
|
bool | int
|
Verbosity level. |
False
|
x_val
|
Tensor | None
|
Optional validation features of shape
|
None
|
y_val
|
Tensor | None
|
Optional validation targets of shape |
None
|
patience
|
int | None
|
Number of consecutive epochs without improvement
before early stopping. Set to |
20
|
restore_best
|
bool
|
If |
True
|
weight_decay
|
float
|
L2 weight decay applied to consequent parameters by the default AdamW optimizer. |
1e-08
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary with keys |
dict[str, Any]
|
containing per-epoch loss lists. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If shapes of x, y, x_val, or y_val are
incompatible, or if ur_weight < 0 or ur_target is
outside |
Source code in highfis/base.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 | |
forward
forward_antecedents
Compute normalized rule strengths from model antecedents.
get_consequent_weights
Return the consequent layer weights or None when unavailable.
get_mf_params
Return a serializable description of the model's membership functions.
Source code in highfis/base.py
get_rule_table
Return the rule base as a table of feature-to-MF indices.