当我发现ALS中的因子矩阵是随机初始化的,所以不同的运行会给出稍微不同的结果,使用它们的平均值可以得到更准确的结果,因此需要创建一个小的Pyspark推荐系统的集合。所以我对模型进行了2次训练->它给出了不同的模型ALS对象,但是当使用recommendForAllUsers()方法时,对于不同的模型给出了相同的推荐输出。这里有什么问题,为什么需要重新启动脚本才能得到不同的输出,即使有不同的预测ALS模型?
不存在伪随机的P.S种子参数。
def __train_model(ratings):
"""Train the ALS model with the current dataset
"""
logger.info("Training the ALS model...")
als = ALS(rank=rank, maxIter=iterations, implicitPrefs=True, regParam=regularization_parameter,
userCol="order_id", itemCol="product_id", ratingCol="count")
model = als.fit(ratings)
logger.info("ALS model built!")
return model
model1 = __train_model(ratings_DF)
print(model1)
sim_table_1 = model1.recommendForAllUsers(100).toPandas()
model2 = __train_model(ratings_DF)
print(model2)
sim_table_2 = model2.recommendForAllUsers(100).toPandas()
print('Equality of objects:', model1 == model2)输出
INFO:__main__:Training the ALS model...
INFO:__main__:ALS model built!
ALS_444a9e62eb6938248b4c
INFO:__main__:Training the ALS model...
INFO:__main__:ALS model built!
ALS_465c95728272696c6c67
Equality of objects: False发布于 2018-06-15 23:14:03
如果在实例化ALS实例时不为seed参数提供值,则每次都会默认为相同的值,因为它是字符串的散列("ALS")。这就是为什么你的建议总是一样的。
设置种子默认值的代码:
self._setDefault(seed=hash(type(self).__name__))
示例:
from pyspark.ml.recommendation import ALS
als1 = ALS(rank=10, maxIter=5)
als2 = ALS(rank=10, maxIter=5)
als1.getSeed() == als2.getSeed() == hash("ALS")
>>> True如果每次都想得到不同的模型,可以使用类似numpy.random.randint的方法为种子生成一个随机整数。
https://stackoverflow.com/questions/50391627
复制相似问题