Search in sources :

Example 11 with DenseDoubleVector

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

the class DenseDoubleMatrixTest method plusBy1.

@Test
public void plusBy1() throws Exception {
    double[][] value = { { 1.0, 2.0 }, { 3.0, 4.0 } };
    DenseDoubleMatrix mat = new DenseDoubleMatrix(2, 2, value);
    TDoubleVector vec = new DenseDoubleVector(2, new double[] { 1.0, 1.0 });
    vec.setRowId(0);
    TDoubleVector vec_1 = new SparseDoubleVector(2, new int[] { 1 }, new double[] { 1.0 });
    vec_1.setRowId(1);
    mat.plusBy(vec);
    mat.plusBy(vec_1);
    assertEquals(2.0, mat.get(0, 0));
    assertEquals(3.0, mat.get(0, 1));
    assertEquals(3.0, mat.get(1, 0));
    assertEquals(5.0, mat.get(1, 1));
}
Also used : TDoubleVector(com.tencent.angel.ml.math.vector.TDoubleVector) DenseDoubleVector(com.tencent.angel.ml.math.vector.DenseDoubleVector) SparseDoubleVector(com.tencent.angel.ml.math.vector.SparseDoubleVector) Test(org.junit.Test)

Example 12 with DenseDoubleVector

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

the class DenseDoubleMatrix method initVector.

@Override
public DenseDoubleVector initVector(int rowIndex) {
    DenseDoubleVector ret = new DenseDoubleVector((int) col);
    ret.setMatrixId(matrixId);
    ret.setRowId(rowIndex);
    return ret;
}
Also used : DenseDoubleVector(com.tencent.angel.ml.math.vector.DenseDoubleVector)

Example 13 with DenseDoubleVector

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

the class TransportTest method testGetDenseDoubleMatrix.

@Test
public void testGetDenseDoubleMatrix() throws Exception {
    try {
        Worker worker = LocalClusterContext.get().getWorker(worker0Attempt0Id).getWorker();
        MatrixClient mat = worker.getPSAgent().getMatrixClient("dense_double_mat", 0);
        for (int rowId = 0; rowId < ddRow; rowId += 5) {
            DenseDoubleVector row = (DenseDoubleVector) mat.getRow(rowId);
            DenseDoubleVector expect = new DenseDoubleVector(ddCol);
            assertArrayEquals(row.getValues(), expect.getValues(), 0.0);
            DenseDoubleVector update = new DenseDoubleVector(ddCol);
            update.setMatrixId(mat.getMatrixId());
            update.setRowId(rowId);
            Random rand = new Random(System.currentTimeMillis());
            for (int i = 0; i < ddCol; i += 2) update.set(i, rand.nextDouble());
            mat.increment(update);
            mat.clock().get();
            row = (DenseDoubleVector) mat.getRow(rowId);
            expect.plusBy(update);
            assertArrayEquals(expect.getValues(), row.getValues(), 0.0);
            update = new DenseDoubleVector(ddCol);
            update.setMatrixId(mat.getMatrixId());
            update.setRowId(rowId);
            for (int i = 0; i < ddCol; i += 3) update.set(i, rand.nextDouble());
            mat.increment(update);
            mat.clock().get();
            row = (DenseDoubleVector) mat.getRow(rowId);
            expect.plusBy(update);
            assertArrayEquals(expect.getValues(), row.getValues(), 0.0);
        }
    } catch (Exception x) {
        LOG.error("run testGetDenseDoubleMatrix failed ", x);
        throw x;
    }
}
Also used : Random(java.util.Random) DenseDoubleVector(com.tencent.angel.ml.math.vector.DenseDoubleVector) MatrixClient(com.tencent.angel.psagent.matrix.MatrixClient) IOException(java.io.IOException) MasterServiceTest(com.tencent.angel.master.MasterServiceTest) Test(org.junit.Test)

Example 14 with DenseDoubleVector

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

the class GetValueOfIndexTask method denseDouble.

