首页 > 解决方案 > 仅在 max_iter 之后停止训练 MLPRegressor (solver=lbfgs) 而不是因为“tol”

问题描述

我正在MLPRegressor使用求解器训练模型lbfgs。我已将max_iter参数从默认的 200 更改为 500。我想强制训练持续到 500 次迭代,并且在损失至少没有改善时不停止tol

我已经尝试将 tol 设置为 0.0,然后继续将其设置为负数(例如 -10)

mymodel = mlpr(hidden_layer_sizes=(3,), activation = 'tanh', solver = 
'lbfgs',max_iter=500, tol=0.0, verbose=True)
for i in range(99):
    mymodel = mymodel.fit(xtrain,ytrain)
    print("The number of iterations ran was: ",mymodel.n_iter_)

这就是我得到的:

The number of iterations ran was:  56
The number of iterations ran was:  162
The number of iterations ran was:  154 
The number of iterations ran was:  169
The number of iterations ran was:  127
The number of iterations ran was:  40
The number of iterations ran was:  501
The number of iterations ran was:  501
The number of iterations ran was:  502
The number of iterations ran was:  198

我预计每次迭代 500 次。(甚至没有 501 或 502,因为它们超过了我在 500 中指定的max_iter

标签: pythonscikit-learnneural-network

解决方案


tol参数指定优化的容差。如果损失或分数至少没有改善tol,则认为训练结束,达到收敛。尝试将tol参数设置为None,如它所指示的那样,这样训练在达到-infinity之前不会停止。max_iter

mymodel = mlpr(hidden_layer_sizes=(3,), activation = 'tanh', solver = 
'lbfgs',max_iter=500, tol=None, verbose=True)

推荐阅读