Section for working with a fuzzy neural network

class fuzzyops.fuzzy_nn.mf_funcs.BellMemberFunc(a: float, b: float, c: float)[source]

Bases: Module

It represents an indistinct function of belonging to a decorated bell

Parameters:
  • a (float) – A parameter that defines the width of the function

  • b (float) – A parameter that defines the slope of the function

  • c (float) – Parameter of the function center

forward(x: Tensor) Tensor[source]

Calculates the value of the bell function for the input tensor

Parameters:

x (torch.Tensor) – The input tensor for which the value needs to be calculated

Returns:

The value of the bell function

Return type:

torch.Tensor

class fuzzyops.fuzzy_nn.mf_funcs.GaussMemberFunc(mu: float, sigma: float)[source]

Bases: Module

Represents a fuzzy Gauss membership function

Parameters:
  • mu (float) – The parameter of the center (the average value) of the function

  • sigma (float) – The parameter of the width (standard deviation) of the function

forward(x: Tensor) Tensor[source]

Calculates the value of the Gauss membership function for the input tensor

Parameters:

x (torch.Tensor) – The input tensor for which the value needs to be calculated

Returns:

The value of the Gaussian function

Return type:

torch.Tensor

fuzzyops.fuzzy_nn.mf_funcs.make_bell_mfs(a: float, b: float, c_list: List[float]) List[BellMemberFunc][source]

Creates a list of bell functions

Parameters:
  • a (float) – The width parameter for all created functions

  • b (float) – The tilt parameter for all created functions

  • c_list (List[float]) – A list of center parameters for creating functions

Returns:

A list of created bell functions

Return type:

List[BellMemberFunc]

fuzzyops.fuzzy_nn.mf_funcs.make_gauss_mfs(sigma: float, mu_list: List[float]) List[GaussMemberFunc][source]

Creates a list of Gaussian functions

Parameters:
  • sigma (float) – The width parameter for all created functions

  • mu_list (List[float]) – A list of center parameters for creating functions

Returns:

A list of created Gauss functions

Return type:

List[GaussMemberFunc]

class fuzzyops.fuzzy_nn.model.Model(X: ndarray, Y: ndarray, n_terms: list[int], n_out_vars: int, lr: float, task_type: str, batch_size: int, member_func_type: str, epochs: int, verbose: bool = False, device: str = 'cpu')[source]

Bases: object

A class for creating and training a fuzzy logic model

This class is designed to perform regression and classification tasks using fuzzy logic. It accepts input data, defines model parameters, and performs preprocessing of the data

X

A matrix of features from a data sample

Type:

np.ndarray

Y

The vector of the target variable from the data sample

Type:

np.ndarray

n_input_features

Number of input features

Type:

int

n_terms

A list containing the number of terms for each input feature

Type:

List[int]

n_out_vars

Number of output variables

Type:

int

lr

The learning step for optimization

Type:

float

task_type

Task type (“regression” или “classification”)

Type:

str

batch_size

The size of the subsample for training

Type:

int

member_func_type

Type of membership function (‘gauss’ - Gaussian membership function ‘bell’ is a generalized bell function)

Type:

str

device

The device on which the model will be executed (for example, “cpu” or “cuda”)

Type:

torch.device

epochs

The number of epochs for training the model

Type:

int

scores

A list for saving model metrics

Type:

list

verbose

The flag for the “detailed” output of information about the learning process

Type:

bool

model

The training model is currently undefined

Type:

torch.nn.Module

Parameters:
  • X (np.ndarray) – Input data for the model

  • Y (np.ndarray) – Target values for the model

  • n_terms (list[int]) – The number of terms for each input variable

  • n_out_vars (int) – The number of output variables

  • lr (float) – The learning step

  • task_type (str) – Task type (“regression” или “classification”)

  • batch_size (int) – The size of the subsample for training

  • member_func_type (str) – Type of membership function (‘gauss’ - Gaussian membership function ‘bell’ - generalized bell function)

  • epochs (int) – The number of epochs for training the model

  • verbose (bool) – The output detail level (False by default)

  • device (str) – Computing device (‘cpu’, ‘cuda’), default “cpu”

save_model(path: str) None[source]

Saves the state of the trained model to a file Saves the model parameters using the specified path

Parameters:

path (str) – The path to the file where the model will be saved

Raises:

Exception – If the model has not been trained

train() _NN[source]

Starts the learning process of the model

Performs data preprocessing, model compilation, and training cycle execution using the specified criteria and optimizer

Returns:

A trained model of a fuzzy neural network

Return type:

_NN

fuzzyops.fuzzy_nn.model.process_csv_data(path: str, target_col: str, n_features: int, use_label_encoder: bool, drop_index: bool, split_size: float = 0.2, use_split: bool = False) Tuple[ndarray, ndarray, ndarray, ndarray] | Tuple[ndarray, ndarray][source]

An additional function for data preprocessing with the possibility of dividing the sample into train, test

Parameters:
  • path (str) – The path to the data

  • target_col (str) – The name of the target column

  • n_features (int) – The number of input attributes

  • use_label_encoder (bool) – True - use encoding of input features (if they are specified as a string), False - no

  • drop_index (bool) – True - delete the column with indexes, False - no

  • split_size (float) – The size of the test subsample depends on the size of the entire dataset

  • use_split (bool) – Use division into train, test

Returns:

Union[Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray], Tuple[np.ndarray, np.ndarray]]: Preprocessed data