ANFIS Models

This module contains the documentation for the ANFIS model classes implemented in the toolbox.

All classes below inherit from the base class base_ANFIS, which defines the general structure of ANFIS models and their shared methods (see base_ANFIS).

class neuro_fuzzy_toolbox.models.anfis.ANFIS(*args: Any, **kwargs: Any)[source]

Bases: base_ANFIS

Adaptive Neuro-Fuzzy Inference System (ANFIS) with an arbitrary number of membership functions per input feature.

__init__(mf_distribution, outputs=1, membership_function=torch.nn.Module, output_type='default', features=None, dtype=torch.float32)[source]

Initializes an ANFIS model.

Parameters:
  • mf_distribution (list[int]) – List containing the number of membership functions per input feature.

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

  • membership_function (MembershipFunction) – Membership function to use. Defaults to GeneralizedBell_MF.

  • output_type (str) – Output type of the model. Defaults to 'default'.

  • features (iterable) – Iterable of strings containing the names of the input features considered by the model. Must be of length input_size. Defaults to None.

  • dtype (torch.dtype) – Data type to use in the model. Defaults to torch.float32.

property classes

Returns the class labels that the model attempts to predict.

Returns:

Tensor containing the class labels that the model attempts to predict.

Return type:

torch.Tensor

forward(x, return_probs=False)

Forward pass through the model.

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

  • return_probs (bool) – If True, the output is passed through a Softmax function to obtain class probabilities. Only applies when the model’s output type is 'softmax'; ignored otherwise. Defaults to False.

get_all_consequents_outputs(x, weighted=True)

Returns the individual rule outputs of the model for the given input data.

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

  • weighted (bool) – If True, the rule outputs are weighted by their corresponding firing levels. Defaults to True.

Returns:

Individual rule outputs of shape (outputs, batch_size, num_rules).

Return type:

torch.Tensor

get_consequents()

Returns the consequent parameters of the model.

Returns:

Tensor containing the consequent parameters of shape (outputs, rules, input_size + 1).

Return type:

torch.Tensor

get_consequents_as_parameters_list()

Returns the consequent parameters of the model as a list of parameters. This is useful for optimization algorithms (using PyTorch optimizers).

Returns:

A list containing a single element (nn.Parameter) with the consequent parameters.

Return type:

list[nn.Parameter]

get_consequents_structure()

Returns the structure of the consequent parameters.

Returns:

A list of pandas DataFrames containing the structure of the consequent parameters.

Return type:

list[pandas.DataFrame]

get_firing_levels(x, normalized=False)

Returns the firing levels of the model for the given input data.

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

  • normalized (bool) – If True, returns the normalized firing levels. Defaults to False.

Returns:

Firing levels of shape (batch_size, num_rules).

Return type:

torch.Tensor

get_premises()[source]

Returns the premise parameters of the model.

Returns:

List of tensors containing the premise parameters associated with each input feature, so the list length equals input_size. Each tensor has shape (num_mfs, mf_params), where num_mfs is the number of membership functions for the corresponding feature and mf_params is the number of parameters of the membership function used.

Return type:

list[torch.Tensor]

get_premises_as_parameters_list()[source]

Returns the premise parameters of the model as a list of parameters. This is useful for optimization algorithms (using PyTorch optimizers).

Returns:

A ParameterList containing the premise parameters for each input feature.

Return type:

nn.ParameterList

get_premises_structure()

Returns the structure of the premise parameters.

Returns:

DataFrame containing the structure of the premise parameters.

Return type:

pandas.DataFrame

get_rules_structure()[source]

Returns a combined summary of the premises and consequent parameters for each rule in the model.

The resulting DataFrame organizes each rule as a row, with columns grouped first by premises (showing the membership function parameters of each input feature) and then by the consequent parameters of each model output.

Returns:

DataFrame with a MultiIndex column structure, where the top-level groups correspond to 'premises' and 'output i consequents' for each output i, and rows correspond to individual rules.

Return type:

pandas.DataFrame

init_consequents(x, y, driver=None, ridge_lambda=0.0)

Initializes the consequent parameters of the model using a least-squares estimate.

Note

