apacheignite更新以前训练过的ml模型

ws51t4hk  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(378)

我有一个用于训练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

这看起来像是第二个预测只使用了data2,它必须导致0.3作为预测。
模型更新是如何工作的?如果我必须将data2添加到data1中,然后再次对data1进行训练,那么与对所有组合数据进行的全新训练相比,有什么区别呢?

mspsb9vt

mspsb9vt1#

模型更新是如何工作的?
特别是对于knn:将data2添加到data1,并对组合数据调用modelupdate。
以该测试为例:https://github.com/apache/ignite/blob/635dafb7742673494efa6e8e91e236820156d38f/modules/ml/src/test/java/org/apache/ignite/ml/knn/knnclassificationtest.java#l167
按照测试中的说明操作:设置培训师:

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分类示例:https://github.com/apache/ignite/blob/master/examples/src/main/java/org/apache/ignite/examples/ml/knn/knnclassificationexample.java

相关问题