Search in sources :

Example 66 with Vector

use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.

the class LinearRegressionLSQRTrainer method extendLabeledVector.

/**
 * @param lb Label.
 */
private static LabeledVector<double[]> extendLabeledVector(LabeledVector<Double> lb) {
    double[] featuresArr = new double[lb.features().size() + 1];
    System.arraycopy(lb.features().asArray(), 0, featuresArr, 0, lb.features().size());
    featuresArr[featuresArr.length - 1] = 1.0;
    Vector features = VectorUtils.of(featuresArr);
    double[] lbl = new double[] { lb.label() };
    return features.labeled(lbl);
}
Also used : Vector(org.apache.ignite.ml.math.primitives.vector.Vector) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)

Example 67 with Vector

use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.

the class LinearRegressionSGDTrainer method restoreMLPState.

/**
 * @param mdl Model.
 * @return State of MLP from last learning.
 */
@NotNull
private MultilayerPerceptron restoreMLPState(LinearRegressionModel mdl) {
    Vector weights = mdl.weights();
    double intercept = mdl.intercept();
    MLPArchitecture architecture1 = new MLPArchitecture(weights.size());
    architecture1 = architecture1.withAddedLayer(1, true, Activators.LINEAR);
    MLPArchitecture architecture = architecture1;
    MultilayerPerceptron perceptron = new MultilayerPerceptron(architecture);
    Vector mlpState = weights.like(weights.size() + 1);
    weights.nonZeroes().forEach(ith -> mlpState.set(ith.index(), ith.get()));
    mlpState.set(mlpState.size() - 1, intercept);
    perceptron.setParameters(mlpState);
    return perceptron;
}
Also used : MultilayerPerceptron(org.apache.ignite.ml.nn.MultilayerPerceptron) MLPArchitecture(org.apache.ignite.ml.nn.architecture.MLPArchitecture) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) NotNull(org.jetbrains.annotations.NotNull)

Example 68 with Vector

use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.

the class LogisticRegressionSGDTrainer method restoreMLPState.

/**
 * @param mdl Model.
 * @return state of MLP from last learning.
 */
@NotNull
private MultilayerPerceptron restoreMLPState(LogisticRegressionModel mdl) {
    Vector weights = mdl.weights();
    double intercept = mdl.intercept();
    MLPArchitecture architecture1 = new MLPArchitecture(weights.size());
    architecture1 = architecture1.withAddedLayer(1, true, Activators.SIGMOID);
    MLPArchitecture architecture = architecture1;
    MultilayerPerceptron perceptron = new MultilayerPerceptron(architecture);
    Vector mlpState = weights.like(weights.size() + 1);
    weights.nonZeroes().forEach(ith -> mlpState.set(ith.index(), ith.get()));
    mlpState.set(mlpState.size() - 1, intercept);
    perceptron.setParameters(mlpState);
    return perceptron;
}
Also used : MultilayerPerceptron(org.apache.ignite.ml.nn.MultilayerPerceptron) MLPArchitecture(org.apache.ignite.ml.nn.architecture.MLPArchitecture) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) NotNull(org.jetbrains.annotations.NotNull)

Example 69 with Vector

use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.

the class RecommendationTrainer method sum.

/**
 * Returns sum of two matrices.
 *
 * @param a First matrix.
 * @param b Second matrix.
 * @param <T> Type of a key.
 * @return Sum of two matrices.
 */
private static <T> Map<T, Vector> sum(Map<T, Vector> a, Map<T, Vector> b) {
    if (a == null)
        return b;
    if (b == null)
        return a;
    Map<T, Vector> res = new HashMap<>();
    for (Map<T, Vector> map : Arrays.asList(a, b)) {
        for (Map.Entry<T, Vector> e : map.entrySet()) {
            Vector vector = res.get(e.getKey());
            res.put(e.getKey(), vector == null ? e.getValue() : e.getValue().plus(vector));
        }
    }
    return Collections.unmodifiableMap(res);
}
Also used : HashMap(java.util.HashMap) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) HashMap(java.util.HashMap) Map(java.util.Map)

Example 70 with Vector

use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.

the class RecommendationTrainer method calculateImprovement.

/**
 * Calculates improvement of the model that corresponds to the specified gradient (how significantly model will be
 * improved after the gradient application).
 *
 * @param grad Matrix factorization gradient.
 * @param <O> Type of an object.
 * @param <S> Type of a subject.
 * @return Measure of model improvement correspondent to the specified gradient.
 */
private <O extends Serializable, S extends Serializable> double calculateImprovement(MatrixFactorizationGradient<O, S> grad) {
    double mean = 0;
    for (Vector vector : grad.getObjGrad().values()) {
        for (int i = 0; i < vector.size(); i++) mean += Math.abs(vector.get(i));
    }
    for (Vector vector : grad.getSubjGrad().values()) {
        for (int i = 0; i < vector.size(); i++) mean += Math.abs(vector.get(i));
    }
    mean /= (grad.getSubjGrad().size() + grad.getObjGrad().size());
    return mean;
}
Also used : Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Aggregations

Vector (org.apache.ignite.ml.math.primitives.vector.Vector)265 DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)95 Test (org.junit.Test)94 Ignite (org.apache.ignite.Ignite)78 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)49 HashMap (java.util.HashMap)39 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)38 DummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer)26 FileNotFoundException (java.io.FileNotFoundException)22 TrainerTest (org.apache.ignite.ml.common.TrainerTest)22 DecisionTreeClassificationTrainer (org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer)21 DecisionTreeModel (org.apache.ignite.ml.tree.DecisionTreeModel)21 Serializable (java.io.Serializable)19 IgniteCache (org.apache.ignite.IgniteCache)18 EncoderTrainer (org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer)16 Cache (javax.cache.Cache)15 DoubleArrayVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer)15 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)14 ArrayList (java.util.ArrayList)12 ModelsComposition (org.apache.ignite.ml.composition.ModelsComposition)12