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

LibSVM ファイルを入力として受け取る LinearRegressionWithSGD のトレーニング部分を複製しているようです。

  • トレーニング段階でライブラリにその仕事をさせるのではなく、独自の重みを提供したいと確信していますか?
  • その場合、独自の 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 に答える