Fuzzification Layers

This module contains the fuzzification layers implemented in the toolbox. These layers transform the model inputs into membership values with respect to the defined fuzzy sets.

class neuro_fuzzy_toolbox.layers.fuzzification_layer.FuzzificationLayer(*args: Any, **kwargs: Any)[source]

Bases: Module

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

Transforms input data into membership degrees for each MF of each input feature. Designed to handle a general ANFIS model where different input features may have different numbers of MFs.

__init__(mf_distribution, membership_function=torch.nn.Module, features=None, dtype=torch.float32)[source]

Initializes a new FuzzificationLayer instance.

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

  • membership_function (MembershipFunction) – MF type to use. Defaults to GeneralizedBell_MF().

  • 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)[source]

Forward pass of the fuzzification layer.

Computes the membership degrees for each input feature.

Parameters:

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

Returns:

Membership degrees of shape (batch_size, input_size, max_n_mfs), where max_n_mfs is the maximum number of MFs across all input features.

Return type:

torch.Tensor

Note

Features with fewer MFs than max_n_mfs are zero-padded to match the output shape. This padding is applied during the forward pass and may be improved in future versions by embedding it directly into the premise parameter tensors at instantiation time.

get_premises()[source]

Returns the current premise parameters.

Returns:

List of premise parameter tensors, one per input feature.

Return type:

list[torch.Tensor]

get_premises_as_parameters_list()[source]

Returns the premise parameters as a PyTorch ParameterList.

Returns:

Premise parameters wrapped as a list of trainable parameters.

Return type:

nn.ParameterList

property get_premises_structure

Structure of the premise parameters.

Returns:

DataFrame describing the premise parameters for each input feature and MF.

Return type:

pd.DataFrame

init_premises(x_train)[source]

Initializes the MF premise parameters from training data.

Parameters:

x_train (torch.Tensor) – Training input data of shape (n_samples, input_size).

property num_mfs

Number of MFs per input feature.

Returns:

Tensor containing the number of MFs for each input feature.

Return type:

torch.Tensor

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

Plots the premise membership functions.

Parameters:
  • mf (int, optional) – Index of the MF to plot. If None, all MFs are plotted. Defaults to None.

  • input_dim (int, optional) – Index of the input feature to plot. If None, all input features are plotted. Defaults to None.

  • group_by_dim (bool) – If True, all MFs for each input feature are grouped into a single plot. Defaults to False.

  • linestyles (str or list[str]) – Line style or list of line styles to cycle through when plotting MFs. Accepted values are '-', '--', '-.', and ':'. Defaults to '-'.

  • linewidths (float or list[float]) – Line width or list of line widths to cycle through when plotting MFs. Defaults to 2.5.

set_premises(premises)[source]

Sets the premise parameters for all input features.

Parameters:

premises (list[torch.Tensor]) – List of premise parameter tensors. Each tensor must have shape (num_mfs, mf_params), where mf_params is the number of parameters of the MF type in use.

class neuro_fuzzy_toolbox.layers.fuzzification_layer.h_FuzzificationLayer(*args: Any, **kwargs: Any)[source]

Bases: Module

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

Transforms input data into membership degrees for each MF of each input feature. Unlike FuzzificationLayer, this layer enforces the same number of MFs for every input feature, constraining each to the same number of linguistic variables.

__init__(input_size, num_mfs=1, membership_function=torch.nn.Module, features=None, dtype=torch.float32)[source]

Initializes a new h_FuzzificationLayer instance.

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

  • num_mfs (int) – Number of MFs per input feature. Defaults to 1.

  • membership_function (MembershipFunction) – MF type to use. Defaults to GeneralizedBell_MF().

  • 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)[source]

Forward pass of the homogeneous fuzzification layer.

Computes the membership degrees for each input feature.

Parameters:

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

Returns:

Membership degrees of shape (batch_size, input_size, num_mfs).

Return type:

torch.Tensor

get_premises()[source]

Returns the current premise parameters.

Returns:

Premise parameter tensor of shape (input_size, num_mfs, mf_params), where mf_params is the number of parameters of the MF type in use.

Return type:

torch.Tensor

get_premises_as_parameters_list()[source]

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