The backend method used for the least-squares estimation is specified by the driver parameter. For more information, see: https://pytorch.org/docs/stable/generated/torch.linalg.lstsq.html.

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

  • y (torch.Tensor) – Output data tensor of shape (batch_size, outputs).

  • driver (str) – Backend function to use for the least-squares estimation. Valid values are 'gels', 'gelsy', 'gelsd', and 'gelss'. If None, defaults to 'gels'.

  • ridge_lambda (float) – Lambda value for Ridge regularization in the least-squares estimation. If 0., no regularization is applied. Defaults to 0..

Important

If the model has output_type='softmax', the class labels in y are expected to be integers representing the target classes, and a one-hot encoding of these labels will be performed internally for the least-squares estimation. If the labels are not of the form [0, 1, 2, ...], the model will automatically adjust to the labels present in y and set them as the classes it will attempt to predict. This is useful when users prefer to work with custom class labels directly. The target class labels can also be set manually using set_custom_classes_ids().

init_premises(x)

Initializes the premise parameters of the model’s fuzzification layer from the provided data.

Parameters:

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

load_state_dict(state_dict)[source]

Loads a model state dictionary.

Parameters:

state_dict (dict) – Dictionary containing the model state.

property num_mfs

Returns the number of membership functions per input feature.

Returns:

Tensor containing the number of membership functions for each input feature.

Return type:

torch.Tensor

property outputs

Returns the number of outputs of the model.

Returns:

Number of outputs.

Return type:

int

plot_premises(mf=None, input_dim=None, group_by_dim=False, linestyles='-', linewidths=2.5)

Plots the membership functions of the model’s premises.

Parameters:
  • mf (int) – Index of the membership function to plot. If None, all membership functions are plotted. Defaults to None.

  • input_dim (int) – Input feature dimension to plot. If None, all dimensions are plotted. Defaults to None.

  • group_by_dim (bool) – If True, groups all membership functions into a single plot per input dimension. Defaults to False.

  • linestyles (str | list[str]) – A string or list of strings specifying the line styles used to represent the membership functions. If a list is provided, the styles are applied cyclically. Valid values are: '-', '--', '-.', ':'. Defaults to '-'.

  • linewidths (float) – Line width used to plot the membership functions. Defaults to 2.5.

predict(x)

Runs inference on the input data and adjusts the output to the expected format based on the model’s output type.

Parameters:

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

Returns:

Model predictions.

Return type:

torch.Tensor

property rules

Returns the number of rules in the ANFIS model.

Returns:

Number of rules.

Return type:

int

set_consequents(consequents)

Sets the consequent parameters of the model.

Parameters:

consequents (torch.Tensor) – Tensor containing the consequent parameters of shape (outputs, rules, input_size + 1).

set_custom_classes_ids(new_classes_ids)

Sets custom labels for the classes that the model attempts to predict.

Note

By default, for a problem with C classes, the model uses labels of the form [0, 1, ..., C-1]. This method allows setting custom class labels, which will always be stored in ascending order.

Important

Only applicable when the model has output_type='softmax'.

Parameters:

new_classes_ids (list[int]) – List containing the new class labels.

set_premises(premises)[source]

Sets the membership function parameters of the model’s fuzzification layer.

Parameters:

premises (list[torch.Tensor]) – List of tensors containing the premise parameters. Each tensor must have shape (num_mfs, mf_params), where num_mfs is the number of membership functions for the corresponding feature and mf_params is the number of parameters of the membership function used.

class neuro_fuzzy_toolbox.models.anfis.h_ANFIS(*args: Any, **kwargs: Any)[source]

Bases: base_ANFIS

Homogeneous Adaptive Neuro-Fuzzy Inference System (ANFIS).

Implements an ANFIS model where every input feature shares the same number of membership functions, restricting each feature to the same number of linguistic variables.

Supports an optional rule-reduced mode that avoids the full combinatorial expansion when computing firing levels. 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 membership functions per feature rather than num_mfs ** input_size. For further details, see rule-reduced ANFIS.

__init__(input_size, num_mfs, outputs=1, membership_function=torch.nn.Module, output_type='default', rule_reduced=False, features=None, dtype=torch.float32)[source]

Initializes a homogeneous ANFIS model.

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

  • num_mfs (int) – Number of membership functions per input feature.

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

  • membership_function (MembershipFunction) – Membership function to use. Defaults to GeneralizedBell_MF.

  • output_type (str) – Output type of the model. Defaults to 'default'.

  • rule_reduced (bool) – If True, instantiates a rule-reduced ANFIS model. Defaults to False.

  • features (iterable) – Iterable of strings containing the names of the input features considered by the model. Must be of length input_size. Defaults to None.

  • dtype (torch.dtype) – Data type to use in the model. Defaults to torch.float32.

