Search in sources :

Example 1 with Vector

use of com.alibaba.alink.common.linalg.Vector in project Alink by alibaba.

the class DeepARModelMapper method predictMultiVar.

@Override
protected Tuple2<Vector[], String> predictMultiVar(Timestamp[] historyTimes, Vector[] historyVals, int predictNum) {
    Timestamp[] predictTimes = TimeSeriesMapper.getPredictTimes(historyTimes, predictNum);
    int window = historyVals.length;
    int series = 0;
    DenseVector[] vectors = new DenseVector[historyVals.length];
    for (int i = 0; i < window; ++i) {
        vectors[i] = VectorUtil.getDenseVector(historyVals[i]);
        if (vectors[i] == null) {
            throw new IllegalArgumentException("history values should not be null.");
        }
        series = vectors[i].size();
    }
    FloatTensor[][] tensors = new FloatTensor[series][window];
    for (int i = 0; i < series; ++i) {
        tensors[i][0] = Tensor.cat(new FloatTensor[] { new FloatTensor(new float[] { 0.0f }), DeepARFeaturesGenerator.generateFromFrequency(calendar.get(), unit, historyTimes[0]) }, -1, null);
        for (int j = 1; j < window; ++j) {
            tensors[i][j] = Tensor.cat(new FloatTensor[] { new FloatTensor(new float[] { (float) vectors[j - 1].get(i) }), DeepARFeaturesGenerator.generateFromFrequency(calendar.get(), unit, historyTimes[j]) }, -1, null);
        }
    }
    FloatTensor[] batch = new FloatTensor[series];
    for (int i = 0; i < series; ++i) {
        batch[i] = Tensor.stack(tensors[i], 0, null);
    }
    Vector[] result = new Vector[predictNum];
    Row[] sigmas = new Row[predictNum];
    for (int i = 0; i < predictNum; ++i) {
        result[i] = new DenseVector(series);
        sigmas[i] = Row.of(new DenseVector(series));
    }
    for (int i = 0; i < series; ++i) {
        float mu = (float) historyVals[window - 1].get(i);
        FloatTensor v = new FloatTensor(new float[] { 0.0f, 0.0f });
        int nonZero = 0;
        for (int j = 0; j < window; ++j) {
            float cell = batch[i].getFloat(j, 0);
            if (cell != 0) {
                nonZero += 1;
            }
            v.setFloat(v.getFloat(0) + cell, 0);
        }
        if (mu != 0) {
            nonZero += 1;
            v.setFloat(v.getFloat(0) + mu, 0);
        }
        if (nonZero == 0) {
            continue;
        }
        v.setFloat(v.getFloat(0) / nonZero + 1.0f, 0);
        for (int j = 0; j < window; ++j) {
            batch[i].setFloat(batch[i].getFloat(j, 0) / v.getFloat(0), j, 0);
        }
        mu = mu / v.getFloat(0);
        for (int j = 0; j < predictNum; ++j) {
            batch[i] = Tensor.cat(new FloatTensor[] { batch[i], Tensor.stack(new FloatTensor[] { Tensor.cat(new FloatTensor[] { new FloatTensor(new float[] { mu }), DeepARFeaturesGenerator.generateFromFrequency(calendar.get(), unit, predictTimes[j]) }, -1, null) }, 0, null) }, 0, null);
            FloatTensor pred;
            try {
                pred = (FloatTensor) tfTableModelPredictModelMapper.map(Row.of(batch[i])).getField(0);
            } catch (Exception e) {
                return Tuple2.of(null, null);
            }
            mu = pred.getFloat(window + j, 0);
            float sigma = pred.getFloat(window + j, 1);
            result[j].set(i, mu * v.getFloat(0) + v.getFloat(1));
            ((Vector) (sigmas[j].getField(0))).set(i, sigma * v.getFloat(0));
        }
    }
    return Tuple2.of(result, new MTable(Arrays.asList(sigmas), new String[] { "sigma" }, new TypeInformation<?>[] { VectorTypes.DENSE_VECTOR }).toString());
}
Also used : Timestamp(java.sql.Timestamp) MTable(com.alibaba.alink.common.MTable) FloatTensor(com.alibaba.alink.common.linalg.tensor.FloatTensor) Row(org.apache.flink.types.Row) Vector(com.alibaba.alink.common.linalg.Vector) DenseVector(com.alibaba.alink.common.linalg.DenseVector) DenseVector(com.alibaba.alink.common.linalg.DenseVector)

Example 2 with Vector

use of com.alibaba.alink.common.linalg.Vector in project Alink by alibaba.

the class LSTNetModelMapper method predictMultiVar.

@Override
protected Tuple2<Vector[], String> predictMultiVar(Timestamp[] historyTimes, Vector[] historyVals, int predictNum) {
    Tuple2<Timestamp[], FloatTensor> t = toTensor(historyTimes, historyVals);
    FloatTensor pred = null;
    try {
        pred = (FloatTensor) tfTableModelPredictModelMapper.map(Row.of(t.f1)).getField(0);
    } catch (Exception ex) {
    // pass
    }
    return pred == null ? Tuple2.of(null, null) : Tuple2.of(new Vector[] { DoubleTensor.of(pred).toVector() }, null);
}
Also used : FloatTensor(com.alibaba.alink.common.linalg.tensor.FloatTensor) Vector(com.alibaba.alink.common.linalg.Vector)

Example 3 with Vector

use of com.alibaba.alink.common.linalg.Vector in project Alink by alibaba.

the class ClusterEvaluationUtil method getClusterStatistics.

