3

我在 scikit-learn 的 MLPClassifier 中试验了 warm_start 参数。但是,我在运行以下代码时收到错误消息。

    clf = MLPClassifier(solver='adam',
                        hidden_layer_sizes=(128, 128),
                        activation='relu',
                        max_iter = 3,
                        verbose=True,
                        tol= 1e-100,
                        n_iter_no_change=10,
                        early_stopping=True,
                        warm_start=True)

    clf.fit(df_feat, df_label)
    clf.fit(df_feat, df_label)

以下是我的输出和错误消息。第一个 clf.fit() 能够运行完成,但第二个 clf.fit() 在 1 次迭代后产生错误消息。

Iteration 1, loss = 1.84596208
Validation score: 0.610841
Iteration 2, loss = 1.04435735
Validation score: 0.731758
Iteration 3, loss = 0.84135025
Validation score: 0.760945
C:\Users\LHongRu1\Anaconda3\envs\py36\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py:571: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (3) reached and th
e optimization hasn't converged yet.
  % self.max_iter, ConvergenceWarning)
Iteration 4, loss = 0.77392135
Traceback (most recent call last):
  File "train_3(to_experiment).py", line 70, in <module>
    main()
  File "train_3(to_experiment).py", line 62, in main
    clf.fit(df_feat, df_label)
  File "C:\Users\LHongRu1\Anaconda3\envs\py36\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py", line 995, in fit
    hasattr(self, "classes_")))
  File "C:\Users\LHongRu1\Anaconda3\envs\py36\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py", line 370, in _fit
    intercept_grads, layer_units, incremental)
  File "C:\Users\LHongRu1\Anaconda3\envs\py36\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py", line 540, in _fit_stochastic
    self._update_no_improvement_count(early_stopping, X_val, y_val)
  File "C:\Users\LHongRu1\Anaconda3\envs\py36\lib\site-packages\sklearn\neural_network\_multilayer_perceptron.py", line 604, in _update_no_improvement_count
    if self.loss_curve_[-1] > self.best_loss_ - self.tol:
AttributeError: 'MLPClassifier' object has no attribute 'best_loss_'

如果我更改为warm_start = False,则 clf.fit() 都能够运行完成。

4

1 回答 1

2

试试看early_stopping=False,对我有用。解决问题。

于 2020-05-07T18:16:08.623 回答