property classes

Returns the class labels that the model attempts to predict.

Returns:

Tensor containing the class labels that the model attempts to predict.

Return type:

torch.Tensor

forward(x, return_probs=False)

Forward pass through the model.

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

  • return_probs (bool) – If True, the output is passed through a Softmax function to obtain class probabilities. Only applies when the model’s output type is 'softmax'; ignored otherwise. Defaults to False.

get_all_consequents_outputs(x, weighted=True)

Returns the individual rule outputs of the model for the given input data.

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

  • weighted (bool) – If True, the rule outputs are weighted by their corresponding firing levels. Defaults to True.

Returns:

Individual rule outputs of shape (outputs, batch_size, num_rules).

Return type:

torch.Tensor

get_consequents()

Returns the consequent parameters of the model.

Returns:

Tensor containing the consequent parameters of shape (outputs, rules, input_size + 1).

Return type:

torch.Tensor

get_consequents_as_parameters_list()

Returns the consequent parameters of the model as a list of parameters. This is useful for optimization algorithms (using PyTorch optimizers).

Returns:

A list containing a single element (nn.Parameter) with the consequent parameters.

Return type:

list[nn.Parameter]

get_consequents_structure()

Returns the structure of the consequent parameters.

Returns:

A list of pandas DataFrames containing the structure of the consequent parameters.

Return type:

list[pandas.DataFrame]

get_firing_levels(x, normalized=False)

Returns the firing levels of the model for the given input data.

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

  • normalized (bool) – If True, returns the normalized firing levels. Defaults to False.

Returns:

Firing levels of shape (batch_size, num_rules).

Return type:

torch.Tensor

get_premises()

Returns the premise parameters of the model.

Returns:

Tensor containing the premise parameters of shape (input_size, num_mfs, mf_params).

Return type:

torch.Tensor

get_premises_as_parameters_list()

Returns the premise parameters of the model as a list of parameters. This is useful for optimization algorithms (using PyTorch optimizers).

Returns:

A list containing a single element (nn.Parameter) with the premise parameters.

Return type:

list[nn.Parameter]

get_premises_structure()

Returns the structure of the premise parameters.

Returns:

DataFrame containing the structure of the premise parameters.

Return type:

pandas.DataFrame

get_rules_structure()[source]

Returns a combined summary of the premises and consequent parameters for each rule in the model.

The resulting DataFrame organizes each rule as a row, with columns grouped first by premises (showing the membership function parameters of each input feature) and then by the consequent parameters of each model output.

Returns:

DataFrame with a MultiIndex column structure, where the top-level groups correspond to 'premises' and 'output i consequents' for each output i, and rows correspond to individual rules.

Return type:

pandas.DataFrame

init_consequents(x, y, driver=None, ridge_lambda=0.0)

Initializes the consequent parameters of the model using a least-squares estimate.

Note

The backend method used for the least-squares estimation is specified by the driver parameter. For more information, see: https://pytorch.org/docs/stable/generated/torch.linalg.lstsq.html.

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

  • y (torch.Tensor) – Output data tensor of shape (batch_size, outputs).

  • driver (str) – Backend function to use for the least-squares estimation. Valid values are 'gels', 'gelsy', 'gelsd', and 'gelss'. If None, defaults to 'gels'.

  • ridge_lambda (float) – Lambda value for Ridge regularization in the least-squares estimation. If 0., no regularization is applied. Defaults to 0..

Important

If the model has output_type='softmax', the class labels in y are expected to be integers representing the target classes, and a one-hot encoding of these labels will be performed internally for the least-squares estimation. If the labels are not of the form [0, 1, 2, ...], the model will automatically adjust to the labels present in y and set them as the classes it will attempt to predict. This is useful when users prefer to work with custom class labels directly. The target class labels can also be set manually using set_custom_classes_ids().

init_premises(x)

Initializes the premise parameters of the model’s fuzzification layer from the provided data.

Parameters:

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

load_state_dict(state_dict)[source]

Loads a model state dictionary.

Parameters:

