Search in sources :

Example 11 with Vector

use of com.tencent.angel.ml.math2.vector.Vector in project angel by Tencent.

the class PGDUpdateFunc method update.

@Override
public void update(RowBasedPartition partition, int factor, double[] scalars) {
    double lr = scalars[0];
    double l1RegParam = scalars[1];
    double l2RegParam = scalars[2];
    double batchSize = (int) scalars[3];
    for (int f = 0; f < factor; f++) {
        ServerRow gradientServerRow = partition.getRow(f + factor);
        try {
            gradientServerRow.startWrite();
            Vector weight = ServerRowUtils.getVector(partition.getRow(f));
            Vector gradient = ServerRowUtils.getVector(gradientServerRow);
            if (batchSize > 1) {
                gradient.idiv(batchSize);
            }
            double lrTemp = lr / (1 + l2RegParam * lr);
            if (l2RegParam != 0.0) {
                weight.imul(1 - lrTemp * l2RegParam).iaxpy(gradient, -lrTemp);
            } else {
                weight.iaxpy(gradient, -lrTemp);
            }
            if (l1RegParam != 0) {
                Ufuncs.isoftthreshold(weight, lrTemp * l1RegParam);
            }
            gradient.clear();
        } finally {
            gradientServerRow.endWrite();
        }
    }
}
Also used : ServerRow(com.tencent.angel.ps.storage.vector.ServerRow) Vector(com.tencent.angel.ml.math2.vector.Vector)

Example 12 with Vector

use of com.tencent.angel.ml.math2.vector.Vector in project angel by Tencent.

the class AsyncAdamFunc method update.

@Override
public void update(Vector[] vectors, Vector grad, double[] doubles, int[] ints) {
    double eta = doubles[0];
    double gamma = doubles[1];
    double beta = doubles[2];
    int numUpdates = ints[2];
    double powBeta = Math.pow(beta, numUpdates);
    double powGamma = Math.pow(gamma, numUpdates);
    Vector weight = vectors[0];
    Vector velocity = vectors[1];
    Vector square = vectors[2];
    OptFuncs.iexpsmoothing(velocity, grad, beta);
    OptFuncs.iexpsmoothing2(square, grad, gamma);
    velocity = Ufuncs.indexget(velocity, grad);
    Vector delta = OptFuncs.adamdelta(velocity, square, powBeta, powGamma);
    delta.imul(eta);
    weight.isub(delta);
}
Also used : Vector(com.tencent.angel.ml.math2.vector.Vector)

Example 13 with Vector

use of com.tencent.angel.ml.math2.vector.Vector in project angel by Tencent.

the class AsyncMomentumFunc method update.

@Override
public void update(Vector[] vectors, Vector grad, double[] doubles, int[] ints) {
    double eta = doubles[0];
    double momentum = doubles[1];
    Vector weight = vectors[0];
    Vector velocity = vectors[1];
    Ufuncs.imomentumupdate(velocity, grad, momentum, eta);
    grad = Ufuncs.indexget(velocity, grad);
    weight.isub(grad);
}
Also used : Vector(com.tencent.angel.ml.math2.vector.Vector)

Example 14 with Vector

use of com.tencent.angel.ml.math2.vector.Vector in project angel by Tencent.

the class MixedBinaryInZAExecutor method apply.

