mojolearn.MLPClassifier#
- class mojolearn.MLPClassifier(hidden_layer_sizes=(100,), activation='relu', *, solver='adam', alpha=0.0001, batch_size='auto', learning_rate='constant', learning_rate_init=0.001, power_t=0.5, max_iter=200, shuffle=True, random_state=None, tol=0.0001, verbose=False, warm_start=False, momentum=0.9, nesterovs_momentum=True, early_stopping=False, validation_fraction=0.1, beta_1=0.9, beta_2=0.999, epsilon=1e-08, n_iter_no_change=10, max_fun=15000, numeric_mode=None)[source]#
Bases:
_BaseMLPMulti-layer perceptron classifier, as scikit-learn’s
MLPClassifier.Minimizes the log loss plus an L2 penalty, with a logistic output for two classes and softmax for more, using Adam or SGD. Parameters, attributes, losses, update rules, learning-rate schedules and the stopping rule follow scikit-learn. Weights use scikit-learn’s Glorot-uniform bounds but are drawn from mojolearn’s own seeded generator, and the shuffle order is mojolearn’s own, so results for a given
random_statediffer from scikit-learn’s.Xmust be 2-D float32 (float64 raisesTypeError);yis a 1-D array of labels with at least two classes. Not supported: multilabel targets,solver='lbfgs',early_stopping=True,warm_start,partial_fitandverbose.- Parameters:
hidden_layer_sizes (int or tuple of int, default (100,)) – Width of each hidden layer; every width must be > 0.
activation ({‘identity’, ‘logistic’, ‘tanh’, ‘relu’}, default ‘relu’) – Hidden-layer activation.
solver ({‘adam’, ‘sgd’}, default ‘adam’) – Optimizer. ‘lbfgs’ raises
NotImplementedError.alpha (float, default 0.0001) – L2 penalty strength (>= 0).
batch_size (int or ‘auto’, default ‘auto’) – Minibatch size; ‘auto’ is
min(200, n_samples).learning_rate ({‘constant’, ‘invscaling’, ‘adaptive’}, default ‘constant’) – Learning-rate schedule of the ‘sgd’ solver, as scikit-learn.
learning_rate_init (float, default 0.001) – Initial learning rate (> 0).
power_t (float, default 0.5) – Exponent of the ‘invscaling’ schedule.
max_iter (int, default 200) – Maximum number of epochs (>= 1).
shuffle (bool, default True) – Shuffle the rows every epoch.
random_state (int or None, default None) – Seed of the weight initialization and the shuffle.
tol (float, default 1e-4) – Tolerance of the stopping rule.
verbose (bool, default False) – Must be False; True raises
NotImplementedError.warm_start (bool, default False) – Must be False; True raises
NotImplementedError.momentum (float, default 0.9) – Momentum of the ‘sgd’ solver.
nesterovs_momentum (bool, default True) – Use Nesterov momentum with the ‘sgd’ solver.
early_stopping (bool, default False) – Must be False; True raises
NotImplementedError.validation_fraction (float, default 0.1) – Accepted for compatibility; unused because early stopping is not supported.
beta_1 (float, default 0.9) – Adam’s first-moment decay.
beta_2 (float, default 0.999) – Adam’s second-moment decay.
epsilon (float, default 1e-8) – Adam’s denominator term.
n_iter_no_change (int, default 10) – Epochs without
tolimprovement before stopping.max_fun (int, default 15000) – Accepted for compatibility (lbfgs only); unused.
numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode. None uses the library default.
- Variables:
classes_ (ndarray) – Sorted class labels.
coefs_ (list of ndarray) – Weight matrix of each layer, (n_in, n_out).
intercepts_ (list of ndarray) – Bias vector of each layer.
loss_ (float) – Loss of the last epoch.
best_loss_ (float) – Lowest loss seen.
n_iter_ (int) – Number of epochs run.
t_ (int) – Number of training samples seen.
n_features_in_ (int) – Number of input features.
n_layers_ (int) – Number of layers, input and output included.
n_outputs_ (int) – Number of outputs.
out_activation_ (str) – Output activation.
Examples
>>> import numpy as np, mojolearn >>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32) >>> y = (X[:, 0] > 0.5).astype(np.int64) >>> model = mojolearn.MLPClassifier(hidden_layer_sizes=(32,), max_iter=50).fit(X, y) >>> proba = model.predict_proba(X)
- __init__(hidden_layer_sizes=(100,), activation='relu', *, solver='adam', alpha=0.0001, batch_size='auto', learning_rate='constant', learning_rate_init=0.001, power_t=0.5, max_iter=200, shuffle=True, random_state=None, tol=0.0001, verbose=False, warm_start=False, momentum=0.9, nesterovs_momentum=True, early_stopping=False, validation_fraction=0.1, beta_1=0.9, beta_2=0.999, epsilon=1e-08, n_iter_no_change=10, max_fun=15000, numeric_mode=None)#
Methods