Layers¶
anfis_toolbox.layers.MembershipLayer ¶
Membership layer for ANFIS (Adaptive Neuro-Fuzzy Inference System).
This is the first layer of ANFIS that applies membership functions to input variables. Each input variable has multiple membership functions that transform crisp input values into fuzzy membership degrees.
This layer serves as the fuzzification stage, converting crisp inputs into fuzzy sets that can be processed by subsequent ANFIS layers.
Attributes:
| Name | Type | Description |
|---|---|---|
input_mfs |
dict
|
Dictionary mapping input names to lists of membership functions. |
input_names |
list
|
List of input variable names. |
n_inputs |
int
|
Number of input variables. |
mf_per_input |
list
|
Number of membership functions per input. |
last |
dict
|
Cache of last forward pass computations for backward pass. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_mfs
|
dict
|
Dictionary mapping input names to lists of membership functions. Format: {input_name: [MembershipFunction, ...]} |
required |
Source code in anfis_toolbox/layers.py
membership_functions
property
¶
Alias for input_mfs to provide a standardized interface.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, list[MembershipFunction]]
|
Dictionary mapping input names to lists of membership functions. |
backward ¶
Performs backward pass to compute gradients for membership functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gradients
|
dict
|
Dictionary mapping input names to gradient arrays. Format: {input_name: np.ndarray with shape (batch_size, n_mfs)} |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, dict[str, list[dict[str, float]]]]
|
Nested structure with parameter gradients mirroring |
Source code in anfis_toolbox/layers.py
forward ¶
Performs forward pass to compute membership degrees for all inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input data with shape (batch_size, n_inputs). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, ndarray]
|
Dictionary mapping input names to membership degree arrays. Format: {input_name: np.ndarray with shape (batch_size, n_mfs)} |
Source code in anfis_toolbox/layers.py
reset ¶
Resets all membership functions to their initial state.
Returns:
| Type | Description |
|---|---|
None
|
None |
anfis_toolbox.layers.RuleLayer ¶
RuleLayer(
input_names: list[str],
mf_per_input: list[int],
rules: Sequence[Sequence[int]] | None = None,
)
Rule layer for ANFIS (Adaptive Neuro-Fuzzy Inference System).
This layer computes the rule strengths (firing strengths) by applying the T-norm (typically product) operation to the membership degrees of all input variables for each rule.
This is the second layer of ANFIS that takes membership degrees from the MembershipLayer and computes rule activations.
Attributes:
| Name | Type | Description |
|---|---|---|
input_names |
list
|
List of input variable names. |
n_inputs |
int
|
Number of input variables. |
mf_per_input |
list
|
Number of membership functions per input. |
rules |
list
|
List of all possible rule combinations. |
last |
dict
|
Cache of last forward pass computations for backward pass. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_names
|
list
|
List of input variable names. |
required |
mf_per_input
|
list
|
Number of membership functions per input variable. |
required |
rules
|
Sequence[Sequence[int]] | None
|
Optional explicit rule set where each
rule is a sequence of membership-function indices, one per input. When
|
None
|
Source code in anfis_toolbox/layers.py
backward ¶
Performs backward pass to compute gradients for membership functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dL_dw
|
ndarray
|
Gradient of loss with respect to rule strengths. Shape: (batch_size, n_rules) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, ndarray]
|
Dictionary mapping input names to gradient arrays for membership functions. Format: {input_name: np.ndarray with shape (batch_size, n_mfs)} |
Source code in anfis_toolbox/layers.py
forward ¶
Performs forward pass to compute rule strengths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
membership_outputs
|
dict
|
Dictionary mapping input names to membership degree arrays. Format: {input_name: np.ndarray with shape (batch_size, n_mfs)} |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Rule strengths with shape (batch_size, n_rules). |
Source code in anfis_toolbox/layers.py
anfis_toolbox.layers.NormalizationLayer ¶
Normalization layer for ANFIS (Adaptive Neuro-Fuzzy Inference System).
This layer normalizes the rule strengths (firing strengths) to ensure they sum to 1.0 for each sample in the batch. This is a crucial step in ANFIS as it converts rule strengths to normalized rule weights.
The normalization formula is: norm_w_i = w_i / sum(w_j for all j)
Attributes:
| Name | Type | Description |
|---|---|---|
last |
dict
|
Cache of last forward pass computations for backward pass. |
Source code in anfis_toolbox/layers.py
backward ¶
Performs backward pass to compute gradients for original rule weights.
The gradient computation uses the quotient rule for derivatives: If norm_w_i = w_i / sum_w, then: - d(norm_w_i)/d(w_i) = (sum_w - w_i) / sum_w² - d(norm_w_i)/d(w_j) = -w_j / sum_w² for j ≠ i
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dL_dnorm_w
|
ndarray
|
Gradient of loss with respect to normalized weights. Shape: (batch_size, n_rules) |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Gradient of loss with respect to original weights. Shape: (batch_size, n_rules) |
Source code in anfis_toolbox/layers.py
forward ¶
Performs forward pass to normalize rule weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
w
|
ndarray
|
Rule strengths with shape (batch_size, n_rules). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Normalized rule weights with shape (batch_size, n_rules). Each row sums to 1.0. |
Source code in anfis_toolbox/layers.py
anfis_toolbox.layers.ConsequentLayer ¶
Consequent layer for ANFIS (Adaptive Neuro-Fuzzy Inference System).
This layer implements the consequent part of fuzzy rules in ANFIS. Each rule has a linear consequent function of the form: f_i(x) = p_i * x_1 + q_i * x_2 + ... + r_i (TSK model)
The final output is computed as a weighted sum: y = Σ(w_i * f_i(x)) where w_i are normalized rule weights
Attributes:
| Name | Type | Description |
|---|---|---|
n_rules |
int
|
Number of fuzzy rules. |
n_inputs |
int
|
Number of input variables. |
parameters |
ndarray
|
Linear parameters for each rule with shape (n_rules, n_inputs + 1). Each row contains [p_i, q_i, ..., r_i] for rule i. |
gradients |
ndarray
|
Accumulated gradients for parameters. |
last |
dict
|
Cache of last forward pass computations for backward pass. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_rules
|
int
|
Number of fuzzy rules. |
required |
n_inputs
|
int
|
Number of input variables. |
required |
Source code in anfis_toolbox/layers.py
backward ¶
Performs backward pass to compute gradients for parameters and inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dL_dy
|
ndarray
|
Gradient of loss with respect to layer output. Shape: (batch_size, 1) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[ndarray, ndarray]
|
(dL_dnorm_w, dL_dx) where: - dL_dnorm_w: Gradient w.r.t. normalized weights, shape (batch_size, n_rules) - dL_dx: Gradient w.r.t. input x, shape (batch_size, n_inputs) |
Source code in anfis_toolbox/layers.py
forward ¶
Performs forward pass to compute the final ANFIS output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input data with shape (batch_size, n_inputs). |
required |
norm_w
|
ndarray
|
Normalized rule weights with shape (batch_size, n_rules). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Final ANFIS output with shape (batch_size, 1). |
Source code in anfis_toolbox/layers.py
reset ¶
anfis_toolbox.layers.ClassificationConsequentLayer ¶
ClassificationConsequentLayer(
n_rules: int,
n_inputs: int,
n_classes: int,
random_state: int | None = None,
)
Consequent layer that produces per-class logits for classification.
Each rule i has a vector of class logits with a linear function of inputs: f_i(x) = W_i x + b_i, where W_i has shape (n_classes, n_inputs) and b_i (n_classes,). We store parameters as a single array of shape (n_rules, n_classes, n_inputs + 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_rules
|
int
|
Number of fuzzy rules in the layer. |
required |
n_inputs
|
int
|
Number of input features. |
required |
n_classes
|
int
|
Number of output classes. |
required |
random_state
|
int | None
|
Random seed for parameter initialization. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
n_rules |
int
|
Stores the number of fuzzy rules. |
n_inputs |
int
|
Stores the number of input features. |
n_classes |
int
|
Stores the number of output classes. |
parameters |
ndarray
|
Randomly initialized parameters for each rule, class, and input (including bias). |
gradients |
ndarray
|
Gradient values initialized to zeros, matching the shape of parameters. |
last |
dict
|
Dictionary for storing intermediate results or state. |
Source code in anfis_toolbox/layers.py
backward ¶
Computes the backward pass for the classification consequent layer.
Source code in anfis_toolbox/layers.py
forward ¶
Computes the forward pass for the classification consequent layer.