Membership Functions

This module defines the membership functions (MFs) used in the toolbox. MFs are used to define the fuzzy sets of the input variables in neuro-fuzzy models.

All classes below inherit from the base class MembershipFunction, which defines the common interface and serves as a reference for future implementations.

class neuro_fuzzy_toolbox.func.membership.Gaussian_MF(*args: Any, **kwargs: Any)[source]

Bases: MembershipFunction

Gaussian membership function, defined as:

\[gaussian(x) = e^{-\frac{(x - \mu)^2}{2\sigma^2}}\]
donde:
  • \(x\) is the input variable.

  • \(\mu\) is the center of the function.

  • \(\sigma\) is the standard deviation, controlling the width of the curve.

forward(x, premises)[source]

Forward pass of the Gaussian membership function.

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

  • premises (torch.Tensor) – Premise parameters of shape (input_size, num_mfs, len(self._params)).

Returns:

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

Return type:

torch.Tensor

general_initialize_premises(x_train, mf_distribution)[source]

Initializes the Gaussian MF premise parameters from training data, allowing a different number of MFs per input feature.

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

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

Returns:

List of premise parameter tensors, one per input feature. The tensor at index i has shape (input_size, mf_distribution[i], len(self._params)).

Return type:

list[torch.Tensor]

initialize_premises(x_train, num_mfs)[source]

Initializes the Gaussian MF premise parameters from training data, assuming the same number of MFs for every input feature.

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

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

Returns:

Initialized premise parameters of shape (input_size, num_mfs, len(self._params)).

Return type:

torch.Tensor

random_premises(input_size, num_mfs, dtype)[source]

Generates randomly initialized premise parameters in the range [-1, 1], with \(\sigma\) constrained to positive values.

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

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

  • dtype (torch.dtype) – Data type of the premise parameters.

Returns:

Randomly initialized premise parameters of shape (input_size, num_mfs, len(self._params)).

Return type:

torch.Tensor

random_single_feature_mfs(n_mfs, dtype)[source]

Generates randomly initialized premise parameters for a single input feature in the range [-1, 1], with \(\sigma\) constrained to positive values.

Parameters:
  • n_mfs (int) – Number of MFs.

  • dtype (torch.dtype) – Data type of the premise parameters.

Returns:

Randomly initialized premise parameters of shape (n_mfs, len(self._params)).

Return type:

torch.Tensor

class neuro_fuzzy_toolbox.func.membership.GeneralizedBell_MF(*args: Any, **kwargs: Any)[source]

Bases: MembershipFunction

Generalized bell-shaped membership function, defined as:

\[generalized\_bell(x) = \frac{1}{1 + \left(\frac{|x - c|}{a}\right)^{2b}}\]
where:
  • \(x\) is the input variable.

  • \(a\) is the width parameter.

  • \(b\) is the slope parameter.

  • \(c\) is the center parameter.

forward(x, premises)[source]

Forward pass of the Generalized Bell membership function.

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

  • premises (torch.Tensor) – Premise parameters of shape (input_size, num_mfs, len(self._params)).

Returns:

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

Return type:

torch.Tensor

general_initialize_premises(x_train, mf_distribution)[source]

Initializes the Generalized Bell MF premise parameters from training data, allowing a different number of MFs per input feature.

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

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

Returns:

List of premise parameter tensors, one per input feature. The tensor at index i has shape (input_size, mf_distribution[i], len(self._params)).

Return type:

list[torch.Tensor]

initialize_premises(x_train, num_mfs)[source]

Initializes the Generalized Bell MF premise parameters from training data, assuming the same number of MFs for every input feature.

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

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

Returns:

Initialized premise parameters of shape (input_size, num_mfs, len(self._params)).

Return type:

torch.Tensor

random_premises(input_size, num_mfs, dtype)[source]

Generates randomly initialized premise parameters in the range [-1, 1], with the width and slope parameters (\(a\) and \(b\)) constrained to positive values.

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

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

  • dtype (torch.dtype) – Data type of the premise parameters.

Returns:

Randomly initialized premise parameters of shape (input_size, num_mfs, len(self._params)).

Return type:

torch.Tensor

random_single_feature_mfs(n_mfs, dtype)[source]

Generates randomly initialized premise parameters for a single input feature in the range [-1, 1], with the width and slope parameters (\(a\) and \(b\)) constrained to positive values.

Parameters:
  • n_mfs (int) – Number of MFs.

  • dtype (torch.dtype) – Data type of the premise parameters.

Returns:

Randomly initialized premise parameters of shape (n_mfs, len(self._params)).

Return type:

torch.Tensor

class neuro_fuzzy_toolbox.func.membership.HighSlopeBell_MF(*args: Any, **kwargs: Any)[source]

Bases: MembershipFunction

High-slope generalized bell-shaped membership function.

A variant of the Generalized Bell MF where the slope parameter \(b\) is fixed at 8.0, enforcing a strict and consistent shape across all MFs. It is defined as:

\[generalized\_bell(x) = \frac{1}{1 + \left(\frac{|x - c|}{a}\right)^{2\cdot 8}}\]
donde:
  • \(x\) is the input variable.

  • \(a\) is the width parameter.

  • \(c\) is the center parameter.

forward(x, premises)[source]

Forward pass of the High-Slope Bell membership function.

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

  • premises (torch.Tensor) – Premise parameters of shape (input_size, num_mfs, len(self._params)).

Returns:

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

Return type:

torch.Tensor

general_initialize_premises(x_train, mf_distribution)[source]

Initializes the High-Slope Bell MF premise parameters from training data, allowing a different number of MFs per input feature.

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

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

Returns:

List of premise parameter tensors, one per input feature. The tensor at index i has shape (input_size, mf_distribution[i], len(self._params)).

Return type:

list[torch.Tensor]

initialize_premises(x_train, num_mfs)[source]

Initializes the High-Slope Bell MF premise parameters from training data, assuming the same number of MFs for every input feature.

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

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

Returns:

Initialized premise parameters of shape (input_size, num_mfs, len(self._params)).

Return type:

torch.Tensor

random_premises(input_size, num_mfs, dtype)[source]

Generates randomly initialized premise parameters in the range [-1, 1], with the width parameter (\(a\)) constrained to positive values.

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

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

  • dtype (torch.dtype) – Data type of the premise parameters.

Returns:

Randomly initialized premise parameters of shape (input_size, num_mfs, len(self._params)).

Return type:

torch.Tensor

random_single_feature_mfs(n_mfs, dtype)[source]

Generates randomly initialized premise parameters for a single input feature in the range [-1, 1], with the width parameter (\(a\)) constrained to positive values.

Parameters:
  • n_mfs (int) – Number of MFs.

  • dtype (torch.dtype) – Data type of the premise parameters.

Returns:

Randomly initialized premise parameters of shape (n_mfs, len(self._params)).

Return type:

torch.Tensor