Apache Ignite 更新先前训练的 ML 模型
Apache Ignite updating previously trained ML model
我有一个用于训练 KNN 模型的数据集。稍后我想用新的训练数据更新模型。我看到的是更新后的模型只接受新的训练数据而忽略了之前训练的数据。
Vectorizer vec = new DummyVectorizer<Integer>(1, 2).labeled(0);
DatasetTrainer<KNNClassificationModel, Double> trainer = new KNNClassificationTrainer();
KNNClassificationModel model;
KNNClassificationModel modelUpdated;
Map<Integer, Vector> trainingData = new HashMap<Integer, Vector>();
Map<Integer, Vector> trainingDataNew = new HashMap<Integer, Vector>();
Double[][] data1 = new Double[][] {
{0.136,0.644,0.154},
{0.302,0.634,0.779},
{0.806,0.254,0.211},
{0.241,0.951,0.744},
{0.542,0.893,0.612},
{0.334,0.277,0.486},
{0.616,0.259,0.121},
{0.738,0.585,0.017},
{0.124,0.567,0.358},
{0.934,0.346,0.863}};
Double[][] data2 = new Double[][] {
{0.300,0.236,0.193}};
Double[] observationData = new Double[] { 0.8, 0.7 };
// fill dataset (in cache)
for (int i = 0; i < data1.length; i++)
trainingData.put(i, new DenseVector(data1[i]));
// first training / prediction
model = trainer.fit(trainingData, 1, vec);
System.out.println("First prediction : " + model.predict(new DenseVector(observationData)));
// new training data
for (int i = 0; i < data2.length; i++)
trainingDataNew.put(data1.length + i, new DenseVector(data2[i]));
// second training / prediction
modelUpdated = trainer.update(model, trainingDataNew, 1, vec);
System.out.println("Second prediction: " + modelUpdated.predict(new DenseVector(observationData)));
作为输出我得到这个:
First prediction : 0.124
Second prediction: 0.3
这看起来像第二个预测只使用了数据 2,它必须导致 0.3 作为预测。
模型更新如何工作?如果我必须将 data2 添加到 data1,然后再次对 data1 进行训练,与对所有组合数据进行全新训练相比,会有什么不同?
模型更新如何工作?
特别针对 KNN:
将 data2 添加到 data1 并在组合数据上调用 modelUpdate。
按照该测试中的说明进行操作:
设置你的教练:
KNNClassificationTrainer trainer = new KNNClassificationTrainer()
.withK(3)
.withDistanceMeasure(new EuclideanDistance())
.withWeighted(false);
然后设置矢量化器:(注意标记坐标的创建方式)
model = trainer.fit(
trainingData,
parts,
new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST)
);
然后根据需要调用 updateModel。
KNNClassificationModel updatedOnData = trainer.update(
originalMdlOnEmptyDataset,
newData,
parts,
new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST)
);
KNN 分类文档:https://ignite.apache.org/docs/latest/machine-learning/binary-classification/knn-classification
我有一个用于训练 KNN 模型的数据集。稍后我想用新的训练数据更新模型。我看到的是更新后的模型只接受新的训练数据而忽略了之前训练的数据。
Vectorizer vec = new DummyVectorizer<Integer>(1, 2).labeled(0);
DatasetTrainer<KNNClassificationModel, Double> trainer = new KNNClassificationTrainer();
KNNClassificationModel model;
KNNClassificationModel modelUpdated;
Map<Integer, Vector> trainingData = new HashMap<Integer, Vector>();
Map<Integer, Vector> trainingDataNew = new HashMap<Integer, Vector>();
Double[][] data1 = new Double[][] {
{0.136,0.644,0.154},
{0.302,0.634,0.779},
{0.806,0.254,0.211},
{0.241,0.951,0.744},
{0.542,0.893,0.612},
{0.334,0.277,0.486},
{0.616,0.259,0.121},
{0.738,0.585,0.017},
{0.124,0.567,0.358},
{0.934,0.346,0.863}};
Double[][] data2 = new Double[][] {
{0.300,0.236,0.193}};
Double[] observationData = new Double[] { 0.8, 0.7 };
// fill dataset (in cache)
for (int i = 0; i < data1.length; i++)
trainingData.put(i, new DenseVector(data1[i]));
// first training / prediction
model = trainer.fit(trainingData, 1, vec);
System.out.println("First prediction : " + model.predict(new DenseVector(observationData)));
// new training data
for (int i = 0; i < data2.length; i++)
trainingDataNew.put(data1.length + i, new DenseVector(data2[i]));
// second training / prediction
modelUpdated = trainer.update(model, trainingDataNew, 1, vec);
System.out.println("Second prediction: " + modelUpdated.predict(new DenseVector(observationData)));
作为输出我得到这个:
First prediction : 0.124
Second prediction: 0.3
这看起来像第二个预测只使用了数据 2,它必须导致 0.3 作为预测。
模型更新如何工作?如果我必须将 data2 添加到 data1,然后再次对 data1 进行训练,与对所有组合数据进行全新训练相比,会有什么不同?
模型更新如何工作?
特别针对 KNN:
将 data2 添加到 data1 并在组合数据上调用 modelUpdate。
按照该测试中的说明进行操作: 设置你的教练:
KNNClassificationTrainer trainer = new KNNClassificationTrainer()
.withK(3)
.withDistanceMeasure(new EuclideanDistance())
.withWeighted(false);
然后设置矢量化器:(注意标记坐标的创建方式)
model = trainer.fit(
trainingData,
parts,
new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST)
);
然后根据需要调用 updateModel。
KNNClassificationModel updatedOnData = trainer.update(
originalMdlOnEmptyDataset,
newData,
parts,
new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST)
);
KNN 分类文档:https://ignite.apache.org/docs/latest/machine-learning/binary-classification/knn-classification