private static Vector apply(CompLongLongVector v1, LongIntVector v2, Binary op) {
    LongLongVector[] parts = v1.getPartitions();
    Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
    if (v2.isSparse()) {
        ObjectIterator<Long2IntMap.Entry> iter = v2.getStorage().entryIterator();
        if (v1.size() > v2.size()) {
            long subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
            while (iter.hasNext()) {
                Long2IntMap.Entry entry = iter.next();
                long idx = entry.getLongKey();
                int pidx = (int) (idx / subDim);
                long subidx = idx % subDim;
                if (parts[pidx].hasKey(subidx)) {
                    ((LongLongVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), entry.getIntValue()));
                }
            }
        } else {
            long base = 0;
            for (int i = 0; i < parts.length; i++) {
                LongLongVector part = parts[i];
                LongLongVectorStorage resPart = (LongLongVectorStorage) resParts[i];
                if (part.isDense()) {
                    long[] partValues = part.getStorage().getValues();
                    long[] resPartValues = resPart.getValues();
                    for (int j = 0; j < partValues.length; j++) {
                        if (v2.hasKey(j + base)) {
                            resPartValues[j] = op.apply(partValues[j], v2.get(j + base));
                        }
                    }
                } else if (part.isSparse()) {
                    ObjectIterator<Long2LongMap.Entry> piter = part.getStorage().entryIterator();
                    while (piter.hasNext()) {
                        Long2LongMap.Entry entry = piter.next();
                        long idx = entry.getLongKey();
                        if (v2.hasKey(idx + base)) {
                            resPart.set(idx, op.apply(entry.getLongValue(), v2.get(idx + base)));
                        }
                    }
                } else {
                    // sorted
                    if (op.isKeepStorage()) {
                        long[] partIndices = part.getStorage().getIndices();
                        long[] partValues = part.getStorage().getValues();
                        long[] resPartIndices = resPart.getIndices();
                        long[] resPartValues = resPart.getValues();
                        for (int j = 0; j < partIndices.length; j++) {
                            long idx = partIndices[j];
                            if (v2.hasKey(idx + base)) {
                                resPartIndices[j] = idx;
                                resPartValues[j] = op.apply(partValues[j], v2.get(idx + base));
                            }
                        }
                    } else {
                        long[] partIndices = part.getStorage().getIndices();
                        long[] partValues = part.getStorage().getValues();
                        for (int j = 0; j < partIndices.length; j++) {
                            long idx = partIndices[j];
                            if (v2.hasKey(idx + base)) {
                                resPart.set(idx, op.apply(partValues[j], v2.get(idx + base)));
                            }
                        }
                    }
                }
                base += part.getDim();
            }
        }
    } else {
        // sorted
        if (v1.size() > v2.size()) {
            long subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
            long[] v2Indices = v2.getStorage().getIndices();
            int[] v2Values = v2.getStorage().getValues();
            for (int i = 0; i < v2Indices.length; i++) {
                long idx = v2Indices[i];
                int pidx = (int) (idx / subDim);
                long subidx = idx % subDim;
                if (parts[pidx].hasKey(subidx)) {
                    ((LongLongVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
                }
            }
        } else {
            long base = 0;
            for (int i = 0; i < parts.length; i++) {
                LongLongVector part = parts[i];
                LongLongVectorStorage resPart = (LongLongVectorStorage) resParts[i];
                if (part.isDense()) {
                    long[] partValues = part.getStorage().getValues();
                    long[] resPartValues = resPart.getValues();
                    for (int j = 0; j < partValues.length; j++) {
                        if (v2.hasKey(j + base)) {
                            resPartValues[j] = op.apply(partValues[j], v2.get(j + base));
                        }
                    }
                } else if (part.isSparse()) {
                    ObjectIterator<Long2LongMap.Entry> piter = part.getStorage().entryIterator();
                    while (piter.hasNext()) {
                        Long2LongMap.Entry entry = piter.next();
                        long idx = entry.getLongKey();
                        if (v2.hasKey(idx + base)) {
                            resPart.set(idx, op.apply(entry.getLongValue(), v2.get(idx + base)));
                        }
                    }
                } else {
                    // sorted
                    if (op.isKeepStorage()) {
                        long[] partIndices = part.getStorage().getIndices();
                        long[] partValues = part.getStorage().getValues();
                        long[] resPartIndices = resPart.getIndices();
                        long[] resPartValues = resPart.getValues();
                        for (int j = 0; j < partIndices.length; j++) {
                            long idx = partIndices[j];
                            if (v2.hasKey(idx + base)) {
                                resPartIndices[j] = idx;
                                resPartValues[j] = op.apply(partValues[j], v2.get(idx + base));
                            }
                        }
                    } else {
                        long[] partIndices = part.getStorage().getIndices();
                        long[] partValues = part.getStorage().getValues();
                        for (int j = 0; j < partIndices.length; j++) {
                            long idx = partIndices[j];
                            if (v2.hasKey(idx + base)) {
                                resPart.set(idx, op.apply(partValues[j], v2.get(idx + base)));
                            }
                        }
                    }
                }
                base += part.getDim();
            }
        }
    }
    LongLongVector[] res = new LongLongVector[parts.length];
    int i = 0;
    for (LongLongVector part : parts) {
        res[i] = new LongLongVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (LongLongVectorStorage) resParts[i]);
        i++;
    }
    v1.setPartitions(res);
    return v1;
}
Also used : CompLongLongVector(com.tencent.angel.ml.math2.vector.CompLongLongVector) LongLongVector(com.tencent.angel.ml.math2.vector.LongLongVector) Long2IntMap(it.unimi.dsi.fastutil.longs.Long2IntMap) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) Long2LongMap(it.unimi.dsi.fastutil.longs.Long2LongMap) IntIntVectorStorage(com.tencent.angel.ml.math2.storage.IntIntVectorStorage) Storage(com.tencent.angel.ml.math2.storage.Storage) LongIntVectorStorage(com.tencent.angel.ml.math2.storage.LongIntVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) LongDoubleVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) LongLongVectorStorage(com.tencent.angel.ml.math2.storage.LongLongVectorStorage) LongFloatVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatVectorStorage) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) LongLongVectorStorage(com.tencent.angel.ml.math2.storage.LongLongVectorStorage)

