2015-03-25 58 views
11

\负荷模型我使用PySpark和MLlib星火1.3.0工作的正确方法,我需要保存和载入我的模型。我用这样的代码(来自官方documentation拍摄)什么是保存在星火 PySpark

from pyspark.mllib.recommendation import ALS, MatrixFactorizationModel, Rating 

data = sc.textFile("data/mllib/als/test.data") 
ratings = data.map(lambda l: l.split(',')).map(lambda l: Rating(int(l[0]), int(l[1]), float(l[2]))) 
rank = 10 
numIterations = 20 
model = ALS.train(ratings, rank, numIterations) 
testdata = ratings.map(lambda p: (p[0], p[1])) 
predictions = model.predictAll(testdata).map(lambda r: ((r[0], r[1]), r[2])) 
predictions.collect() # shows me some predictions 
model.save(sc, "model0") 

# Trying to load saved model and work with it 
model0 = MatrixFactorizationModel.load(sc, "model0") 
predictions0 = model0.predictAll(testdata).map(lambda r: ((r[0], r[1]), r[2])) 

后,我尝试使用model0我得到一个长回溯,这只能到此为止:

Py4JError: An error occurred while calling o70.predict. Trace: 
py4j.Py4JException: Method predict([class org.apache.spark.api.java.JavaRDD]) does not exist 
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:333) 
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:342) 
    at py4j.Gateway.invoke(Gateway.java:252) 
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:133) 
    at py4j.commands.CallCommand.execute(CallCommand.java:79) 
    at py4j.GatewayConnection.run(GatewayConnection.java:207) 
    at java.lang.Thread.run(Thread.java:745) 

所以我的问题是 - 我是做错了什么?据我调试,我的模型存储(本地和HDFS),他们包含许多文件与一些数据。我有一种感觉,模型保存正确,但可能他们没有正确加载。我也搜索了一下,但没有发现任何相关信息。

貌似这个保存\加载功能已被添加在最近因为这个星火1.3.0,我有另外一个问题 - 什么是推荐的方式发布1.3.0之前保存\负荷模型?我还没有找到任何好的方法来做到这一点,至少对于Python来说。我也试过味酸,但面临着同样的问题,这里描述Save Apache Spark mllib model in python

回答

2

我碰到这也是 - 它看起来像一个bug。我已经报告给spark jira

+0

十分感谢!看起来像这是一个Python绑定只有bug .. – artemdevel 2015-03-30 08:53:31

5

由于this pull request于2015年3月28日合并(一天后,你的问题是最后编辑)这个问题已经得到解决。

您只需要从GitHub(git clone git://github.com/apache/spark.git -b branch-1.3)克隆/获取最新版本,然后使用$ mvn -DskipTests clean package构建它(遵循spark/README.md中的说明)。

注:我遇到了麻烦建设星火因为Maven的是被靠不住的。我通过使用$ update-alternatives --config mvn并选择优先级为150的'路径'来解决该问题,无论这意味着什么。 Explanation here。保存模型

+0

是的,我见过这个公关,谢谢!但我还没有尝试过自己构建Spark。另外感谢Maven提供的技巧:) – artemdevel 2015-03-31 20:13:07

6

的一种方式(Scala中,但可能是在Python类似):

// persist model to HDFS 
sc.parallelize(Seq(model), 1).saveAsObjectFile("linReg.model") 
然后

保存的模型可以加载为:

val linRegModel = sc.objectFile[LinearRegressionModel]("linReg.model").first() 

也见相关question

欲了解更多详情,请参阅(ref

0

使用pi在ML中使用Peline来训练模型,然后使用MLWriter和MLReader保存模型并将其读回。

from pyspark.ml import Pipeline 
from pyspark.ml import PipelineModel 

pipeTrain.write().overwrite().save(outpath) 
model_in = PipelineModel.load(outpath) 
+1

谢谢,但这个问题是非常古老的:)很多事情从它被问到的时候已经改变。 – artemdevel 2017-10-13 17:39:01