use of com.tencent.angel.ml.math2.storage.IntDoubleDenseVectorStorage in project angel by Tencent.
the class RowBasedMatrix method dot.
@Override
public Vector dot(Vector other) {
double[] resArr = new double[rows.length];
for (int i = 0; i < rows.length; i++) {
resArr[i] = rows[i].dot(other);
}
IntDoubleDenseVectorStorage storage = new IntDoubleDenseVectorStorage(resArr);
return new IntDoubleVector(matrixId, 0, clock, rows.length, storage);
}
use of com.tencent.angel.ml.math2.storage.IntDoubleDenseVectorStorage in project angel by Tencent.
the class DotMatrixExecutor method apply.
private static Vector apply(BlasDoubleMatrix mat, boolean trans, IntDoubleVector v) {
int m = mat.getNumRows(), n = mat.getNumCols();
double[] resArr;
if (trans) {
assert m == v.getDim();
resArr = new double[n];
} else {
assert n == v.getDim();
resArr = new double[m];
}
int r = mat.getNumRows(), c = mat.getNumCols();
double[] data = mat.getData();
if (v.isDense()) {
double[] tempArray = v.getStorage().getValues();
if (trans) {
blas.dgemv("N", c, r, 1.0, data, c, tempArray, 1, 0.0, resArr, 1);
} else {
blas.dgemv("T", c, r, 1.0, data, c, tempArray, 1, 0.0, resArr, 1);
}
} else if (v.isSparse()) {
if (trans) {
for (int j = 0; j < c; j++) {
ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int i = entry.getIntKey();
resArr[j] += data[i * c + j] * entry.getDoubleValue();
}
}
} else {
for (int i = 0; i < r; i++) {
ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int j = entry.getIntKey();
resArr[i] += data[i * c + j] * entry.getDoubleValue();
}
}
}
} else {
// sorted
if (trans) {
for (int j = 0; j < r; j++) {
int[] idxs = v.getStorage().getIndices();
double[] vals = v.getStorage().getValues();
for (int k = 0; k < idxs.length; k++) {
resArr[j] += data[idxs[k] * c + j] * vals[k];
}
}
} else {
for (int i = 0; i < r; i++) {
int[] idxs = v.getStorage().getIndices();
double[] vals = v.getStorage().getValues();
for (int k = 0; k < idxs.length; k++) {
resArr[i] += data[i * c + idxs[k]] * vals[k];
}
}
}
}
IntDoubleDenseVectorStorage storage = new IntDoubleDenseVectorStorage(resArr);
return new IntDoubleVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
use of com.tencent.angel.ml.math2.storage.IntDoubleDenseVectorStorage in project angel by Tencent.
the class DotMatrixExecutor method apply.
private static Vector apply(BlasDoubleMatrix mat, boolean trans, IntLongVector v) {
int m = mat.getNumRows(), n = mat.getNumCols();
double[] resArr;
if (trans) {
assert m == v.getDim();
resArr = new double[n];
} else {
assert n == v.getDim();
resArr = new double[m];
}
int r = mat.getNumRows(), c = mat.getNumCols();
double[] data = mat.getData();
if (v.isDense()) {
double[] tempArray = ArrayCopy.copy(v.getStorage().getValues(), new double[v.getDim()]);
if (trans) {
blas.dgemv("N", c, r, 1.0, data, c, tempArray, 1, 0.0, resArr, 1);
} else {
blas.dgemv("T", c, r, 1.0, data, c, tempArray, 1, 0.0, resArr, 1);
}
} else if (v.isSparse()) {
if (trans) {
for (int j = 0; j < c; j++) {
ObjectIterator<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int i = entry.getIntKey();
resArr[j] += data[i * c + j] * entry.getLongValue();
}
}
} else {
for (int i = 0; i < r; i++) {
ObjectIterator<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int j = entry.getIntKey();
resArr[i] += data[i * c + j] * entry.getLongValue();
}
}
}
} else {
// sorted
if (trans) {
for (int j = 0; j < r; j++) {
int[] idxs = v.getStorage().getIndices();
long[] vals = v.getStorage().getValues();
for (int k = 0; k < idxs.length; k++) {
resArr[j] += data[idxs[k] * c + j] * vals[k];
}
}
} else {
for (int i = 0; i < r; i++) {
int[] idxs = v.getStorage().getIndices();
long[] vals = v.getStorage().getValues();
for (int k = 0; k < idxs.length; k++) {
resArr[i] += data[i * c + idxs[k]] * vals[k];
}
}
}
}
IntDoubleDenseVectorStorage storage = new IntDoubleDenseVectorStorage(resArr);
return new IntDoubleVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
use of com.tencent.angel.ml.math2.storage.IntDoubleDenseVectorStorage in project angel by Tencent.
the class DotMatrixExecutor method apply.
private static Vector apply(BlasDoubleMatrix mat, boolean trans, IntFloatVector v) {
int m = mat.getNumRows(), n = mat.getNumCols();
double[] resArr;
if (trans) {
assert m == v.getDim();
resArr = new double[n];
} else {
assert n == v.getDim();
resArr = new double[m];
}
int r = mat.getNumRows(), c = mat.getNumCols();
double[] data = mat.getData();
if (v.isDense()) {
double[] tempArray = ArrayCopy.copy(v.getStorage().getValues(), new double[v.getDim()]);
if (trans) {
blas.dgemv("N", c, r, 1.0, data, c, tempArray, 1, 0.0, resArr, 1);
} else {
blas.dgemv("T", c, r, 1.0, data, c, tempArray, 1, 0.0, resArr, 1);
}
} else if (v.isSparse()) {
if (trans) {
for (int j = 0; j < c; j++) {
ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int i = entry.getIntKey();
resArr[j] += data[i * c + j] * entry.getFloatValue();
}
}
} else {
for (int i = 0; i < r; i++) {
ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int j = entry.getIntKey();
resArr[i] += data[i * c + j] * entry.getFloatValue();
}
}
}
} else {
// sorted
if (trans) {
for (int j = 0; j < r; j++) {
int[] idxs = v.getStorage().getIndices();
float[] vals = v.getStorage().getValues();
for (int k = 0; k < idxs.length; k++) {
resArr[j] += data[idxs[k] * c + j] * vals[k];
}
}
} else {
for (int i = 0; i < r; i++) {
int[] idxs = v.getStorage().getIndices();
float[] vals = v.getStorage().getValues();
for (int k = 0; k < idxs.length; k++) {
resArr[i] += data[i * c + idxs[k]] * vals[k];
}
}
}
}
IntDoubleDenseVectorStorage storage = new IntDoubleDenseVectorStorage(resArr);
return new IntDoubleVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
use of com.tencent.angel.ml.math2.storage.IntDoubleDenseVectorStorage in project angel by Tencent.
the class GBDTController method updateLeafPreds.
public void updateLeafPreds() throws Exception {
LOG.info("------Update leaf node predictions------");
long startTime = System.currentTimeMillis();
Set<String> needFlushMatrixSet = new HashSet<String>(1);
if (taskContext.getTaskIndex() == 0) {
int nodeNum = this.forest[currentTree].nodes.size();
IntDoubleVector vec = new IntDoubleVector(this.maxNodeNum, new IntDoubleDenseVectorStorage(this.maxNodeNum));
for (int nid = 0; nid < nodeNum; nid++) {
if (null != this.forest[currentTree].nodes.get(nid) && this.forest[currentTree].nodes.get(nid).isLeaf()) {
float weight = this.forest[currentTree].nodes.get(nid).getLeafValue();
LOG.debug(String.format("Leaf weight of node[%d]: %f", nid, weight));
vec.set(nid, weight);
}
}
PSModel nodePreds = this.model.getPSModel(this.param.nodePredsName);
nodePreds.increment(this.currentTree, vec);
// the leader task adds node prediction to flush list
needFlushMatrixSet.add(this.param.nodePredsName);
}
clockAllMatrix(needFlushMatrixSet, true);
LOG.info(String.format("Update leaf node predictions cost: %d ms", System.currentTimeMillis() - startTime));
}
Aggregations