mojolearn.MLPRegressor#

class mojolearn.MLPRegressor(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: _BaseMLP

Multi-layer perceptron regressor, as scikit-learn’s MLPRegressor.

Minimizes half the mean squared error plus an L2 penalty, with an identity output layer, 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_state differ from scikit-learn’s.

X must be 2-D float32 (float64 raises TypeError); y is (n_samples,) or (n_samples, n_outputs). Not supported: solver='lbfgs', early_stopping=True, warm_start, partial_fit and verbose.

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 tol improvement 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:
  • 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.

  • loss_curve_ (list of float) – Loss of every epoch.

  • 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.sum(axis=1)
>>> model = mojolearn.MLPRegressor(hidden_layer_sizes=(64,), max_iter=50).fit(X, y)
>>> pred = model.predict(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

fit(X, y)

Train on X (n_samples, n_features) float32 and targets y; returns self.

predict(X)

Predicted values, (n_samples,) for a 1-D training target, else (n_samples, n_outputs).

fit(X, y)[source]#

Train on X (n_samples, n_features) float32 and targets y; returns self.

predict(X)[source]#

Predicted values, (n_samples,) for a 1-D training target, else (n_samples, n_outputs).