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

Spark MLlib: Should I call .cache before fitting a model?

假设我正在训练一个 Spark MLlib 模型,如下所示:

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

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

调用 traingData.cache 会提高训练时的性能还是不需要?

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

无需为 Spark LogisticRegression(和其他一些模型)调用 .cacheLogisticRegression中的train方法(被Predictor.fit(...)调用)实现如下:

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) 而不是其余数据。