Skip to content

AttributeError: 'MLPRegressor' object has no attribute '_best_coefs' #24713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
davidshumway opened this issue Oct 21, 2022 · 10 comments · Fixed by #24788
Closed

AttributeError: 'MLPRegressor' object has no attribute '_best_coefs' #24713

davidshumway opened this issue Oct 21, 2022 · 10 comments · Fixed by #24788

Comments

@davidshumway
Copy link

davidshumway commented Oct 21, 2022

The following parameters were working fine with another dataset. When I switched to a new dataset, for some reason an AttributeError is occurring. Any ideas?

model = MLPRegressor(hidden_layer_sizes=(10), activation='tanh', batch_size=1000,
  learning_rate_init=0.1, max_iter=5000, momentum=.9, solver='adam', early_stopping=True,
  random_state=1, verbose=1)
X = [[0.,0.,0.,0.2173913,0.,0.5,
  0.,0.46666667,0.76351351,0.41140777,0.55555556,0.03361345,
  0.09022556,0.78107607,0.13250518,1.,0.,0.,
  0.,0.,1.],
 [0.80769231,1.,0.,0.69565217,0.20942029,0.5,
  0.,0.13333333,0.,0.,0.88888889,0.95424837,
  0.96491228,0.83766234,0.97584541,0.,0.,0.,
  0.,0.,0.]] 
y = [1.0, 1.0]
model.fit(X, y)

Output:

...
Iteration 4997, loss = 0.00001391
Validation score: nan
Iteration 4998, loss = 0.00002568
Validation score: nan
Iteration 4999, loss = 0.00001822
Validation score: nan
Iteration 5000, loss = 0.00001470
Validation score: nan

AttributeError: 'MLPRegressor' object has no attribute '_best_coefs'

The error occurs here:

self.coefs_ = self._best_coefs

The error also seems to completely disappear when there are more than around 10-25 training examples.

@github-actions github-actions bot added the Needs Triage Issue requires triage label Oct 21, 2022
@glemaitre
Copy link
Member

You have 2 samples and request early_stopping. From the verbose, it seems that the validation score is always nan probably because you are using 0 or 1 sample (10% of 2 samples). Since the coefficients are stored only if the score is decreasing, it actually never happens and thus _best_coefs is never set.

This is really a corner case and we choose to detect it much earlier even before to start trying to optimize the problem because we don't have enough samples in the validation set.

@glemaitre glemaitre added Bug and removed Needs Triage Issue requires triage labels Oct 22, 2022
@glemaitre
Copy link
Member

The error also seems to completely disappear when there are more than around 10-25 training examples.

Be aware that you should not do any machine learning with so few samples.

@davidshumway
Copy link
Author

You have 2 samples and request early_stopping. From the verbose, it seems that the validation score is always nan probably because you are using 0 or 1 sample (10% of 2 samples). Since the coefficients are stored only if the score is decreasing, it actually never happens and thus _best_coefs is never set.

The example provided is 2 samples but it's happening with up to 10 samples.

@glemaitre
Copy link
Member

Yes you need at least 2 samples to compute the R2 scores. By default, you take 10% validation. so above 10 samples, you get the required 2 samples (with the rounding).

@davidshumway
Copy link
Author

Yes you need at least 2 samples to compute the R2 scores. By default, you take 10% validation. so above 10 samples, you get the required 2 samples (with the rounding).

Aha got it

@betatim
Copy link
Member

betatim commented Oct 24, 2022

This is really a corner case and we choose to detect it much earlier even before to start trying to optimize the problem because we don't have enough samples in the validation set.

Does the earlier check actually get made? If yes, why did it not catch this case? Or was your comment a "we should detect it much earlier" -> a PR would be helpful/welcome to implement this?

@glemaitre
Copy link
Member

Sorry for the confusion.

I meant that we should create a PR to detect this case (i.e. not enough samples to compute the validation metric) and raise an error.

@mohitthakur13
Copy link
Contributor

mohitthakur13 commented Oct 29, 2022

Hi @glemaitre ,
As nobody seemed to have taken it up, I tried to attempt to design a fix based on your recommendation. Would you like to have a look at it and suggest improvements? (See PR above.)
Thank you.

@sdivyanshu90
Copy link

Hi @glemaitre,

I hope you're doing well. I came across this issue and wanted to check if someone is already working on it. If it's still open and no one is currently assigned, I’d happily take it up and work on resolving it.

Looking forward to your response.

@glemaitre
Copy link
Member

It looks like #24788 has been open. Before to open a new PR, I'll like to have a look at this open PR because it seems that we did not provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment