如何从 pyspark 中的 MLP 管道模型中获取最佳超参数?

How to get the best hyperparameter from MLP pipeline model in pyspark?

我正在使用来自 pyspark.ml.classificationMLP classifier。我正在使用交叉验证将我的 MLP 模型拟合到数据集,即; ParamGrid 方法。我正在使用 ParamGrid 方法迭代多个超参数。之后,我使用交叉验证 class 进行训练并获得最佳超参数。训练后,当我尝试从交叉验证对象访问最佳超参数时出现错误。谁能告诉我如何获得最好的超参数?

from pyspark.ml.classification import MultilayerPerceptronClassifier
layers = [4, 5, 4, 3]
clf = MultilayerPerceptronClassifier(labelCol='label',layers=layers)
pipeline = Pipeline(stages=[clf])
x1 = 'stepSize'
x2 = 'maxIter'
paramGrid = ParamGridBuilder() \
    .addGrid(getattr(clf,x1), [0.1, 0.2]) \
    .addGrid(getattr(clf,x2),[5,10])\
    .build()
evaluator = MulticlassClassificationEvaluator(labelCol='label',
                                                          predictionCol='prediction', metricName='f1')
crossval = CrossValidator(estimator=pipeline,
                                      estimatorParamMaps=paramGrid,
                                      evaluator=evaluator,
                                      numFolds=2)
cvModel = crossval.fit(train_data)
cvModel.bestModel.stages[0]._java_obj.getMaxIter()

错误:

Py4JError: An error occurred while calling o1127.getMaxIter. Trace:
py4j.Py4JException: Method getMaxIter([]) does not exist
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326)
    at py4j.Gateway.invoke(Gateway.java:274)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)

这个 cvModel.bestModel.stages[0]._java_obj.getMaxIter() 在我使用逻辑回归或随机森林 classifier 时有效。我仅在使用 MLP classifier 时收到错误。当我们使用 MLP classifier 时,有什么方法可以得到最好的超参数?

我遇到了同样的错误 运行 完全相同的代码和下面的行 post 为我解决了这个问题。

modelOnly.bestModel.stages[-1]._java_obj.parent().getRegParam()

所以您缺少的部分是 "parent()" 调用,您需要 "parent()" 调用。希望这对您有所帮助!