state_dict (dict) – Dictionary containing the model state.

property num_mfs

Returns the number of membership functions per input feature.

Returns:

Number of membership functions per input feature.

Return type:

int

property outputs

Returns the number of outputs of the model.

Returns:

Number of outputs.

Return type:

int

plot_premises(mf=None, input_dim=None, group_by_dim=False, linestyles='-', linewidths=2.5)

Plots the membership functions of the model’s premises.

Parameters:
  • mf (int) – Index of the membership function to plot. If None, all membership functions are plotted. Defaults to None.

  • input_dim (int) – Input feature dimension to plot. If None, all dimensions are plotted. Defaults to None.

  • group_by_dim (bool) – If True, groups all membership functions into a single plot per input dimension. Defaults to False.

  • linestyles (str | list[str]) – A string or list of strings specifying the line styles used to represent the membership functions. If a list is provided, the styles are applied cyclically. Valid values are: '-', '--', '-.', ':'. Defaults to '-'.

  • linewidths (float) – Line width used to plot the membership functions. Defaults to 2.5.

predict(x)

Runs inference on the input data and adjusts the output to the expected format based on the model’s output type.

Parameters:

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

Returns:

Model predictions.

Return type:

torch.Tensor

property rules

Returns the number of rules in the ANFIS model.

Returns:

Number of rules.

Return type:

int

set_consequents(consequents)

Sets the consequent parameters of the model.

Parameters:

consequents (torch.Tensor) – Tensor containing the consequent parameters of shape (outputs, rules, input_size + 1).

set_custom_classes_ids(new_classes_ids)

Sets custom labels for the classes that the model attempts to predict.

Note

By default, for a problem with C classes, the model uses labels of the form [0, 1, ..., C-1]. This method allows setting custom class labels, which will always be stored in ascending order.

Important

Only applicable when the model has output_type='softmax'.

Parameters:

new_classes_ids (list[int]) – List containing the new class labels.

set_premises(premises)

Sets the membership function parameters of the model’s fuzzification layer.

Parameters:

premises (torch.Tensor) – Tensor containing the premise parameters of shape (input_size, num_mfs, mf_params), where mf_params is the number of parameters of the membership function.

class neuro_fuzzy_toolbox.models.anfis.rule_reduced_ANFIS(*args: Any, **kwargs: Any)[source]

Bases: base_ANFIS

Rule-reduced Adaptive Neuro-Fuzzy Inference System (ANFIS).

Implements a homogeneous ANFIS model where every input feature shares the same number of membership functions, restricting each feature to the same number of linguistic variables. Unlike the standard ANFIS, the number of rules is reduced by avoiding the full combinatorial expansion when computing firing levels. Instead, only the membership degrees at matching indices across features are multiplied together, yielding a number of rules equal to the number of membership functions per feature. For further details, see rule-reduced ANFIS.

Note

Includes an experimental default_rule parameter that appends an extra firing level to capture input combinations not covered by the reduced rule set. This functionality is not fully supported across all toolbox operations and is subject to change in future versions.

Warning

The use of default_rule=True is experimental, is not supported in all toolbox operations, and may produce unexpected behavior.

__init__(input_size, num_mfs, outputs=1, default_rule=False, membership_function=torch.nn.Module, output_type='default', features=None, dtype=torch.float32)[source]

Initializes a rule-reduced ANFIS model.

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

  • num_mfs (int) – Number of membership functions per input feature.

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

  • 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.

  • membership_function (MembershipFunction) – Membership function to use. Defaults to GeneralizedBell_MF.

  • output_type (str) – Output type of the model. Defaults to 'default'.

  • features (iterable) – Iterable of strings containing the names of the input features considered by the model. Must be of length input_size. Defaults to None.

  • dtype (torch.dtype) – Data type to use in the model. Defaults to torch.float32.

add_rules(means, stds)[source]

Adds new rules to the rule-reduced ANFIS model by generating new membership functions from the provided means and standard deviations. The consequent parameters for the new rules are initialized randomly.

Note

This method is agnostic to the membership function type used. Each membership function has a specific transformation function to convert the provided means and standard deviations into the corresponding membership function parameters.

