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);
}
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;
}
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;
}
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);
}
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;
}
Aggregations