public void denseDouble(TaskContext tContext) {
    long startGen = System.currentTimeMillis();
    int[] index = genIndexs(feaNum, nnz);
    long cost = System.currentTimeMillis() - startGen;
    LOG.info("Gen index cost: " + cost + " ms.");
    try {
        MatrixClient dMatClient = tContext.getMatrix(DENSE_DOUBLE_MAT);
        // Set PS Model values
        long startInc = System.currentTimeMillis();
        DenseDoubleVector delt = new DenseDoubleVector(feaNum);
        for (int i = 0; i < feaNum; i++) delt.set(i, (double) i);
        dMatClient.increment(0, delt);
        dMatClient.clock().get();
        long costInc = System.currentTimeMillis() - startInc;
        LOG.info("Increment delt cost " + costInc + " ms.");
        // Wait for all tasks finish this clock
        dMatClient.getTaskContext().globalSync(dMatClient.getMatrixId());
        // Get values of index array
        long startGet = System.currentTimeMillis();
        IndexGetFunc func = new IndexGetFunc(new IndexGetParam(tContext.getMatrix(DENSE_DOUBLE_MAT).getMatrixId(), 0, index));
        SparseDoubleVector row = (SparseDoubleVector) ((GetRowResult) dMatClient.get(func)).getRow();
        long costGet = System.currentTimeMillis() - startGet;
        LOG.info("Get row of indexs cost " + costGet + " ms.");
    } catch (Throwable e) {
        throw new AngelException(e);
    }
}
Also used : AngelException(com.tencent.angel.exception.AngelException) DenseDoubleVector(com.tencent.angel.ml.math.vector.DenseDoubleVector) MatrixClient(com.tencent.angel.psagent.matrix.MatrixClient) IndexGetFunc(com.tencent.angel.ml.matrix.psf.get.enhance.indexed.IndexGetFunc) IndexGetParam(com.tencent.angel.ml.matrix.psf.get.enhance.indexed.IndexGetParam) SparseDoubleVector(com.tencent.angel.ml.math.vector.SparseDoubleVector)

Example 15 with DenseDoubleVector

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

the class L1LogLossTest 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 = l1LogLoss.loss(xList, yList, w, 2);
    assertEquals(0.741297, test, 0.00001);
}
Also used : DenseDoubleVector(com.tencent.angel.ml.math.vector.DenseDoubleVector) TAbstractVector(com.tencent.angel.ml.math.TAbstractVector) Test(org.junit.Test)

Aggregations

DenseDoubleVector (com.tencent.angel.ml.math.vector.DenseDoubleVector)20 Test (org.junit.Test)15 MatrixClient (com.tencent.angel.psagent.matrix.MatrixClient)5 AngelException (com.tencent.angel.exception.AngelException)3 TVector (com.tencent.angel.ml.math.TVector)3 SparseDoubleVector (com.tencent.angel.ml.math.vector.SparseDoubleVector)3 TDoubleVector (com.tencent.angel.ml.math.vector.TDoubleVector)3 MasterServiceTest (com.tencent.angel.master.MasterServiceTest)2 TAbstractVector (com.tencent.angel.ml.math.TAbstractVector)2 MathsTest (com.tencent.angel.ml.utils.MathsTest)2 IOException (java.io.IOException)2 Random (java.util.Random)2 DenseDoubleMatrix (com.tencent.angel.ml.math.matrix.DenseDoubleMatrix)1 CompSparseLongKeyDoubleVector (com.tencent.angel.ml.math.vector.CompSparseLongKeyDoubleVector)1 SparseDummyVector (com.tencent.angel.ml.math.vector.SparseDummyVector)1 SparseLongKeyDoubleVector (com.tencent.angel.ml.math.vector.SparseLongKeyDoubleVector)1 Pull (com.tencent.angel.ml.matrix.psf.aggr.Pull)1 IndexGetFunc (com.tencent.angel.ml.matrix.psf.get.enhance.indexed.IndexGetFunc)1 IndexGetParam (com.tencent.angel.ml.matrix.psf.get.enhance.indexed.IndexGetParam)1 GetRowResult (com.tencent.angel.ml.matrix.psf.get.single.GetRowResult)1