Search in sources :

Example 1 with TAbstractVector

use of com.tencent.angel.ml.math.TAbstractVector in project angel by Tencent.

the class L2LogLossTest method testLoss2.

@Test
public void testLoss2() throws Exception {
    double[] data1 = { 1.0, 2.0 };
    double[] data2 = { 2.0, 1.0 };
    DenseDoubleVector denseDoubleVector1 = new DenseDoubleVector(2, data1);
    DenseDoubleVector denseDoubleVector2 = new DenseDoubleVector(2, data1);
    DenseDoubleVector w = new DenseDoubleVector(2, data2);
    TAbstractVector[] xList = new TAbstractVector[2];
    xList[0] = denseDoubleVector1;
    xList[1] = denseDoubleVector2;
    double[] yList = new double[2];
    yList[0] = 0;
    yList[1] = 1;
    double test = l2LogLoss.loss(xList, yList, w, 2);
    assertEquals(0.736297, test, 0.00001);
}
Also used : DenseDoubleVector(com.tencent.angel.ml.math.vector.DenseDoubleVector) TAbstractVector(com.tencent.angel.ml.math.TAbstractVector) Test(org.junit.Test) MathsTest(com.tencent.angel.ml.utils.MathsTest)

Example 2 with TAbstractVector

use of com.tencent.angel.ml.math.TAbstractVector in project angel by Tencent.

the class DenseDoubleVectorTest method dotSparseDummyVector.

@Test
public void dotSparseDummyVector() throws Exception {
    int dim = 1000;
    Random random = new Random(System.currentTimeMillis());
    double[] values = new double[dim];
    for (int i = 0; i < dim; i++) {
        values[i] = random.nextDouble();
    }
    DenseDoubleVector vec = new DenseDoubleVector(dim, values);
    TAbstractVector vec_1 = new SparseDummyVector(dim);
    int nnz = random.nextInt(dim);
    int[] indexs = genIndexes(dim, nnz);
    for (int idx : indexs) ((SparseDummyVector) vec_1).set(idx, 1);
    double sum = 0.0;
    for (int idx : indexs) sum += vec.get(idx);
    assertEquals(sum, vec.dot(vec_1));
}
Also used : Random(java.util.Random) TAbstractVector(com.tencent.angel.ml.math.TAbstractVector) Test(org.junit.Test)

Example 3 with TAbstractVector

use of com.tencent.angel.ml.math.TAbstractVector in project angel by Tencent.

the class DenseFloatVectorTest method plusTest.

@Test
public void plusTest() throws Exception {
    DenseFloatVector vec_1 = genDenseFloatVector(dim);
    TAbstractVector vec_2 = genDenseFloatVector(dim);
    TAbstractVector vec_3 = genDenseFloatVector(dim);
    TFloatVector vec_4 = (TFloatVector) vec_1.plus(vec_2);
    for (int i = 0; i < dim; i++) assertEquals(vec_1.get(i) + ((DenseFloatVector) vec_2).get(i), vec_4.get(i));
    TFloatVector vec_5 = (TFloatVector) vec_1.plus(vec_3);
    for (int i = 0; i < dim; i++) assertEquals(vec_1.get(i) + (float) ((DenseFloatVector) vec_3).get(i), vec_5.get(i));
    TFloatVector vec_6 = (TFloatVector) vec_1.plus(vec_2, 2.0f);
    for (int i = 0; i < dim; i++) assertEquals(vec_1.get(i) + 2.0f * ((DenseFloatVector) vec_2).get(i), vec_6.get(i));
    TFloatVector vec_7 = (TFloatVector) vec_1.plus(vec_3, 2.0);
    for (int i = 0; i < dim; i++) assertEquals(vec_1.get(i) + 2.0f * (float) ((DenseFloatVector) vec_3).get(i), vec_7.get(i));
    TFloatVector vec_8 = (TFloatVector) vec_1.plus(vec_3, 2);
    for (int i = 0; i < dim; i++) assertEquals(vec_1.get(i) + 2 * (float) ((DenseFloatVector) vec_3).get(i), vec_8.get(i));
}
Also used : TAbstractVector(com.tencent.angel.ml.math.TAbstractVector) Test(org.junit.Test)

