soft.fuzzy.sets.continuous package
Submodules
soft.fuzzy.sets.continuous.abstract module
Implements an abstract class called ContinuousFuzzySet using PyTorch. All fuzzy sets defined over a continuous domain are derived from this class. Further, the Membership class is defined within, which contains a helpful interface understanding membership degrees.
- class soft.fuzzy.sets.continuous.abstract.ContinuousFuzzySet(centers=None, widths=None, use_sparse_tensor=False, labels: List[str] | None = None)
Bases:
ABC
,Module
A generic and abstract torch.nn.Module class that implements continuous fuzzy sets.
This is the most important Python class regarding fuzzy sets within this Soft Computing library.
Defined here are most of the common methods made available to all fuzzy sets. Fuzzy sets that will later be used in other features such as neuro-fuzzy networks are expected to abide by the conventions outlined within. For example, parameters ‘centers’ and ‘widths’ are often expected, but inference engines (should) only rely on the fuzzy set membership degrees.
However, for convenience, some aspects of the SelfOrganize code may search for vertices that have attributes of type ‘ContinuousFuzzySet’. Thus, if it is pertinent that a vertex within the KnowledgeBase is recognized as a fuzzy set, it is very likely one might be interested in inheriting or extending from ContinuousFuzzySet.
- area() Tensor
Calculate the area beneath the fuzzy curve (i.e., membership function) using torchquad.
This is a slightly expensive operation, but it is used for approximating the Mamdani fuzzy inference with arbitrary continuous fuzzy sets.
Typically, the results will be cached somewhere, so that the area value can be reused.
- Returns:
torch.Tensor
- area_helper(fuzzy_sets) List[float]
Splits the fuzzy set (if representing a fuzzy variable) into individual fuzzy sets (the fuzzy variable’s possible fuzzy terms), and does so recursively until the base case is reached. Once the base case is reached (i.e., a single fuzzy set), the area under its curve within the integration_domain is calculated. The result is a
- Parameters:
fuzzy_sets – The fuzzy set to split into smaller fuzzy sets.
- Returns:
A list of floats.
- abstract calculate_membership(observations: Tensor) NoReturn | Tensor
Calculate the membership of an element to this fuzzy set; not implemented as this is a generic and abstract class. This method is overridden by a class that specifies the type of fuzzy set (e.g., Gaussian, Triangular).
- Parameters:
observations – Two-dimensional matrix of observations, where a row is a single
observation. (observation and each column is related to an attribute measured during that)
- Returns:
None
- static count_granule_terms(granules: List[Type[ContinuousFuzzySet]]) ndarray
Count the number of granules that occur in each dimension.
- Parameters:
granules – A list of granules, where each granule is a ContinuousFuzzySet object.
- Returns:
A Numpy array with shape (len(granules), ) and the data type is integer.
- classmethod create(number_of_variables: int, number_of_terms: int) NoReturn | ContinuousFuzzySet
Create a fuzzy set with the given number of variables and terms, where each variable has the same number of terms. For example, if we have two variables, then we might have three terms for each variable, such as “low”, “medium”, and “high”. This would result in a total of nine fuzzy sets. The centers and widths are initialized randomly.
- Parameters:
number_of_variables – The number of variables.
number_of_terms – The number of terms.
- Returns:
A ContinuousFuzzySet object, or a NotImplementedError if the method is not implemented.
- extend(centers, widths)
Given additional parameters, centers and widths, extend the existing self.centers and self.widths, respectively. Additionally, update the necessary backend logic.
- Parameters:
centers – The centers of new fuzzy sets.
widths – The widths of new fuzzy sets.
- Returns:
None
- forward(observations) Membership
Forward pass of the function. Applies the function to the input elementwise.
- Parameters:
observations – Two-dimensional matrix of observations,
column (where a row is a single observation and each)
observation. (is related to an attribute measured during that)
- Returns:
The membership degrees of the observations for the Gaussian fuzzy set.
- static get_subclass(class_name: str) NoReturn | ContinuousFuzzySet
Get the subclass of ContinuousFuzzySet with the given class name.
- Parameters:
class_name – The name of the subclass of ContinuousFuzzySet.
- Returns:
A subclass of ContinuousFuzzySet.
- classmethod load(path: Path) ContinuousFuzzySet
Load the fuzzy set from a file.
- Returns:
None
- property mask: Tensor
Get the mask of the fuzzy set, where the mask is a tensor of the same shape as the centers and widths. The mask has a value of 1 if the fuzzy set exists, and 0 if the fuzzy set does not exist.
- Returns:
torch.Tensor
- reshape_parameters()
Reshape the parameters of the fuzzy set (e.g., centers, widths) so that they are the correct shape for subsequent operations.
- Returns:
None
- save(path: Path)
Save the fuzzy set to a file.
- Returns:
None
- split() ndarray
Efficient implementation of splitting this (self) fuzzy set, where each row contains the fuzzy sets for a fuzzy variable. For example, if we index the result with [0][1], then we would retrieve the second fuzzy (i.e., linguistic) term for the first fuzzy (i.e., linguistic) variable.
- Returns:
numpy.array
- split_by_variables() list | List[Type[ContinuousFuzzySet]]
This operation takes the ContinuousFuzzySet and converts it to a list of ContinuousFuzzySet objects, if applicable. For example, rather than using a single Gaussian object to represent all Gaussian membership functions in the input space, this function will convert that to a list of Gaussian objects, where each Gaussian function is defined and restricted to a single input dimension. This is particularly helpful when modifying along a specific dimension.
- Returns:
A list of ContinuousFuzzySet objects, where the length is equal to the number of input dimensions.
- static stack(granules: List[Type[ContinuousFuzzySet]]) ContinuousFuzzySet
Create a condensed and stacked representation of the given granules.
- Parameters:
granules – A list of granules, where each granule is a ContinuousFuzzySet object.
- Returns:
A ContinuousFuzzySet object.
- class soft.fuzzy.sets.continuous.abstract.Membership(elements, degrees, mask)
Bases:
Membership
The Membership class contains information describing both membership degrees and membership mask for some given elements. The membership degrees are often the degree of membership, truth, activation, applicability, etc. of a fuzzy set, or more generally, a concept. The membership mask is shaped such that it helps filter or ‘mask’ out membership degrees that belong to fuzzy sets or concepts that are not actually real.
The distinction between the two is made as applying the mask will zero out membership degrees that are not real, but this might be incorrectly interpreted as having zero degree of membership to the fuzzy set.
By including the elements’ information with the membership degrees and mask, it is possible to keep track of the original elements that were used to calculate the membership degrees. This is useful for debugging purposes, and it is also useful for understanding the membership degrees and mask in the context of the original elements. Also, it can be used in conjunction with the mask to filter out membership degrees that are not real, as well as assist in performing advanced operations.
soft.fuzzy.sets.continuous.group module
This module contains the GroupedFuzzySets class, which is a generic and abstract torch.nn.Module class that contains a torch.nn.ModuleList of ContinuousFuzzySet objects. The expectation here is that each ContinuousFuzzySet may define fuzzy sets of different conventions, such as Gaussian, Triangular, Trapezoidal, etc. Then, subsequent inference engines can handle these heterogeneously defined fuzzy sets with no difficulty. Further, this class was specifically designed to incorporate dynamic addition of new fuzzy sets in the construction of neuro-fuzzy networks via network morphism.
- class soft.fuzzy.sets.continuous.group.GroupedFuzzySets(*args, modules_list=None, expandable=False, **kwargs)
Bases:
Module
A generic and abstract torch.nn.Module class that contains a torch.nn.ModuleList of ContinuousFuzzySet objects. The expectation here is that each ContinuousFuzzySet may define fuzzy sets of different conventions, such as Gaussian, Triangular, Trapezoidal, etc. Then, subsequent inference engines can handle these heterogeneously defined fuzzy sets with no difficulty. Further, this class was specifically designed to incorporate dynamic addition of new fuzzy sets in the construction of neuro-fuzzy networks via network morphism.
However, this class does not carry out any functionality that is necessarily tied to fuzzy sets, it is simply named so as this was its intended purpose - grouping fuzzy sets. In other words, the same “trick” of using a torch.nn.ModuleList of torch.nn.Module objects applies to any kind of torch.nn.Module object.
- calculate_module_responses(observations) Tuple[Tensor, Tensor] | NoReturn
Calculate the responses from the modules in the torch.nn.ModuleList of GroupedFuzzySets.
- static evenly_spaced_exemplars(data: ndarray, max_peaks: int) ndarray
Find the peaks in the data and return the peaks, or a subset of the peaks if there are more than max_peaks.
- Parameters:
data – The data to find the peaks in.
max_peaks – The maximum number of peaks to return.
- Returns:
The peaks, or a subset of the peaks if there are more than max_peaks.
- expand(observations, module_responses, module_masks) Tuple[Tensor, Tensor, Tensor]
Expand the GroupedFuzzySets if necessary.
- forward(observations) Membership
Calculate the responses from the modules in the torch.nn.ModuleList of GroupedFuzzySets. Expand the GroupedFuzzySets if necessary.
- classmethod load(path: Path) GroupedFuzzySets
Load the model from the given path.
- Parameters:
path – The path to load the GroupedFuzzySet from.
- Returns:
The loaded GroupedFuzzySet.
- prune(module_type: Type[ContinuousFuzzySet]) None
Prune the torch.nn.ModuleList of GroupedFuzzySets by keeping the first module, but collapsing the rest of the modules into a single module. This is done to reduce the number of torch.nn.Modules in the list for computational efficiency.
- save(path: Path) None
Save the model to the given path.
- Parameters:
path – The path to save the GroupedFuzzySet to.
- Returns:
None
soft.fuzzy.sets.continuous.impl module
Implements various membership functions by inheriting from ContinuousFuzzySet.
- class soft.fuzzy.sets.continuous.impl.Gaussian(centers=None, widths=None, width_multiplier: float = 1.0, labels: List[str] | None = None)
Bases:
LogGaussian
Implementation of the Gaussian membership function, written in PyTorch.
- calculate_membership(observations: Tensor) Tensor
Calculate the membership of an element to this fuzzy set; not implemented as this is a generic and abstract class. This method is overridden by a class that specifies the type of fuzzy set (e.g., Gaussian, Triangular).
- Parameters:
observations – Two-dimensional matrix of observations, where a row is a single
observation. (observation and each column is related to an attribute measured during that)
- Returns:
None
- class soft.fuzzy.sets.continuous.impl.LogGaussian(centers=None, widths=None, width_multiplier: float = 1.0, labels: List[str] | None = None)
Bases:
ContinuousFuzzySet
Implementation of the Log Gaussian membership function, written in PyTorch. This is a modified version that helps when the dimensionality is high, and TSK product inference engine will be used.
- calculate_membership(observations: Tensor) Tensor
Calculate the membership of an element to this fuzzy set; not implemented as this is a generic and abstract class. This method is overridden by a class that specifies the type of fuzzy set (e.g., Gaussian, Triangular).
- Parameters:
observations – Two-dimensional matrix of observations, where a row is a single
observation. (observation and each column is related to an attribute measured during that)
- Returns:
None
- property sigmas: Tensor
Gets the sigma for the Gaussian fuzzy set; alias for the ‘widths’ parameter.
- Returns:
torch.Tensor
- class soft.fuzzy.sets.continuous.impl.LogisticCurve(midpoint, growth, supremum)
Bases:
Module
A generic torch.nn.Module class that implements a logistic curve, which allows us to tune the midpoint, and growth of the curve, with a fixed supremum (the supremum is the maximum value of the curve).
- forward(tensors: Tensor) Tensor
Calculate the value of the logistic curve at the given point.
- Parameters:
tensors
Returns:
- class soft.fuzzy.sets.continuous.impl.Lorentzian(centers=None, widths=None, labels: List[str] | None = None)
Bases:
ContinuousFuzzySet
Implementation of the Lorentzian membership function, written in PyTorch.
- calculate_membership(observations: Tensor) Tensor
Calculate the membership of an element to this fuzzy set; not implemented as this is a generic and abstract class. This method is overridden by a class that specifies the type of fuzzy set (e.g., Gaussian, Triangular).
- Parameters:
observations – Two-dimensional matrix of observations, where a row is a single
observation. (observation and each column is related to an attribute measured during that)
- Returns:
None
- property sigmas: Tensor
Gets the sigma for the Lorentzian fuzzy set; alias for the ‘widths’ parameter.
- Returns:
torch.Tensor
- class soft.fuzzy.sets.continuous.impl.Triangular(centers=None, widths=None, labels: List[str] | None = None)
Bases:
ContinuousFuzzySet
Implementation of the Triangular membership function, written in PyTorch.
- calculate_membership(observations: Tensor) Tensor
Forward pass of the function. Applies the function to the input elementwise.
- Parameters:
observations – Two-dimensional matrix of observations,
column (where a row is a single observation and each)
observation. (is related to an attribute measured during that)
- Returns:
The membership degrees of the observations for the Triangular fuzzy set.