【问题标题】:Apache Ignite updating previously trained ML modelApache Ignite 更新先前训练的 ML 模型
【发布时间】:2021-03-02 01:52:46
【问题描述】:

我有一个用于训练 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 上进行训练,那么与对所有组合数据进行全新训练相比会有什么不同?

【问题讨论】:

    标签: java machine-learning ignite


    【解决方案1】:

    模型更新如何工作?
    特别针对 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

    【讨论】:

    • 你看我的代码了吗?我做的和github上的测试差不多。至少这在我看来是这样。实际上,即使行为是相同的,因为测试首先使用空 HashMap 进行训练,这导致与我看到的相同的行为,即只使用了第二个训练数据。如果我需要将 data2 添加到 data1 然后调用 modelUpdate 这有什么意义,因为我基本上对所有数据再次进行了完整的训练。还是我错过了什么?
    • 我同意,updateModel(用于 knn)需要修复。检查实现:github.com/apache/ignite/blob/… 如您所见,未使用 mdl 参数,仅使用新数据创建 LocalDataSet。我用几个例子验证了它。
    • 好的。我会向项目报告。
    猜你喜欢
    • 1970-01-01
    • 2017-12-31
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 2019-07-19
    • 2021-11-01
    相关资源
    最近更新 更多