【问题标题】:Cannot change storage level of RDD无法更改 RDD 的存储级别
【发布时间】:2016-06-10 14:16:47
【问题描述】:

以下 Spark 代码:

val model = ALS.trainImplicit(ratings = ratingsRDD,
                              rank = rank,
                              iterations = numIterations,
                              lambda = lambda,
                              alpha = alpha)  

model.productFeatures.cache()

val modelSubsetRDD = new MatrixFactorizationModel(
  rank = rank,
  userFeatures = model.productFeatures,
  productFeatures = model.productFeatures)

引发以下异常:

在 RDD 已经分配了一个之后,不能更改它的存储级别 等级

StorageLevel.MEMORY_ONLY 引发了相同的异常。

另一方面,以下代码可以正常工作:

    val model = ALS.trainImplicit(ratings = ratingsRDD,
                              rank = rank,
                              iterations = numIterations,
                              lambda = lambda,
                              alpha = alpha)  
    val modelSubsetRDD = new MatrixFactorizationModel(
      rank = rank,
      userFeatures = model.userFeatures,
      productFeatures = model.productFeatures)

    model.userFeatures.persist(StorageLevel.MEMORY_ONLY)
    model.productFeatures.persist(StorageLevel.MEMORY_ONLY)

注意到这次userFeaturesproductFeatures 被设置为模型的两个不同成员。但是,我不确定为什么会这样。

【问题讨论】:

  • 如果您仍有问题,请更新您的问题。如果以下答案解决了您的问题,请将其标记为已接受。

标签: scala apache-spark machine-learning recommendation-engine


【解决方案1】:

您可能从代码中的其他地方获得了一些持久性?在返回模型之前不确定ALS.trainImplicit 在做什么。

调用cache() 会将RDD 存储在MEMORY_ONLY 中,而调用persist 允许您更改缓存类型。所以我猜这个 RDD 已经在其他地方持久化了,你正试图用cache() 重新持久化它,这就是问题所在。但是,使用persist 更改持久性类型是完全可以接受的。

编辑:

试试下面的代码:

val model = ALS.trainImplicit(ratings = ratingsRDD,
                              rank = rank,
                              iterations = numIterations,
                              lambda = lambda,
                              alpha = alpha)  
if(model.productFeatures.getStorageLevel() == StorageLevel.NONE)
    model.productFeatures.cache()

val modelSubsetRDD = new MatrixFactorizationModel(
  rank = rank,
  userFeatures = model.productFeatures,
  productFeatures = model.productFeatures)

这应该避免您尝试缓存已经缓存的内容(在内存或磁盘中)。

【讨论】:

  • 我遇到了同样的异常。奇怪的是,最后一段代码有效。
  • MatrixFactorizationModel 是否有任何不持久性?真的很难帮助两个基本上是黑盒子的类。
  • 是的,但我在两种情况下都使用相同的代码。唯一的区别是,在一种情况下,我使用 productFeatures 两次..
  • 这不是唯一的区别。当您调用 MatrixFactorizationModel 时,您的持久化/缓存调用也会被反转。如果我的猜测是正确的,那么如果您使用第二个代码示例并将持久调用移动到更高的位置,您也会得到一个异常。
  • 您可以查看this thread,如果还没有,则仅保留RDD。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多