Example 15 with Vector

use of com.tencent.angel.ml.math2.vector.Vector in project angel by Tencent.

the class MixedBinaryOutNonZAExecutor method apply.

private static Vector apply(CompIntDoubleVector v1, IntIntVector v2, Binary op) {
    IntDoubleVector[] parts = v1.getPartitions();
    Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
    if (v2.isDense()) {
        int[] v2Values = v2.getStorage().getValues();
        int base = 0, k = 0;
        for (IntDoubleVector part : parts) {
            IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[k];
            double[] newValues = resPart.getValues();
            if (part.isDense()) {
                double[] partValue = part.getStorage().getValues();
                for (int i = 0; i < partValue.length; i++) {
                    int idx = i + base;
                    newValues[i] = op.apply(partValue[i], v2Values[idx]);
                }
            } else if (part.isSparse()) {
                if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
                    for (int i = 0; i < part.getDim(); i++) {
                        resPart.set(i, op.apply(0, v2Values[i + base]));
                    }
                    ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
                    while (iter.hasNext()) {
                        Int2DoubleMap.Entry entry = iter.next();
                        int idx = entry.getIntKey();
                        resPart.set(idx, op.apply(entry.getDoubleValue(), v2Values[idx + base]));
                    }
                } else {
                    for (int i = 0; i < newValues.length; i++) {
                        if (part.getStorage().hasKey(i)) {
                            resPart.set(i, op.apply(part.get(i), v2Values[i + base]));
                        } else {
                            resPart.set(i, op.apply(0, v2Values[i + base]));
                        }
                    }
                }
            } else {
                // sorted
                if (op.isKeepStorage()) {
                    int dim = part.getDim();
                    int[] resIndices = resPart.getIndices();
                    double[] resValues = resPart.getValues();
                    int[] partIndices = part.getStorage().getIndices();
                    double[] partValues = part.getStorage().getValues();
                    for (int i = 0; i < dim; i++) {
                        resIndices[i] = i;
                        resValues[i] = op.apply(0, v2Values[i + base]);
                    }
                    int size = part.size();
                    for (int i = 0; i < size; i++) {
                        int idx = partIndices[i];
                        resValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
                    }
                } else {
                    if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
                        int[] partIndices = part.getStorage().getIndices();
                        double[] partValues = part.getStorage().getValues();
                        for (int i = 0; i < part.getDim(); i++) {
                            newValues[i] = op.apply(0, v2Values[i + base]);
                        }
                        int size = part.size();
                        for (int i = 0; i < size; i++) {
                            int idx = partIndices[i];
                            newValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
                        }
                    } else {
                        IntDoubleVectorStorage partStorage = part.getStorage();
                        for (int i = 0; i < newValues.length; i++) {
                            if (partStorage.hasKey(i)) {
                                newValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
                            } else {
                                newValues[i] = op.apply(0, v2Values[i + base]);
                            }
                        }
                    }
                }
            }
            base += part.getDim();
            k++;
        }
    } else {
        if (v2.isSparse()) {
            if (!op.isKeepStorage()) {
                for (int i = 0; i < parts.length; i++) {
                    if (parts[i].getStorage() instanceof IntDoubleSortedVectorStorage) {
                        resParts[i] = new IntDoubleSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
                    }
                }
            }
            int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
            ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2IntMap.Entry entry = iter.next();
                int gidx = entry.getIntKey();
                int pidx = (int) (gidx / subDim);
                int subidx = gidx % subDim;
                ((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), entry.getIntValue()));
            }
        } else {
            // sorted
            if (!op.isKeepStorage()) {
                for (int i = 0; i < parts.length; i++) {
                    if (parts[i].getStorage() instanceof IntDoubleSortedVectorStorage) {
                        resParts[i] = new IntDoubleSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
                    }
                }
            }
            int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
            int[] v2Indices = v2.getStorage().getIndices();
            int[] v2Values = v2.getStorage().getValues();
            for (int i = 0; i < v2Indices.length; i++) {
                int gidx = v2Indices[i];
                int pidx = (int) (gidx / subDim);
                int subidx = gidx % subDim;
                ((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
            }
        }
    }
    IntDoubleVector[] res = new IntDoubleVector[parts.length];
    int i = 0;
    for (IntDoubleVector part : parts) {
        res[i] = new IntDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntDoubleVectorStorage) resParts[i]);
        i++;
    }
    return new CompIntDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
