【问题标题】:MLLIb: Saving and loading a modelMLLIb:保存和加载模型
【发布时间】:2016-06-23 23:28:20
【问题描述】:

我正在使用LinearRegressionWithSGD,然后我保存模型权重并截取。

包含权重的文件具有以下格式:

1.20455
0.1356
0.000456

Intercept 为 0,因为我使用的是 train 而不是设置截距,因此暂时可以忽略它。我现在想初始化一个新的模型对象并使用上述文件中保存的这些权重。我们正在使用 CDH 5.1

类似的东西:

// Here is the code the load data and train the model on it.
val weights = sc.textFile("linear-weights");
val model = new LinearRegressionWithSGD(weights);

然后使用如下:

// Here is where I want to use the trained model to predict on new data.
val valuesAndPreds = testData.map { point =>
  // Predicting on new data.
  val prediction = model.predict(point.features)
  (point.label, prediction)
}

任何关于我如何做到这一点的指针?

【问题讨论】:

    标签: scala apache-spark apache-spark-mllib


    【解决方案1】:

    您似乎正在复制 LinearRegressionWithSGD 的训练部分 - 它以 LibSVM 文件作为输入。

    • 您确定要提供自己的权重,而不是让库在训练阶段完成其工作吗?
    • 如果是这样,那么您可以创建自己的 LinearRegressionWithSGD 并覆盖 createModel

    如果您已经计算了所需的权重/以自己的方式进行了训练,以下是您的步骤:

     // Stick in your weights below ..
    var model = algorithm.createModel(weights, 0.0)
    
    // Now you can run the last steps of the 'normal' process
    val prediction = model.predict(test.map(_.features))
    val predictionAndLabel = prediction.zip(test.map(_.label))
    

    顺便说一句,这里的参考是更“标准”的方法,包括训练步骤:

    val data = MLUtils.loadLibSVMFile(sc, inputFile).cache()
    
    val splits = examples.randomSplit(Array(0.8, 0.2))
    val training = splits(0).cache()
    val test = splits(1).cache()
    
    val updater = params.regType match {
      case NONE => new SimpleUpdater()
      case L1 => new L1Updater()
      case L2 => new SquaredL2Updater()
    }
    
    val algorithm = new LinearRegressionWithSGD()
    
    val algorithm = new LinearRegressionWithSGD()
    algorithm.optimizer
      .setNumIterations(params.numIterations)
      .setStepSize(params.stepSize)
      .setUpdater(updater)
      .setRegParam(params.regParam)
    
    val model = algorithm.run(training)
    
    val prediction = model.predict(test.map(_.features))
    val predictionAndLabel = prediction.zip(test.map(_.label))
    

    【讨论】:

      猜你喜欢
      • 2019-12-06
      • 2016-03-20
      • 2016-11-15
      • 2021-12-15
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      相关资源
      最近更新 更多