0

I have a neural network model which I wish to fit to my training data. When I compile the below line of code

history = pipeline.fit(inputs[train], targets[train], epochs=epochs, batchsize=batchsize)

I receive the following error message:

Pipeline.fit does not accept the epochs parameter. You can pass parameters to specific 
steps of your pipeline using the stepname__parameter format, e.g. 
`Pipeline.fit(X, y, logisticregression__sample_weight=sample_weight)`

How to resolve this issue?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Nanda
  • 361
  • 1
  • 5
  • 14
  • Please post a [mre]; it is impossible to know how an `epochs` parameter might be involved here with the information (not) provided. – desertnaut Dec 13 '21 at 15:17

1 Answers1

3

I don't think that the epochs parameter is defined for MLPClassifier; you should use the max_iter parameter instead.

Then if you want to specify hyperparameters within a Pipeline, you can do as follows:

from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.impute import SimpleImputer    

from sklearn.datasets import make_classification


X, y = make_classification()


model = make_pipeline(SimpleImputer(), StandardScaler(), MLPClassifier())

params = {
    'mlpclassifier__max_iter' : 10,
    'mlpclassifier__batch_size' : 20
}

model.set_params(**params)
model.fit(X, y)

I would suggest using this notation as you can easily reuse it to perform a GridSearchCV.

Mario
  • 1,631
  • 2
  • 21
  • 51
Antoine Dubuis
  • 4,974
  • 1
  • 15
  • 29
  • Thank you, Antoine. Is there a way to include "impute" in the pipeline? I have a dataset with several NaN values, so I have to get rid of these via imputation. – Nanda Dec 13 '21 at 14:16
  • I added `SimpleImputer` to my example. – Antoine Dubuis Dec 13 '21 at 14:39
  • Hi Antoine, I tried your example. But, somehow, it does not work. I am not able to decode the following error message. "X has 14 features, but SimpleImputer is expecting 20 features as input." The input X has 14 features in my dataset, but I am not able to understand why SimpleImputer expects 20 features. – Nanda Dec 13 '21 at 15:27