Skip to content

MLPRegressor

Multi-layer Perceptron for regression.

This model is still work in progress. Here are some features that still need implementing:

  • learn_one and predict_one just cast the input dict to a single row dataframe and then call learn_many and predict_many respectively. This is very inefficient. - Not all of the optimizers in the optim module can be used as they are not all vectorised. - Emerging and disappearing features are not supported. Each instance/batch has to have the same features. - The gradient haven't been numerically checked.

Parameters

  • hidden_dims

    The dimensions of the hidden layers. For example, specifying (10, 20) means that there are two hidden layers with 10 and 20 neurons, respectively. Note that the number of layers the network contains is equal to the number of hidden layers plus two (to account for the input and output layers).

  • activations

    The activation functions to use at each layer, including the input and output layers. Therefore you need to specify three activation if you specify one hidden layer.

  • loss (optim.losses.Loss) – defaults to None

    Loss function. Defaults to optim.losses.Squared.

  • optimizer (optim.Optimizer) – defaults to None

    Optimizer. Defaults to optim.SGD(.01).

  • seed (int) – defaults to None

    Random number generation seed. Set this for reproducibility.

Attributes

  • n_layers

    Return the number of layers in the network. The number of layers is equal to the number of hidden layers plus 2. The 2 accounts for the input layer and the output layer.

Examples

>>> from river import datasets
>>> from river import evaluate
>>> from river import neural_net as nn
>>> from river import optim
>>> from river import preprocessing as pp
>>> from river import metrics

>>> model = (
...     pp.StandardScaler() |
...     nn.MLPRegressor(
...         hidden_dims=(5,),
...         activations=(
...             nn.activations.ReLU,
...             nn.activations.ReLU,
...             nn.activations.Identity
...         ),
...         optimizer=optim.SGD(1e-3),
...         seed=42
...     )
... )

>>> dataset = datasets.TrumpApproval()

>>> metric = metrics.MAE()

>>> evaluate.progressive_val_score(dataset, model, metric)
MAE: 1.589827

You can also use this to process mini-batches of data.

>>> model = (
...     pp.StandardScaler() |
...     nn.MLPRegressor(
...         hidden_dims=(10,),
...         activations=(
...             nn.activations.ReLU,
...             nn.activations.ReLU,
...             nn.activations.ReLU
...         ),
...         optimizer=optim.SGD(1e-4),
...         seed=42
...     )
... )

>>> dataset = datasets.TrumpApproval()
>>> batch_size = 32

>>> for epoch in range(10):
...     for xb in pd.read_csv(dataset.path, chunksize=batch_size):
...         yb = xb.pop('five_thirty_eight')
...         y_pred = model.predict_many(xb)
...         model = model.learn_many(xb, yb)

>>> model.predict_many(xb)
      five_thirty_eight
992           39.361609
993           46.398536
994           42.094086
995           40.195802
996           40.782954
997           40.839678
998           40.896403
999           48.362659
1000          42.021849

Methods

call

Make predictions.

Parameters

  • X (pandas.core.frame.DataFrame)
clone

Return a fresh estimator with the same parameters.

The clone has the same parameters but has not been updated with any data. This works by looking at the parameters from the class signature. Each parameter is either - recursively cloned if it's a River classes. - deep-copied via copy.deepcopy if not. If the calling object is stochastic (i.e. it accepts a seed parameter) and has not been seeded, then the clone will not be idempotent. Indeed, this method's purpose if simply to return a new instance with the same input parameters.

learn_many

Train the network.

Parameters

  • X (pandas.core.frame.DataFrame)
  • y (pandas.core.frame.DataFrame)
learn_one

Fits to a set of features x and a real-valued target y.

Parameters

  • x (dict)
  • y (numbers.Number)

Returns

Regressor: self

predict_many
predict_one

Predicts the target value of a set of features x.

Parameters

  • x (dict)

Returns

Number: The prediction.