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