Consequent Functions
This module defines the consequent functions used in the toolbox. These functions define the output of each rule in neuro-fuzzy models.
All classes below inherit from the base class ConsequentFunction,
which defines the common interface and serves as a reference for future
implementations.
Note
Currently, only the standard linear consequent function of the ANFIS model is implemented. Adding new consequent function types would require non-trivial changes to the model and training algorithm implementations.
- class neuro_fuzzy_toolbox.func.consequent.Linear_CF(*args: Any, **kwargs: Any)[source]
Bases:
ConsequentFunctionLinear consequent function.
Computes the output of the neuro-fuzzy network from the input features and rule consequents as a linear combination, defined as:
\[O_j = \sum_{i=1}^{n} (c_{i,j} \cdot x_i) + c_{n+1,j}\]- where:
\(O_j\) is the \(j\)-th output of the consequent layer of an ANFIS model (associated to the \(j\)-th rule).
\(x_i\) is the \(i\)-th feature of an input sample \(x\) of size \(n\).
\(c_{i,j}\) is the \(i\)-th consequent parameter associated with the \(j\)-th rule of an ANFIS model.
- forward(x, consequents, weights)[source]
Forward pass of the linear consequent function.
- Parameters:
x (torch.Tensor) – Input tensor of shape
(batch_size, input_size)containing the input features.consequents (torch.Tensor) – Tensor of shape
(outputs, rules, input_size + 1)containing the consequent parameters, whererulesis the number of fuzzy rules andoutputsis the number of model outputs.weights (torch.Tensor) – Tensor of shape
(batch_size, rules)containing the normalized firing levels for each rule.
- Returns:
Tensor of shape
(outputs, batch_size, rules)containing the weighted rule outputs.- Return type:
torch.Tensor
- get_consequents_outputs(x, consequents)[source]
Returns the individual rule outputs without weighting by normalized firing levels.
- Parameters:
x (torch.Tensor) – Input tensor of shape
(batch_size, input_size)containing the input features.consequents (torch.Tensor) – Tensor of shape
(outputs, rules, input_size + 1)containing the consequent parameters, whererulesis the number of fuzzy rules andoutputsis the number of model outputs.
- Returns:
Tensor of shape
(outputs, batch_size, rules)containing the unweighted output of each rule, without multiplication by the normalized firing levels.- Return type:
torch.Tensor
- random_consequents(outputs, rules, input_size, dtype)[source]
Initializes the consequent parameters randomly in the range
[-1, 1].- Parameters:
outputs (int) – Number of model outputs.
rules (int) – Number of fuzzy rules.
input_size (int) – Number of input features.
dtype (torch.dtype) – Data type for the returned tensor.
- Returns:
Tensor of shape
(outputs, rules, input_size + 1)containing randomly initialized consequent parameters.- Return type:
torch.Tensor