public static ClusterMetricsSummary getClusterStatistics(Iterable<Tuple2<Vector, String>> rows, ContinuousDistance distance, Tuple3<String, DenseVector, DenseVector> meanAndSum) {
    int total = 0;
    String clusterId = meanAndSum.f0;
    DenseVector meanVector = meanAndSum.f1;
    DenseVector sumVector = meanAndSum.f2;
    double distanceSum = 0.0;
    double distanceSquareSum = 0.0;
    double vectorNormL2Sum = 0.0;
    for (Tuple2<Vector, String> t : rows) {
        double d = distance.calc(meanVector, t.f0);
        distanceSum += d;
        distanceSquareSum += d * d;
        vectorNormL2Sum += t.f0.normL2Square();
        total++;
    }
    return new ClusterMetricsSummary(clusterId, total, distanceSum / total, distanceSquareSum, vectorNormL2Sum, meanVector, distance, sumVector);
}
Also used : Vector(com.alibaba.alink.common.linalg.Vector) DenseVector(com.alibaba.alink.common.linalg.DenseVector) DenseVector(com.alibaba.alink.common.linalg.DenseVector)

Example 4 with Vector

use of com.alibaba.alink.common.linalg.Vector in project Alink by alibaba.

the class ClusterEvaluationUtil method calMeanAndSum.

public static Tuple3<String, DenseVector, DenseVector> calMeanAndSum(Iterable<Tuple2<Vector, String>> rows, int vectorSize, FastDistance distance) {
    int total = 0;
    String clusterId = null;
    DenseVector sumVector = DenseVector.zeros(vectorSize);
    for (Tuple2<Vector, String> t : rows) {
        if (null == clusterId) {
            clusterId = t.f1;
        }
        Vector vec = t.f0;
        if (distance instanceof EuclideanDistance) {
            sumVector.plusEqual(vec);
        } else {
            vec.scaleEqual(1.0 / vec.normL2());
            sumVector.plusEqual(vec);
        }
        total++;
    }
    DenseVector meanVector = sumVector.scale(1.0 / total);
    if (distance instanceof CosineDistance) {
        meanVector.scaleEqual(1.0 / meanVector.normL2());
    }
    return Tuple3.of(clusterId, meanVector, sumVector);
}
Also used : EuclideanDistance(com.alibaba.alink.operator.common.distance.EuclideanDistance) Vector(com.alibaba.alink.common.linalg.Vector) DenseVector(com.alibaba.alink.common.linalg.DenseVector) DenseVector(com.alibaba.alink.common.linalg.DenseVector) CosineDistance(com.alibaba.alink.operator.common.distance.CosineDistance)

Example 5 with Vector

use of com.alibaba.alink.common.linalg.Vector in project Alink by alibaba.

the class ManHattanDistance method calc.

@Override
void calc(FastDistanceVectorData vector, FastDistanceMatrixData matrix, double[] res) {
    Vector vec = vector.getVector();
    if (vec instanceof DenseVector) {
        double[] vecData = ((DenseVector) vec).getData();
        double[] matrixData = matrix.getVectors().getData();
        int vectorSize = vecData.length;
        for (int i = 0; i < matrix.getVectors().numCols(); i++) {
            res[i] = calc(vecData, 0, matrixData, i * vectorSize, vectorSize);
        }
    } else {
        int[] indices = ((SparseVector) vec).getIndices();
        double[] values = ((SparseVector) vec).getValues();
        DenseMatrix denseMatrix = matrix.getVectors();
        double[] matrixData = denseMatrix.getData();
        int cnt = 0;
        for (int i = 0; i < denseMatrix.numCols(); i++) {
            int p1 = 0;
            for (int j = 0; j < denseMatrix.numRows(); j++) {
                if (p1 < indices.length && indices[p1] == j) {
                    res[i] += Math.abs(values[p1] - matrixData[cnt++]);
                    p1++;
                } else {
                    res[i] += Math.abs(matrixData[cnt++]);
                }
            }
        }
    }
}
Also used : SparseVector(com.alibaba.alink.common.linalg.SparseVector) Vector(com.alibaba.alink.common.linalg.Vector) DenseVector(com.alibaba.alink.common.linalg.DenseVector) SparseVector(com.alibaba.alink.common.linalg.SparseVector) DenseVector(com.alibaba.alink.common.linalg.DenseVector) DenseMatrix(com.alibaba.alink.common.linalg.DenseMatrix)

Aggregations

Vector (com.alibaba.alink.common.linalg.Vector)116 DenseVector (com.alibaba.alink.common.linalg.DenseVector)87 SparseVector (com.alibaba.alink.common.linalg.SparseVector)72 Test (org.junit.Test)41 Row (org.apache.flink.types.Row)35 ArrayList (java.util.ArrayList)26 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)22 DenseMatrix (com.alibaba.alink.common.linalg.DenseMatrix)20 List (java.util.List)20 HashMap (java.util.HashMap)18 BatchOperator (com.alibaba.alink.operator.batch.BatchOperator)17 DataSet (org.apache.flink.api.java.DataSet)13 BaseVectorSummary (com.alibaba.alink.operator.common.statistics.basicstatistic.BaseVectorSummary)11 Configuration (org.apache.flink.configuration.Configuration)9 Collector (org.apache.flink.util.Collector)9 FastDistanceVectorData (com.alibaba.alink.operator.common.distance.FastDistanceVectorData)8 RichMapFunction (org.apache.flink.api.common.functions.RichMapFunction)8 EuclideanDistance (com.alibaba.alink.operator.common.distance.EuclideanDistance)6 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)6 AllReduce (com.alibaba.alink.common.comqueue.communication.AllReduce)5