artemmalykh commented on a change in pull request #5767: [ML] IGNITE-10573: 
Consistent API for Ensemble training
URL: https://github.com/apache/ignite/pull/5767#discussion_r247901935
 
 

 ##########
 File path: 
modules/ml/src/main/java/org/apache/ignite/ml/composition/bagging/BaggedTrainer.java
 ##########
 @@ -0,0 +1,200 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.ml.composition.bagging;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Random;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import org.apache.ignite.ml.IgniteModel;
+import org.apache.ignite.ml.composition.CompositionUtils;
+import 
org.apache.ignite.ml.composition.combinators.parallel.TrainersParallelComposition;
+import 
org.apache.ignite.ml.composition.predictionsaggregator.PredictionsAggregator;
+import org.apache.ignite.ml.dataset.DatasetBuilder;
+import org.apache.ignite.ml.environment.LearningEnvironmentBuilder;
+import org.apache.ignite.ml.math.functions.IgniteBiFunction;
+import org.apache.ignite.ml.math.functions.IgniteFunction;
+import org.apache.ignite.ml.math.primitives.vector.Vector;
+import org.apache.ignite.ml.math.primitives.vector.VectorUtils;
+import org.apache.ignite.ml.trainers.AdaptableDatasetTrainer;
+import org.apache.ignite.ml.trainers.DatasetTrainer;
+import org.apache.ignite.ml.trainers.transformers.BaggingUpstreamTransformer;
+import org.apache.ignite.ml.util.Utils;
+
+/**
+ * Trainer encapsulating logic of bootstrap aggregating (bagging).
+ * This trainer accepts some other trainer and returns bagged version of it.
+ * Resulting model consists of submodels results of which are aggregated by a 
specified aggregator.
+ * <p>Bagging is done
+ * on both samples and features (<a 
href="https://en.wikipedia.org/wiki/Bootstrap_aggregating";></a>Samples 
bagging</a>,
+ * <a href="https://en.wikipedia.org/wiki/Random_subspace_method";></a>Features 
bagging</a>).</p>
+ *
+ * @param <M> Type of model produced by trainer for which bagged version is 
created.
+ * @param <L> Type of labels.
+ * @param <T> Type of trainer for which bagged version is created.
+ */
+public class BaggedTrainer<M extends IgniteModel<Vector, Double>, L, T extends 
DatasetTrainer<M, L>> extends
+    DatasetTrainer<BaggedModel, L> {
+    /** Trainer for which bagged version is created. */
+    private final DatasetTrainer<M, L> tr;
+
+    /** Aggregator of submodels results. */
+    private final PredictionsAggregator aggregator;
+
+    /** Count of submodels in the ensemble. */
+    private final int ensembleSize;
+
+    /** Ratio determining which part of dataset will be taken as subsample for 
each submodel training. */
+    private final double subsampleRatio;
+
+    /** Dimensionality of feature vectors. */
+    private final int featuresVectorSize;
+
+    /** Dimension of subspace on which all samples from subsample are 
projected. */
+    private final int featureSubspaceDim;
+
+    /**
+     * Construct instance of this class with given parameters.
+     *
+     * @param tr Trainer for making bagged.
+     * @param aggregator Aggregator of models.
+     * @param ensembleSize Size of ensemble.
+     * @param subsampleRatio Ratio (subsample size) / (initial dataset size).
+     * @param featuresVectorSize Dimensionality of feature vector.
+     * @param featureSubspaceDim Dimensionality of feature subspace.
+     */
+    public BaggedTrainer(DatasetTrainer<M, L> tr,
+        PredictionsAggregator aggregator, int ensembleSize, double 
subsampleRatio, int featuresVectorSize,
+        int featureSubspaceDim) {
+        this.tr = tr;
+        this.aggregator = aggregator;
+        this.ensembleSize = ensembleSize;
+        this.subsampleRatio = subsampleRatio;
+        this.featuresVectorSize = featuresVectorSize;
+        this.featureSubspaceDim = featureSubspaceDim;
+    }
+
+    /**
+     * Create trainer bagged trainer.
+     *
+     * @return Bagged trainer.
+     */
+    private DatasetTrainer<IgniteModel<Vector, Double>, L> getTrainer() {
+        List<int[]> mappings = (featuresVectorSize > 0 && featureSubspaceDim 
!= featuresVectorSize) ?
+            IntStream.range(0, ensembleSize).mapToObj(
+                modelIdx -> getMapping(
+                    featuresVectorSize,
+                    featureSubspaceDim,
+                    environment.randomNumbersGenerator().nextLong() + 
modelIdx))
 
 Review comment:
   Agree.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to