【问题标题】:How to get neurons weights from MultilayerPerceptronClassifier如何从 MultilayerPerceptronClassifier 获取神经元权重
【发布时间】:2018-08-15 00:21:06
【问题描述】:

我在 pySpark(使用 Spark 1.6.0)中使用 MLP 多类分类器,或多或少遵循 here 中的示例。

由于我有兴趣训练一次模型,然后在不同的数据集上使用已经训练过的模型,我想检索神经元权重(就像使用 pickle 包的 python sklearn 解释 here 一样)。

但是,在阅读documentation 后,我无法获得模型的权重和内部参数。

如果有帮助,我的代码是:

# Importing PySpark libraries
from pyspark import SparkConf, SparkContext
from pyspark.sql import SQLContext, HiveContext
from pyspark.ml.classification import MultilayerPerceptronClassifier
from pyspark.ml.evaluation import MulticlassClassificationEvaluator

#%% Codigo inicio

if __name__ == "__main__":

    conf  = SparkConf().setAppName("prueba_features")
    sc    = SparkContext(conf=conf)
    hc    = HiveContext(sc)
    sqlc  = SQLContext(sc)

    # Load training data
    data = sqlc.read.format("libsvm")\
        .load("/user/sample_multiclass_classification_data.txt")

    # print data
    print("\nData set: \n{}".format(data))

    # Split the data into train and test
    splits = data.randomSplit([0.6, 0.4], 1234)
    train = splits[0]
    test = splits[1]

    # print sets
    print("\nTraining set: \n{}".format(train))
    print("\nTest set: \n{}".format(test))

    # specify layers for the neural network:
    # input layer of size 4 (features), two intermediate of size 5 and 4
    # and output of size 3 (classes)
    layers = [4, 5, 4, 3]

    # create the trainer and set its parameters
    trainer = MultilayerPerceptronClassifier(maxIter=100, layers=layers, blockSize=128, seed=1234)

    # train the model
    model = trainer.fit(train)

    # compute precision on the test set
    result = model.transform(test)
    predictionAndLabels = result.select("prediction", "label")
    evaluator_prec = MulticlassClassificationEvaluator(metricName="precision")
    evaluator_rec = MulticlassClassificationEvaluator(metricName="recall")
    evaluator_f1 = MulticlassClassificationEvaluator(metricName="f1")

    # print fitting precision and results
    print("\nResults: \n{}".format(result))

    print("\nKPIs")
    print("Precision: " + str(evaluator_prec.evaluate(predictionAndLabels)))
    print("Recall: " + str(evaluator_rec.evaluate(predictionAndLabels)))
    print("F1-score: " + str(evaluator_f1.evaluate(predictionAndLabels)))

    # we end the SparkContext
    sc.stop()

如果可能的话,有人知道如何使用 pySpark MLP 吗?

【问题讨论】:

    标签: python apache-spark pyspark


    【解决方案1】:

    你要找的方法是weights:

    weights

    层的权重。

    2.0.0 版中的新功能。

    正如注释所说,您需要将 Spark 版本更新到至少 2.0 才能使用它。

    【讨论】:

    • 谢谢!但是升级到 2.0.0 不是一个选项,因为无法修改环境......在 Spark 1.6.0 中是否没有一个选项可以像使用具有配置参数的文件一样初始化 MLP 分类器? /跨度>
    猜你喜欢
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 2018-11-28
    • 1970-01-01
    • 2017-12-14
    • 2017-09-23
    相关资源
    最近更新 更多