Also used : Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) IntIntVectorStorage(com.tencent.angel.ml.math2.storage.IntIntVectorStorage) Storage(com.tencent.angel.ml.math2.storage.Storage) IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) LongIntVectorStorage(com.tencent.angel.ml.math2.storage.LongIntVectorStorage) LongLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSparseVectorStorage) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) LongDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage) LongDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage) LongLongVectorStorage(com.tencent.angel.ml.math2.storage.LongLongVectorStorage) LongFloatVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatVectorStorage) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) IntIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage) LongIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSortedVectorStorage) IntLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage) IntLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage) LongIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSparseVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) IntFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage) LongLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSortedVectorStorage) LongDoubleVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) IntIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage) IntFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage) LongFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage) LongFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) Int2IntMap(it.unimi.dsi.fastutil.ints.Int2IntMap)

Aggregations

IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)189 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)188 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)188 LongIntVectorStorage (com.tencent.angel.ml.math2.storage.LongIntVectorStorage)186 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)185 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)183 LongLongVectorStorage (com.tencent.angel.ml.math2.storage.LongLongVectorStorage)183 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)182 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)170 Storage (com.tencent.angel.ml.math2.storage.Storage)169 LongDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage)127 LongFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage)125 LongLongSparseVectorStorage (com.tencent.angel.ml.math2.storage.LongLongSparseVectorStorage)121 IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)119 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)119 LongIntSparseVectorStorage (com.tencent.angel.ml.math2.storage.LongIntSparseVectorStorage)119 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)118 LongDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage)118 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)117 LongFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage)117