1

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

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

1.20455
0.1356
0.000456

截距为 0,因为我使用的是火车而不是设置截距,因此暂时可以忽略它。我现在想初始化一个新的模型对象并使用上述文件中保存的这些权重。我们正在使用 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)
}

任何指示我该怎么做?

4

1 回答 1

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))
于 2014-12-15T22:58:03.027 回答