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_r247890183
 
 

 ##########
 File path: 
modules/ml/src/main/java/org/apache/ignite/ml/composition/combinators/parallel/TrainersParallelComposition.java
 ##########
 @@ -0,0 +1,113 @@
+/*
+ * 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.combinators.parallel;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.ignite.ml.IgniteModel;
+import org.apache.ignite.ml.composition.CompositionUtils;
+import org.apache.ignite.ml.dataset.DatasetBuilder;
+import org.apache.ignite.ml.environment.parallelism.Promise;
+import org.apache.ignite.ml.math.functions.IgniteBiFunction;
+import org.apache.ignite.ml.math.functions.IgniteSupplier;
+import org.apache.ignite.ml.math.primitives.vector.Vector;
+import org.apache.ignite.ml.trainers.DatasetTrainer;
+
+/**
+ * This class represents a parallel composition of trainers.
+ * Parallel composition of trainers is a trainer itself which trains a list of 
trainers with same
+ * input and output. Training is done in following manner:
+ * <pre>
+ *     1. Independently train all trainers on the same dataset and get a list 
of models.
+ *     2. Combine models produced in step (1) into a {@link 
ModelsParallelComposition}.
+ * </pre>
+ * Updating is made in a similar fashion.
+ * Like in other trainers combinators we avoid to include type of contained 
trainers in type parameters
+ * because otherwise compositions of compositions would have a relatively 
complex generic type which will
+ * reduce readability.
+ *
+ * @param <I> Type of trainers inputs.
+ * @param <O> Type of trainers outputs.
+ * @param <L> Type of dataset labels.
+ */
+public class TrainersParallelComposition<I, O, L> extends 
DatasetTrainer<IgniteModel<I, List<O>>, L> {
+    /** List of trainers. */
+    private final List<DatasetTrainer<IgniteModel<I, O>, L>> trainers;
+
+    /**
+     * Construct an instance of this class from a list of trainers.
+     *
+     * @param trainers Trainers.
+     * @param <M> Type of mode
+     * @param <T>
+     */
+    public <M extends IgniteModel<I, O>, T extends DatasetTrainer<? extends 
IgniteModel<I, O>, L>> TrainersParallelComposition(
+        List<T> trainers) {
+        this.trainers = 
trainers.stream().map(CompositionUtils::unsafeCoerce).collect(Collectors.toList());
+    }
+
+    public static <I, O, M extends IgniteModel<I, O>, T extends 
DatasetTrainer<M, L>, L> TrainersParallelComposition<I, O, L> of(List<T> 
trainers) {
+        List<DatasetTrainer<IgniteModel<I, O>, L>> trs =
+            
trainers.stream().map(CompositionUtils::unsafeCoerce).collect(Collectors.toList());
+
+        return new TrainersParallelComposition<>(trs);
+    }
+
+    /** {@inheritDoc} */
+    @Override public <K, V> IgniteModel<I, List<O>> fit(DatasetBuilder<K, V> 
datasetBuilder,
+        IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, 
V, L> lbExtractor) {
+        List<IgniteSupplier<IgniteModel<I, O>>> tasks = trainers.stream()
+            .map(tr -> (IgniteSupplier<IgniteModel<I, O>>)(() -> 
tr.fit(datasetBuilder, featureExtractor, lbExtractor)))
+            .collect(Collectors.toList());
+
+        List<IgniteModel<I, O>> mdls = 
environment.parallelismStrategy().submit(tasks).stream()
+            .map(Promise::unsafeGet)
+            .collect(Collectors.toList());
+
+        return new ModelsParallelComposition<>(mdls);
+    }
+
+    /** {@inheritDoc} */
+    @Override public <K, V> IgniteModel<I, List<O>> update(IgniteModel<I, 
List<O>> mdl, DatasetBuilder<K, V> datasetBuilder,
+        IgniteBiFunction<K, V, Vector> featureExtractor, IgniteBiFunction<K, 
V, L> lbExtractor) {
+        // Unsafe.
+        ModelsParallelComposition<I, O> typedMdl = 
(ModelsParallelComposition<I, O>)mdl;
+
+        assert typedMdl.submodels().size() == trainers.size();
+        List<IgniteModel<I, O>> mdls = new ArrayList<>();
+
+        for (int i = 0; i < trainers.size(); i++)
+            mdls.add(trainers.get(i).update(typedMdl.submodels().get(i), 
datasetBuilder, featureExtractor, lbExtractor));
+
+        return new ModelsParallelComposition<>(mdls);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean checkState(IgniteModel<I, List<O>> mdl) {
+        // Never called.
+        throw new IllegalStateException();
+    }
+
+    /** {@inheritDoc} */
 
 Review comment:
   Fixed.

----------------------------------------------------------------
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