Returns:

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

Return type:

list[nn.Parameter]

property get_premises_structure

Structure of the premise parameters.

Returns:

DataFrame describing the premise parameters for each input feature and MF.

Return type:

pd.DataFrame

init_premises(x_train)[source]

Initializes the MF premise parameters from training data.

Parameters:

x_train (torch.Tensor) – Training input data of shape (n_samples, input_size).

property num_mfs

Number of MFs per input feature.

Returns:

Number of MFs assigned to each input feature.

Return type:

int

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

Plots the premise membership functions.

Parameters:
  • mf (int, optional) – Index of the MF to plot. If None, all MFs are plotted. Defaults to None.

  • input_dim (int, optional) – Index of the input feature to plot. If None, all input features are plotted. Defaults to None.

  • group_by_dim (bool) – If True, all MFs for each input feature are grouped into a single plot. Defaults to False.

  • linestyles (str or list[str]) – Line style or list of line styles to cycle through when plotting MFs. Accepted values are '-', '--', '-.', and ':'. Defaults to '-'.

  • linewidths (float or list[float]) – Line width or list of line widths to cycle through when plotting MFs. Defaults to 2.5.

set_premises(premises)[source]

Sets the premise parameters for all input features.

Parameters:

premises (torch.Tensor) – Premise parameter tensor of shape (input_size, num_mfs, mf_params), where mf_params is the number of parameters of the MF type in use.

class neuro_fuzzy_toolbox.layers.fuzzification_layer.rule_reduced_FuzzificationLayer(*args: Any, **kwargs: Any)[source]

Bases: Module

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

Transforms input data into membership degrees for each MF of each input feature. Like h_FuzzificationLayer, this layer enforces the same number of MFs for every input feature. It is specifically designed for the rule-reduced ANFIS model, which supports structural adaptation via the SONFIS algorithm.

__init__(input_size, num_mfs=1, membership_function=torch.nn.Module, features=None, dtype=torch.float32)[source]

Initializes a new rule_reduced_FuzzificationLayer instance.

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

  • num_mfs (int) – Number of MFs per input feature. Defaults to 1.

  • membership_function (MembershipFunction) – MF type to use. Defaults to GeneralizedBell_MF().

  • 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)[source]

Forward pass of the rule-reduced fuzzification layer.

Computes the membership degrees for each input feature.

Parameters:

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

Returns:

Membership degrees of shape (batch_size, input_size, num_mfs).

Return type:

torch.Tensor

get_premises()[source]

Returns the current premise parameters.

Returns:

Premise parameter tensor of shape (input_size, num_mfs, mf_params), where mf_params is the number of parameters of the MF type in use.

Return type:

torch.Tensor

get_premises_as_parameters_list()[source]

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

Returns:

Premise parameters wrapped as a list of trainable parameters.

Return type:

nn.ParameterList

property get_premises_structure

Structure of the premise parameters.

Returns:

DataFrame describing the premise parameters for each input feature and MF.

Return type:

pd.DataFrame

init_premises(x_train)[source]

Initializes the MF premise parameters from training data.

Parameters:

x_train (torch.Tensor) – Training input data of shape (n_samples, input_size).

property num_mfs

Number of MFs per input feature.

Returns:

Number of MFs assigned to each input feature.

Return type:

int

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

Plots the premise membership functions.

Parameters:
  • mf (int, optional) – Index of the MF to plot. If None, all MFs are plotted. Defaults to None.

  • input_dim (int, optional) – Index of the input feature to plot. If None, all input features are plotted. Defaults to None.

  • group_by_dim (bool) – If True, all MFs for each input feature are grouped into a single plot. Defaults to False.

  • linestyles (str or list[str]) – Line style or list of line styles to cycle through when plotting MFs. Accepted values are '-', '--', '-.', and ':'. Defaults to '-'.

  • linewidths (float or list[float]) – Line width or list of line widths to cycle through when plotting MFs. Defaults to 2.5.

set_premises(premises)[source]

Sets the premise parameters for all input features.

Parameters:

premises (torch.Tensor) – Premise parameter tensor of shape (input_size, num_mfs, mf_params), where mf_params is the number of parameters of the MF type in use.