Spark MLlib:我应该在安装模型之前调用.cache吗?

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

想象一下,我正在训练Spark MLlib模型,如下所示:

val traingData = loadTrainingData(...)
val logisticRegression = new LogisticRegression()

traingData.cache
val logisticRegressionModel = logisticRegression.fit(trainingData)

呼叫traingData.cache是否在训练时提高了表现还是不需要?

ML算法的.fit(...)方法是否在内部调用cache / unpersist?

scala apache-spark apache-spark-mllib
1个回答
2
投票

没有必要为Spark LogisticRegression(以及其他一些模型)调用.cachetrain中的Predictor.fit(...)方法(由LogisticRegression调用)实现如下:

override protected[spark] def train(dataset: Dataset[_]): LogisticRegressionModel = {
  val handlePersistence = dataset.rdd.getStorageLevel == StorageLevel.NONE // true if not cached-persisted
  train(dataset, handlePersistence)
}

然后...

if (handlePersistence) instances.persist(StorageLevel.MEMORY_AND_DISK)

这通常比自定义调用.cache更有效,因为上面的行中的instances只包含(label, weight, features)而不包含其余数据。

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