Firing Levels Layers

This module contains the firing level layers implemented in the toolbox (including the normalization layer). These layers compute the firing levels from the membership values produced by the fuzzification layer, emulating AND operations across the linguistic variables of the input features.

class neuro_fuzzy_toolbox.layers.firing_levels_layers.FiringLevelsLayer(*args: Any, **kwargs: Any)[source]

Bases: Module

Firing levels layer for a general Adaptive Neuro-Fuzzy Inference System (ANFIS).

Computes the firing level of each fuzzy rule by applying the T-norm (product) operator across the membership degrees of each input feature. Designed to handle a general ANFIS model where different input features may have different numbers of MFs.

__init__(mf_distribution)[source]

Initializes a new FiringLevelsLayer instance.

Parameters:

mf_distribution (list[int]) – Number of MFs for each input feature.

forward(membership_values)[source]

Forward pass of the firing levels layer.

Computes the firing level of each rule as the product of the membership degrees of the corresponding MFs across all input features.

Parameters:

membership_values (torch.Tensor) – Membership degrees of shape (batch_size, input_size, max_num_mfs), where max_num_mfs is the maximum number of MFs across all input features.

Returns:

Firing levels of shape (batch_size, num_mfs_1 * num_mfs_2 * ... * num_mfs_n), where the number of rules is the product of the number of MFs across all input features.

Return type:

torch.Tensor

class neuro_fuzzy_toolbox.layers.firing_levels_layers.NormalizationLayer(*args: Any, **kwargs: Any)[source]

Bases: Module

Normalization layer for an Adaptive Neuro-Fuzzy Inference System (ANFIS).

Normalizes the firing levels across all rules so that they sum to one, producing the normalized firing levels used by the consequent layer.

Optionally supports a default rule that adds an extra firing level to capture input combinations not covered by the reduced rule set.

Warning

The default rule functionality is experimental and not fully supported.

__init__(default_rule=False)[source]

Initializes a new NormalizationLayer instance.

Parameters:

default_rule (bool) – If True, the layer expects the last firing level in the input to correspond to the default rule. In this case, all firing levels (including the default rule’s) are summed for normalization, but the default rule’s firing level is excluded from the normalized output returned by forward(). If False, all firing levels are normalized and returned as-is. Defaults to False.

Warning

The default rule functionality is experimental and not fully supported.

forward(w)[source]

Forward pass of the normalization layer.

Normalizes the firing levels so that they sum to one across all rules. If a zero sum is encountered, it is replaced with one to avoid division by zero.

Parameters:

w (torch.Tensor) – Firing levels of shape (batch_size, num_rules).

Returns:

Normalized firing levels of shape (batch_size, num_rules), or (batch_size, num_rules - 1) if the default rule is enabled.

Return type:

torch.Tensor

class neuro_fuzzy_toolbox.layers.firing_levels_layers.h_FiringLevelsLayer(*args: Any, **kwargs: Any)[source]

Bases: Module

Firing levels layer for a homogeneous Adaptive Neuro-Fuzzy Inference System (ANFIS).

Computes the firing level of each fuzzy rule by applying the T-norm (product) operator across the membership degrees of each input feature. Assumes the same number of MFs for every input feature.

Supports an optional rule-reduced mode that avoids the full combinatorial expansion of membership degrees. In this mode, only the membership degrees at matching indices across features are multiplied together, yielding a number of rules equal to the number of MFs per feature rather than num_mfs ** input_size. For further details, see rule-reduced ANFIS.

__init__(rule_reduced=False)[source]

Initializes a new h_FiringLevelsLayer instance.

Parameters:

rule_reduced (bool) – If True, uses rule-reduced firing level computation instead of the full combinatorial expansion. Defaults to False.

forward(membership_values)[source]

Forward pass of the homogeneous firing levels layer.

Computes the firing level of each rule as the product of the membership degrees of the corresponding MFs across all input features.

Parameters:

membership_values (torch.Tensor) – Membership degrees of shape (batch_size, input_size, num_mfs).

Returns:

Firing levels of shape (batch_size, num_mfs ** input_size) in standard mode, or (batch_size, num_mfs) in rule-reduced mode.

Return type:

torch.Tensor

class neuro_fuzzy_toolbox.layers.firing_levels_layers.rule_reduced_FiringLevelsLayer(*args: Any, **kwargs: Any)[source]

Bases: Module

Firing levels layer for a rule-reduced Adaptive Neuro-Fuzzy Inference System (ANFIS).

Computes the firing level of each fuzzy rule using the rule-reduced approach, where only the membership degrees at matching indices across features are multiplied together. This avoids the full combinatorial expansion, yielding a number of rules equal to the number of MFs per feature. Assumes the same number of MFs for every input feature.

Optionally supports a default rule that adds an extra firing level to capture input combinations not covered by the reduced rule set.

Warning

The default rule functionality is experimental and not fully supported.

__init__(default_rule=False)[source]

Initializes a new rule_reduced_FiringLevelsLayer instance.

Parameters:

default_rule (bool) – If True, appends an extra firing level representing a default rule to capture input combinations not covered by the reduced rule set. Defaults to False.

Warning

The default_rule option is experimental and not fully supported.

forward(membership_values)[source]

Forward pass of the rule-reduced firing levels layer.

Computes the firing level of each rule by multiplying the membership degrees at matching indices across all input features.

Parameters:

membership_values (torch.Tensor) – Membership degrees of shape (batch_size, input_size, num_mfs).

Returns:

Firing levels of shape (batch_size, num_mfs), or (batch_size, num_mfs + 1) if the default rule is enabled.

Return type:

torch.Tensor