为什么我的Spark Mlib ALS协作过滤训练模型这么慢?

问题描述 投票:0回答:1

我目前在我的App中将ALS协作过滤方法用于内容推荐系统。看起来工作正常,预测部分很快,但是训练模型部分需要20秒钟以上。我需要至少1秒或更短的时间,因为我需要几乎实时的建议。我目前使用3台机器的Spark集群,每个节点有17GB。我也使用datastax,但这不应该有任何影响。

我真的不知道为什么以及如何改善这一点?很高兴提出任何建议,谢谢。

这是基本的火花代码:

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

# Load and parse the data
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])))

此部分需要20秒钟以上,但只需要不到1秒。

# Build the recommendation model using Alternating Least Squares
rank = 10
numIterations = 10
model = ALS.train(ratings, rank, numIterations)

# Evaluate the model on training data
testdata = ratings.map(lambda p: (p[0], p[1]))
predictions = model.predictAll(testdata).map(lambda r: ((r[0], r[1]), r[2]))
ratesAndPreds = ratings.map(lambda r: ((r[0], r[1]), r[2])).join(predictions)
MSE = ratesAndPreds.map(lambda r: (r[1][0] - r[1][1])**2).mean()
print("Mean Squared Error = " + str(MSE))

# Save and load model
model.save(sc, "target/tmp/myCollaborativeFilter")
sameModel = MatrixFactorizationModel.load(sc, "target/tmp/myCollaborativeFilter")
apache-spark machine-learning collaborative-filtering
1个回答
0
投票

之所以需要时间的原因之一是因为RDD。对于RDD,没有特定的结构/架构。因此,这些趋向于有些缓慢。当调用ALS.train()时,RDD幕后发生的某些操作(例如,flatmap,count,map)将不得不考虑嵌套结构,因此很慢。

相反,您可以使用数据框而不是RDD尝试相同的操作。由于架构/类型是已知的,因此数据帧操作是最佳的。但是,要使ALS在数据帧上工作,您必须从“ ml.recommendation”导入ALS。我也遇到了同样的问题,当我尝试使用数据框而不是RDD时,它运行得很好。

您也可以在数据变得很大时尝试检查点。

© www.soinside.com 2019 - 2024. All rights reserved.