Parameters:
  • means (torch.Tensor) – Tensor containing the means for generating the new rules, of shape (num_new_rules, input_size), where num_new_rules is the number of rules to add and input_size is the number of input features of the model.

  • stds (torch.Tensor) – Tensor containing the standard deviations for generating the new rules, of shape (num_new_rules, input_size), where num_new_rules is the number of rules to add and input_size is the number of input features of the model.

property classes

Returns the class labels that the model attempts to predict.

Returns:

Tensor containing the class labels that the model attempts to predict.

Return type:

torch.Tensor

forward(x, return_probs=False)

Forward pass through the model.

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

  • return_probs (bool) – If True, the output is passed through a Softmax function to obtain class probabilities. Only applies when the model’s output type is 'softmax'; ignored otherwise. Defaults to False.

get_all_consequents_outputs(x, weighted=True)

Returns the individual rule outputs of the model for the given input data.

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

  • weighted (bool) – If True, the rule outputs are weighted by their corresponding firing levels. Defaults to True.

Returns:

Individual rule outputs of shape (outputs, batch_size, num_rules).

Return type:

torch.Tensor

get_consequents()

Returns the consequent parameters of the model.

Returns:

Tensor containing the consequent parameters of shape (outputs, rules, input_size + 1).

Return type:

torch.Tensor

get_consequents_as_parameters_list()

Returns the consequent parameters of the model as a list of parameters. This is useful for optimization algorithms (using PyTorch optimizers).

Returns:

A list containing a single element (nn.Parameter) with the consequent parameters.

Return type:

list[nn.Parameter]

get_consequents_structure()

Returns the structure of the consequent parameters.

Returns:

A list of pandas DataFrames containing the structure of the consequent parameters.

Return type:

list[pandas.DataFrame]

get_firing_levels(x, normalized=False)

Returns the firing levels of the model for the given input data.

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

  • normalized (bool) – If True, returns the normalized firing levels. Defaults to False.

Returns:

Firing levels of shape (batch_size, num_rules).

Return type:

torch.Tensor

get_premises()

Returns the premise parameters of the model.

Returns:

Tensor containing the premise parameters of shape (input_size, num_mfs, mf_params).

Return type:

torch.Tensor

get_premises_as_parameters_list()[source]

Returns the premise parameters of the model as a list of parameters. This is useful for optimization algorithms.

Returns:

A ParameterList containing the premise parameters of the membership functions.

Return type:

nn.ParameterList

get_premises_structure()

Returns the structure of the premise parameters.

Returns:

DataFrame containing the structure of the premise parameters.

Return type:

pandas.DataFrame

get_rules_structure()[source]

Returns a combined summary of the premises and consequent parameters for each rule in the model.

The resulting DataFrame organizes each rule as a row, with columns grouped first by premises (showing the membership function parameters of each input feature) and then by the consequent parameters of each model output. Since this is a rule-reduced model, the number of rows equals the number of membership functions per feature.

Returns:

DataFrame with a MultiIndex column structure, where the top-level groups correspond to 'premises' and 'output i consequents' for each output i, and rows correspond to individual rules.

Return type:

pandas.DataFrame

init_consequents(x, y, driver=None, ridge_lambda=0.0)

Initializes the consequent parameters of the model using a least-squares estimate.

Note

The backend method used for the least-squares estimation is specified by the driver parameter. For more information, see: https://pytorch.org/docs/stable/generated/torch.linalg.lstsq.html.

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

  • y (torch.Tensor) – Output data tensor of shape (batch_size, outputs).

  • driver (str) – Backend function to use for the least-squares estimation. Valid values are 'gels', 'gelsy', 'gelsd', and 'gelss'. If None, defaults to 'gels'.

  • ridge_lambda (float) – Lambda value for Ridge regularization in the least-squares estimation. If 0., no regularization is applied. Defaults to 0..

Important

If the model has output_type='softmax', the class labels in y are expected to be integers representing the target classes, and a one-hot encoding of these labels will be performed internally for the least-squares estimation. If the labels are not of the form [0, 1, 2, ...], the model will automatically adjust to the labels present in y and set them as the classes it will attempt to predict. This is useful when users prefer to work with custom class labels directly. The target class labels can also be set manually using set_custom_classes_ids().

init_premises(x)

Initializes the premise parameters of the model’s fuzzification layer from the provided data.

Parameters:

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

load_state_dict(state_dict)[source]

Loads a model state dictionary.

Parameters:

state_dict (dict) – Dictionary containing the model state.

