Consequent Layer

This module contains the consequent layer implemented in the toolbox. This layer computes the output value of each rule using the normalized firing levels produced by the preceding layers.

class neuro_fuzzy_toolbox.layers.consequent_layer.ConsequentLayer(*args: Any, **kwargs: Any)[source]

Bases: Module

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

Computes the weighted output of each fuzzy rule using a linear consequent function. Consequent parameters are stored as a single trainable tensor.

__init__(input_size, rules, outputs=1, features=None, dtype=torch.float32)[source]

Initializes a new ConsequentLayer instance.

Parameters:
  • input_size (int) – Number of input features of the ANFIS model.

  • rules (int) – Number of fuzzy rules in the ANFIS model.

  • outputs (int) – Number of model outputs. Defaults to 1.

  • features (iterable, optional) – Names of the input features as strings, of length input_size. Defaults to None.

  • dtype (torch.dtype) – Data type for the layer parameters. Defaults to torch.float32.

forward(x, weights)[source]

Forward pass of the consequent layer.

Computes the weighted output of each rule using the normalized firing levels as weights.

Parameters:
  • x (torch.Tensor) – Input features of shape (batch_size, input_size).

  • weights (torch.Tensor) – Normalized firing levels of shape (batch_size, rules).

Returns:

Weighted rule outputs of shape (outputs, batch_size, rules).

Return type:

torch.Tensor

get_consequents()[source]

Returns the current consequent parameters.

Returns:

Consequent parameter tensor of shape (outputs, rules, input_size + 1).

Return type:

torch.Tensor

get_consequents_as_parameters_list()[source]

Returns the consequent parameters as a single-element list of trainable parameters, useful for passing to optimizers.

Returns:

List containing a single nn.Parameter with all consequent parameters.

Return type:

list[nn.Parameter]

get_consequents_outputs(x)[source]

Returns the individual rule outputs without weighting by normalized firing levels.

Parameters:

x (torch.Tensor) – Input features of shape (batch_size, input_size).

Returns:

Unweighted rule outputs of shape (outputs, batch_size, rules).

Return type:

torch.Tensor

property get_consequents_structure

Structure of the consequent parameters.

Returns:

List of DataFrames, one per model output, each describing the consequent parameters for every rule and input feature.

Return type:

list[pd.DataFrame]

set_consequents(consequents)[source]

Sets the consequent parameters.

Parameters:

consequents (torch.Tensor) – Consequent parameter tensor of shape (outputs, rules, input_size + 1).

class neuro_fuzzy_toolbox.layers.consequent_layer.alt_ConsequentLayer(*args: Any, **kwargs: Any)[source]

Bases: Module

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

Functionally equivalent to ConsequentLayer, but stores the consequent parameters as a nn.ParameterList of per-rule tensors rather than a single tensor. This representation is required by optimizers and training algorithms that add or remove rules dynamically during structural adaptation.

__init__(input_size, rules, outputs=1, features=None, dtype=torch.float32)[source]

Initializes a new alt_ConsequentLayer instance.

Parameters:
  • input_size (int) – Number of input features of the ANFIS model.

  • rules (int) – Number of fuzzy rules in the ANFIS model.

  • outputs (int) – Number of model outputs. Defaults to 1.

  • features (iterable, optional) – Names of the input features as strings, of length input_size. Defaults to None.

  • dtype (torch.dtype) – Data type for the layer parameters. Defaults to torch.float32.

forward(x, weights)[source]

Forward pass of the alternative consequent layer.

Computes the weighted output of each rule using the normalized firing levels as weights.

Parameters:
  • x (torch.Tensor) – Input features of shape (batch_size, input_size).

  • weights (torch.Tensor) – Normalized firing levels of shape (batch_size, rules).

Returns:

Weighted rule outputs of shape (outputs, batch_size, rules).

Return type:

torch.Tensor

get_consequents()[source]

Returns the current consequent parameters as a single tensor.

Returns:

Consequent parameter tensor of shape (outputs, rules, input_size + 1).

Return type:

torch.Tensor

get_consequents_as_parameters_list()[source]

Returns the consequent parameters as a PyTorch ParameterList, useful for passing to optimizers.

Returns:

List of per-rule consequent parameters.

Return type:

nn.ParameterList

get_consequents_outputs(x)[source]

Returns the individual rule outputs without weighting by normalized firing levels.

Parameters:

x (torch.Tensor) – Input features of shape (batch_size, input_size).

Returns:

Unweighted rule outputs of shape (outputs, batch_size, rules).

Return type:

torch.Tensor

property get_consequents_structure

Structure of the consequent parameters.

Returns:

List of DataFrames, one per model output, each describing the consequent parameters for every rule and input feature.

Return type:

list[pd.DataFrame]

set_consequents(consequents)[source]

Sets the consequent parameters from a single tensor, converting each rule’s parameters into a separate trainable entry in the internal nn.ParameterList.

Parameters:

consequents (torch.Tensor) – Consequent parameter tensor of shape (outputs, rules, input_size + 1).