autoqild.classifiers.multi_layer_perceptronΒΆ

A customizable feedforward neural network implemented using Keras for more complex classification tasks.

Classes

MultiLayerPerceptron(n_features, n_classes)

MultiLayerPerceptron class for building and training a feedforward neural network using Keras.

class autoqild.classifiers.multi_layer_perceptron.MultiLayerPerceptron(n_features, n_classes, n_hidden=10, n_units=100, batch_normalization=True, activation='relu', loss_function='categorical_crossentropy', metrics=['accuracy'], optimizer_str='adam', reg_strength=0.0001, kernel_initializer='lecun_normal', learning_rate=0.001, early_stopping=False, model_save_path='', random_state=None, **kwargs)[source]ΒΆ

Bases: BaseEstimator, ClassifierMixin

MultiLayerPerceptron class for building and training a feedforward neural network using Keras.

Parameters:
  • n_features (int) – Number of features or dimensionality of the inputs.

  • n_classes (int) – Number of classes in the classification data samples.

  • n_hidden (int, optional, default=10) – Number of hidden layers.

  • n_units (int, optional, default=100) – Number of units per hidden layer.

  • batch_normalization (bool, optional, default=True) – Whether to use batch normalization.

  • activation (str, optional, default="relu") – Activation function to use in the hidden layers.

  • loss_function (str, optional, default="categorical_crossentropy") – Loss function to use for training.

  • metrics (list of str, optional, default=["accuracy"]) – List of metrics to be evaluated by the model during training and testing.

  • optimizer_str ({"adam", "sgd", ...}, default="adam") – Optimizer to use for training. Must be one of the optimizers available in Keras.

  • reg_strength (float, optional, default=1e-4) – Regularization strength for the L2 regularizer.

  • kernel_initializer (str, optional, default="lecun_normal") – Initializer for the kernel weights matrix.

  • learning_rate (float, optional, default=0.001) – Learning rate for the optimizer.

  • early_stopping (bool, optional, default=False) – Whether to use early stopping during training.

  • model_save_path (str, optional, default="") – Path to save the trained model.

  • random_state (int or None, optional, default=None) – Random state for reproducibility.

  • **kwargs (dict, optional) – Additional keyword arguments.

modelΒΆ

The compiled Keras model for training and prediction in form of labels.

Type:

keras.Model or None

scoring_modelΒΆ

The compiled Keras model for predicting real-valued scores.

Type:

keras.Model or None

loggerΒΆ

Logger object used for logging messages and errors.

Type:

logging.Logger

Private Methods
---------------
_construct_layers(**kwargs)[source]ΒΆ

Construct the input, hidden, and output layers for the MLP model.

_construct_model_[source]ΒΆ

Construct and compile the Keras models.

decision_function(X, verbose=0)[source]ΒΆ

Compute the real-valued scores for each class for the input samples.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Feature matrix.

  • verbose (int, optional, default=0) – Verbosity mode.

Returns:

decision – Decision function values.

Return type:

array-like of shape (n_samples,)

fit(X, y, epochs=50, batch_size=32, callbacks=None, validation_split=0.1, verbose=1, **kwd)[source]ΒΆ

Fit the MLP model to the training data.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Feature matrix.

  • y (array-like of shape (n_samples,)) – Target vector.

  • epochs (int, optional, default=50) – Number of training epochs.

  • batch_size (int, optional, default=32) – Number of samples per gradient update.

  • callbacks (list of keras.callbacks.Callback, optional) – List of callback instances to apply during training.

  • validation_split (float, optional, default=0.1) – Fraction of the training data to be used as validation data.

  • verbose (int, optional, default=1) – Verbosity mode.

  • **kwd (dict, optional) – Additional keyword arguments.

Returns:

self – Fitted estimator.

Return type:

MultiLayerPerceptron

get_params(deep=True)[source]ΒΆ

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

predict(X, verbose=0)[source]ΒΆ

Predict class labels for the input samples with maximum class probability.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Feature matrix.

  • verbose (int, optional, default=0) – Verbosity mode.

Returns:

y_pred – Predicted class labels.

Return type:

array-like of shape (n_samples,)

predict_proba(X, verbose=0)[source]ΒΆ

Predict class probabilities for the input samples.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Feature matrix.

  • verbose (int, optional, default=0) – Verbosity mode.

Returns:

p_pred – Predicted class probabilities.

Return type:

array-like of shape (n_samples, n_classes)

reshape_inputs(y)[source]ΒΆ

Reshape target variable to categorical format if necessary.

Parameters:

y (array-like of shape (n_samples,)) – Target vector.

Returns:

y – Reshaped target vector.

Return type:

array-like of shape (n_samples, n_classes)

score(X, y, sample_weight=None, verbose=0)[source]ΒΆ

Compute the balanced accuracy score for the input samples.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Feature matrix.

  • y (array-like of shape (n_samples,)) – True labels.

  • sample_weight (array-like of shape (n_samples,), optional) – Sample weights.

  • verbose (int, optional, default=0) – Verbosity mode.

Returns:

acc – Balanced accuracy score.

Return type:

float

set_params(**parameters)[source]ΒΆ

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as :class:”~sklearn.pipeline.Pipeline”). The latter have parameters of the form β€œβ€<component>__<parameter>”” so that it”s possible to update each component of a nested object.

Parameters:

**params (dict) – Estimator parameters.

Returns:

self – Estimator instance.

Return type:

estimator instance