Example 4 with TAbstractVector

use of com.tencent.angel.ml.math.TAbstractVector in project angel by Tencent.

the class DenseFloatVectorTest method plusByDenseVectTest.

@Test
public void plusByDenseVectTest() throws Exception {
    DenseFloatVector vec_1 = genDenseFloatVector(dim);
    TAbstractVector vec_2 = genDenseFloatVector(dim);
    TAbstractVector vec_3 = genDenseFloatVector(dim);
    float[] oldValues = vec_1.getValues().clone();
    vec_1.plusBy(vec_2);
    for (int i = 0; i < dim; i++) assertEquals(oldValues[i] + ((DenseFloatVector) vec_2).get(i), vec_1.get(i));
    oldValues = vec_1.getValues().clone();
    vec_1.plusBy(vec_3);
    for (int i = 0; i < dim; i++) assertEquals(oldValues[i] + (float) ((DenseFloatVector) vec_3).get(i), vec_1.get(i));
    oldValues = vec_1.getValues().clone();
    vec_1.plusBy(vec_2, 3.0);
    for (int i = 0; i < dim; i++) assertEquals(oldValues[i] + 3.0f * ((DenseFloatVector) vec_2).get(i), vec_1.get(i));
    oldValues = vec_1.getValues().clone();
    vec_1.plusBy(vec_3, 4);
    for (int i = 0; i < dim; i++) assertEquals(oldValues[i] + 4.0f * (float) ((DenseFloatVector) vec_3).get(i), vec_1.get(i));
}
Also used : TAbstractVector(com.tencent.angel.ml.math.TAbstractVector) Test(org.junit.Test)

Example 5 with TAbstractVector

use of com.tencent.angel.ml.math.TAbstractVector in project angel by Tencent.

the class LabeledUpdateIndexBaseTask method preProcess.

@Override
public void preProcess(TaskContext taskContext) {
    try {
        Reader<KEYIN, VALUEIN> reader = taskContext.getReader();
        while (reader.nextKeyValue()) {
            LabeledData out = parse(reader.getCurrentKey(), reader.getCurrentValue());
            if (out != null) {
                taskDataBlock.put(out);
                if (updateIndexEnable) {
                    TAbstractVector vector = out.getX();
                    if (vector instanceof SparseDummyVector) {
                        int[] indexes = ((SparseDummyVector) vector).getIndices();
                        for (int i = 0; i < indexes.length; i++) {
                            indexSet.add(indexes[i]);
                        }
                    }
                }
            }
        }
        taskDataBlock.flush();
    } catch (Exception e) {
        throw new AngelException("Pre-Process Error.", e);
    }
}
Also used : AngelException(com.tencent.angel.exception.AngelException) LabeledData(com.tencent.angel.ml.feature.LabeledData) TAbstractVector(com.tencent.angel.ml.math.TAbstractVector) SparseDummyVector(com.tencent.angel.ml.math.vector.SparseDummyVector) AngelException(com.tencent.angel.exception.AngelException) IOException(java.io.IOException)

Aggregations

TAbstractVector (com.tencent.angel.ml.math.TAbstractVector)10 Test (org.junit.Test)9 DenseDoubleVector (com.tencent.angel.ml.math.vector.DenseDoubleVector)2 Random (java.util.Random)2 AngelException (com.tencent.angel.exception.AngelException)1 LabeledData (com.tencent.angel.ml.feature.LabeledData)1 SparseDummyVector (com.tencent.angel.ml.math.vector.SparseDummyVector)1 MathsTest (com.tencent.angel.ml.utils.MathsTest)1 IOException (java.io.IOException)1