Search in sources :

Example 1 with DenseFloatVector

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

the class TransportTest method testGetDenseFloatMatrix.

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

Example 2 with DenseFloatVector

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

the class DenseFloatMatrixTest method plusByDenseFloatVectorTest.

@Test
public void plusByDenseFloatVectorTest() throws Exception {
    float[][] value = { { 1.0f, 2.0f }, { 3.0f, 4.0f } };
    DenseFloatMatrix mat = new DenseFloatMatrix(2, 2, value);
    TVector vec = new DenseFloatVector(2, new float[] { 1.0f, 1.0f });
    vec.setRowId(0);
    mat.plusBy(vec);
    assertEquals(2.0f, mat.get(0, 0));
    assertEquals(3.0f, mat.get(0, 1));
    assertEquals(3.0f, mat.get(1, 0));
    assertEquals(4.0f, mat.get(1, 1));
    DenseFloatMatrix mat_1 = new DenseFloatMatrix(2, 2);
    DenseFloatVector vec_1 = new DenseFloatVector(2, new float[] { 1.0f, 1.0f });
    vec_1.setRowId(0);
    mat_1.plusBy(vec_1);
    assertEquals(1.0f, mat_1.get(0, 0));
    assertEquals(1.0f, mat_1.get(0, 1));
    assertEquals(0.0f, mat_1.get(1, 0));
    assertEquals(0.0f, mat_1.get(1, 1));
}
Also used : DenseFloatVector(com.tencent.angel.ml.math.vector.DenseFloatVector) TVector(com.tencent.angel.ml.math.TVector) Test(org.junit.Test)

Example 3 with DenseFloatVector

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

the class DenseFloatMatrixTest method get.

// @Test
// public void plusBy3() throws Exception {
// float[][] value = {{1.0f, 2.0f}, {3.0f, 4.0f}};
// DenseFloatMatrix mat = new DenseFloatMatrix(2, 2,value);
// TFloatVector vec = new DenseFloatVector(2, new float[]{1.0f, 1.0f});
// vec.setRowId(0);
// TDoubleVector vec_1 = new DenseDoubleVector(2, new double[]{1.0f, 1.0f});
// vec_1.setRowId(1);
// TDoubleVector vec_2 = new SparseDoubleVector(2);
// vec_2.set(1, 1.0);
// vec_2.setRowId(0);
// 
// mat.plusBy(vec);
// mat.plusBy(vec_1);
// mat.plusBy(vec_2);
// 
// assertEquals(2.0f, mat.get(0, 0));
// assertEquals(4.0f, mat.get(0, 1));
// assertEquals(4.0f, mat.get(1, 0));
// assertEquals(5.0f, mat.get(1, 1));
// }
@Test
public void get() throws Exception {
    float[][] value = { { 1.0f, 2.0f }, { 3.0f, 4.0f } };
    DenseFloatMatrix mat = new DenseFloatMatrix(2, 2, value);
    assertEquals(1.0f, mat.get(0, 0));
    assertEquals(2.0f, mat.get(0, 1));
    assertEquals(3.0f, mat.get(1, 0));
    assertEquals(4.0f, mat.get(1, 1));
    DenseFloatMatrix mat_1 = new DenseFloatMatrix(2, 2);
    DenseFloatVector vec = new DenseFloatVector(2, new float[] { 1.0f, 2.0f });
    vec.setRowId(0);
    mat_1.plusBy(vec);
    assertEquals(1.0f, mat_1.get(0, 0));
    assertEquals(2.0f, mat_1.get(0, 1));
    assertEquals(0.0f, mat_1.get(1, 0));
    assertEquals(0.0f, mat_1.get(1, 1));
}
Also used : DenseFloatVector(com.tencent.angel.ml.math.vector.DenseFloatVector) Test(org.junit.Test)

Example 4 with DenseFloatVector

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

the class DenseFloatMatrix method initVector.

/**
 * init the vector by set the value
 *
 * @param rowIndex
 * @param values
 * @return
 */
private DenseFloatVector initVector(int rowIndex, float[] values) {
    DenseFloatVector ret = new DenseFloatVector((int) col, values);
    ret.setMatrixId(matrixId);
    ret.setRowId(rowIndex);
    return ret;
}
Also used : DenseFloatVector(com.tencent.angel.ml.math.vector.DenseFloatVector)

Example 5 with DenseFloatVector

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

the class DenseFloatMatrix method initVector.

/**
 * init the empty vector
 *
 * @param rowIndex
 * @return
 */
public DenseFloatVector initVector(int rowIndex) {
    DenseFloatVector ret = new DenseFloatVector((int) col);
    ret.setMatrixId(matrixId);
    ret.setRowId(rowIndex);
    return ret;
}
Also used : DenseFloatVector(com.tencent.angel.ml.math.vector.DenseFloatVector)

Aggregations

DenseFloatVector (com.tencent.angel.ml.math.vector.DenseFloatVector)5 Test (org.junit.Test)3 MasterServiceTest (com.tencent.angel.master.MasterServiceTest)1 TVector (com.tencent.angel.ml.math.TVector)1 MatrixClient (com.tencent.angel.psagent.matrix.MatrixClient)1 IOException (java.io.IOException)1 Random (java.util.Random)1