use of com.tencent.angel.ml.math.vector.SparseDoubleVector in project angel by Tencent.
the class DoubleVectorTest method timesBySparseTest.
@Test
public void timesBySparseTest() {
SparseDoubleVector vector = new SparseDoubleVector(10);
vector.set(1, 1.0);
vector.set(3, 3.0);
vector.set(5, 5.0);
vector.timesBy(2.0);
Assert.assertEquals(2.0, vector.get(1), 0.0);
Assert.assertEquals(6.0, vector.get(3), 0.0);
Assert.assertEquals(10.0, vector.get(5), 0.0);
}
use of com.tencent.angel.ml.math.vector.SparseDoubleVector in project angel by Tencent.
the class DoubleVectorTest method sparseDoubleVectorTest.
@Test
public void sparseDoubleVectorTest() {
int dim = 1000;
int[] indices = new int[dim];
double[] values = new double[dim];
Random random = new Random(Time.monotonicNow());
for (int i = 0; i < dim; i++) {
indices[i] = random.nextInt(dim);
values[i] = random.nextInt(dim);
}
SparseDoubleVector vector = new SparseDoubleVector(dim, indices, values);
int[] indices2 = vector.getIndices();
double[] values2 = vector.getValues();
for (int i = 0; i < indices2.length; i++) {
Assert.assertEquals(vector.get(indices2[i]), values2[i], 0.0);
}
}
use of com.tencent.angel.ml.math.vector.SparseDoubleVector 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.SparseDoubleVector in project angel by Tencent.
the class DoubleVectorTest method densePlusBySparseTest.
@Test
public void densePlusBySparseTest() {
DenseDoubleVector vector = new DenseDoubleVector(10);
vector.set(1, 1.0);
vector.set(3, 3.0);
vector.set(5, 5.0);
SparseDoubleVector sparseDoubleVector = new SparseDoubleVector(10);
sparseDoubleVector.set(1, 1.0);
sparseDoubleVector.set(3, 3.0);
sparseDoubleVector.set(7, 7.0);
vector.plusBy(sparseDoubleVector, 1.0);
Assert.assertEquals(2.0, vector.get(1), 0.0);
Assert.assertEquals(6.0, vector.get(3), 0.0);
Assert.assertEquals(5.0, vector.get(5), 0.0);
Assert.assertEquals(7.0, vector.get(7), 0.0);
}
use of com.tencent.angel.ml.math.vector.SparseDoubleVector in project angel by Tencent.
the class DoubleVectorTest method plusBySparseTest.
@Test
public void plusBySparseTest() {
SparseDoubleVector vector = new SparseDoubleVector(10);
vector.set(1, 1.0);
vector.set(3, 3.0);
vector.set(5, 5.0);
SparseDummyVector dummyVector = new SparseDummyVector(10);
dummyVector.set(1, 1.0);
dummyVector.set(3, 1.0);
vector.plusBy(dummyVector, 1.0);
Assert.assertEquals(2.0, vector.get(1), 0.0);
Assert.assertEquals(4.0, vector.get(3), 0.0);
Assert.assertEquals(5.0, vector.get(5), 0.0);
}
Aggregations