property num_mfs

Returns the number of membership functions per input feature.

Returns:

Number of membership functions per input feature.

Return type:

int

property outputs

Returns the number of outputs of the model.

Returns:

Number of outputs.

Return type:

int

plot_premises(mf=None, input_dim=None, group_by_dim=False, linestyles='-', linewidths=2.5)

Plots the membership functions of the model’s premises.

Parameters:
  • mf (int) – Index of the membership function to plot. If None, all membership functions are plotted. Defaults to None.

  • input_dim (int) – Input feature dimension to plot. If None, all dimensions are plotted. Defaults to None.

  • group_by_dim (bool) – If True, groups all membership functions into a single plot per input dimension. Defaults to False.

  • linestyles (str | list[str]) – A string or list of strings specifying the line styles used to represent the membership functions. If a list is provided, the styles are applied cyclically. Valid values are: '-', '--', '-.', ':'. Defaults to '-'.

  • linewidths (float) – Line width used to plot the membership functions. Defaults to 2.5.

predict(x)

Runs inference on the input data and adjusts the output to the expected format based on the model’s output type.

Parameters:

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

Returns:

Model predictions.

Return type:

torch.Tensor

remove_rules(rules_idxs)[source]

Removes rules from the rule-reduced ANFIS model at the specified indices.

Parameters:

rules_idxs (list[int]) – List of indices of the rules to remove. Each index must be an integer between 0 and num_rules - 1, where num_rules is the current number of rules in the model.

property rules

Returns the number of rules in the ANFIS model.

Returns:

Number of rules.

Return type:

int

set_consequents(consequents)

Sets the consequent parameters of the model.

Parameters:

consequents (torch.Tensor) – Tensor containing the consequent parameters of shape (outputs, rules, input_size + 1).

set_custom_classes_ids(new_classes_ids)

Sets custom labels for the classes that the model attempts to predict.

Note

By default, for a problem with C classes, the model uses labels of the form [0, 1, ..., C-1]. This method allows setting custom class labels, which will always be stored in ascending order.

Important

Only applicable when the model has output_type='softmax'.

Parameters:

new_classes_ids (list[int]) – List containing the new class labels.

set_premises(premises)

Sets the membership function parameters of the model’s fuzzification layer.

Parameters:

premises (torch.Tensor) – Tensor containing the premise parameters of shape (input_size, num_mfs, mf_params), where mf_params is the number of parameters of the membership function.

class neuro_fuzzy_toolbox.models.anfis.base_ANFIS(*args: Any, **kwargs: Any)[source]

Bases: Module

Base class for an Adaptive Neuro-Fuzzy Inference System (ANFIS).

Contains the common methods and attributes shared across the ANFIS model variants implemented in this toolbox. The ANFIS, h_ANFIS and rule_reduced_ANFIS classes inherit from this class and extend it with variant-specific functionality.

Warning

This class should not be instantiated directly.

property classes

Returns the class labels that the model attempts to predict.

Returns:

Tensor containing the class labels that the model attempts to predict.

Return type:

torch.Tensor

forward(x, return_probs=False)[source]

Forward pass through the model.

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

  • return_probs (bool) – If True, the output is passed through a Softmax function to obtain class probabilities. Only applies when the model’s output type is 'softmax'; ignored otherwise. Defaults to False.

get_all_consequents_outputs(x, weighted=True)[source]

Returns the individual rule outputs of the model for the given input data.

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

  • weighted (bool) – If True, the rule outputs are weighted by their corresponding firing levels. Defaults to True.

Returns:

Individual rule outputs of shape (outputs, batch_size, num_rules).

Return type:

torch.Tensor

get_consequents()[source]

Returns the consequent parameters of the model.

Returns:

Tensor containing the consequent parameters of shape (outputs, rules, input_size + 1).

Return type:

torch.Tensor

get_consequents_as_parameters_list()[source]

Returns the consequent parameters of the model as a list of parameters. This is useful for optimization algorithms (using PyTorch optimizers).

Returns:

A list containing a single element (nn.Parameter) with the consequent parameters.

Return type:

list[nn.Parameter]

get_consequents_structure()[source]

Returns the structure of the consequent parameters.

Returns:

A list of pandas DataFrames containing the structure of the consequent parameters.

Return type:

list[pandas.DataFrame]

get_firing_levels(x, normalized=False)[source]

Returns the firing levels of the model for the given input data.

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

  • normalized (bool) – If True, returns the normalized firing levels. Defaults to False.

Returns:

Firing levels of shape (batch_size, num_rules).

Return type:

torch.Tensor

get_premises()[source]

Returns the premise parameters of the model.

Returns:

Tensor containing the premise parameters of shape (input_size, num_mfs, mf_params).

Return type:

torch.Tensor

get_premises_as_parameters_list()[source]

Returns the premise parameters of the model as a list of parameters. This is useful for optimization algorithms (using PyTorch optimizers).

Returns:

A list containing a single element (nn.Parameter) with the premise parameters.

Return type:

list[nn.Parameter]

get_premises_structure()[source]

Returns the structure of the premise parameters.

Returns:

DataFrame containing the structure of the premise parameters.

Return type:

pandas.DataFrame

init_consequents(x, y, driver=None, ridge_lambda=0.0)[source]

Initializes the consequent parameters of the model using a least-squares estimate.

Note

The backend method used for the least-squares estimation is specified by the driver parameter. For more information, see: https://pytorch.org/docs/stable/generated/torch.linalg.lstsq.html.

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

  • y (torch.Tensor) – Output data tensor of shape (batch_size, outputs).

  • driver (str) – Backend function to use for the least-squares estimation. Valid values are 'gels', 'gelsy', 'gelsd', and 'gelss'. If None, defaults to 'gels'.

  • ridge_lambda (float) – Lambda value for Ridge regularization in the least-squares estimation. If 0., no regularization is applied. Defaults to 0..

Important

If the model has output_type='softmax', the class labels in y are expected to be integers representing the target classes, and a one-hot encoding of these labels will be performed internally for the least-squares estimation. If the labels are not of the form [0, 1, 2, ...], the model will automatically adjust to the labels present in y and set them as the classes it will attempt to predict. This is useful when users prefer to work with custom class labels directly. The target class labels can also be set manually using set_custom_classes_ids().

init_premises(x)[source]

Initializes the premise parameters of the model’s fuzzification layer from the provided data.

Parameters:

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

property num_mfs

Returns the number of membership functions per input feature.

Returns:

Number of membership functions per input feature.

Return type:

int

property outputs

Returns the number of outputs of the model.

Returns:

Number of outputs.

Return type:

int

plot_premises(mf=None, input_dim=None, group_by_dim=False, linestyles='-', linewidths=2.5)[source]

Plots the membership functions of the model’s premises.

Parameters:
  • mf (int) – Index of the membership function to plot. If None, all membership functions are plotted. Defaults to None.

  • input_dim (int) – Input feature dimension to plot. If None, all dimensions are plotted. Defaults to None.

  • group_by_dim (bool) – If True, groups all membership functions into a single plot per input dimension. Defaults to False.

  • linestyles (str | list[str]) – A string or list of strings specifying the line styles used to represent the membership functions. If a list is provided, the styles are applied cyclically. Valid values are: '-', '--', '-.', ':'. Defaults to '-'.

  • linewidths (float) – Line width used to plot the membership functions. Defaults to 2.5.

predict(x)[source]

Runs inference on the input data and adjusts the output to the expected format based on the model’s output type.

Parameters:

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

Returns:

Model predictions.

Return type:

torch.Tensor

property rules

Returns the number of rules in the ANFIS model.

Returns:

Number of rules.

Return type:

int

set_consequents(consequents)[source]

Sets the consequent parameters of the model.

Parameters:

consequents (torch.Tensor) – Tensor containing the consequent parameters of shape (outputs, rules, input_size + 1).

set_custom_classes_ids(new_classes_ids)[source]

Sets custom labels for the classes that the model attempts to predict.

Note

By default, for a problem with C classes, the model uses labels of the form [0, 1, ..., C-1]. This method allows setting custom class labels, which will always be stored in ascending order.

Important

Only applicable when the model has output_type='softmax'.

Parameters:

new_classes_ids (list[int]) – List containing the new class labels.

set_premises(premises)[source]

Sets the membership function parameters of the model’s fuzzification layer.

Parameters:

premises (torch.Tensor) – Tensor containing the premise parameters of shape (input_size, num_mfs, mf_params), where mf_params is the number of parameters of the membership function.