Search in sources :

Example 21 with IntDoubleVectorStorage

use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.

the class SimpleUnaryExecutor method apply.

private static Vector apply(IntDoubleVector v1, Unary op) {
    IntDoubleVector res;
    if (op.isOrigin() || v1.isDense()) {
        if (!op.isInplace()) {
            res = v1.copy();
        } else {
            res = v1;
        }
        if (v1.isDense()) {
            double[] values = res.getStorage().getValues();
            for (int i = 0; i < values.length; i++) {
                values[i] = op.apply(values[i]);
            }
        } else if (v1.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = res.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                entry.setValue(op.apply(entry.getDoubleValue()));
            }
        } else if (v1.isSorted()) {
            double[] values = res.getStorage().getValues();
            for (int i = 0; i < v1.size(); i++) {
                values[i] = op.apply(values[i]);
            }
        } else {
            throw new AngelException("The operation is not support!");
        }
    } else {
        IntDoubleVectorStorage newstorage = v1.getStorage().emptyDense();
        IntDoubleVectorStorage storage = v1.getStorage();
        double[] values = newstorage.getValues();
        double tmp = op.apply((double) 0);
        int dim = v1.getDim();
        for (int i = 0; i < dim; i++) {
            values[i] = tmp;
        }
        if (v1.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = storage.entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                values[entry.getIntKey()] = op.apply(entry.getDoubleValue());
            }
        } else {
            // sort
            int[] idxs = storage.getIndices();
            double[] v1Values = storage.getValues();
            for (int k = 0; k < idxs.length; k++) {
                values[idxs[k]] = op.apply(v1Values[k]);
            }
        }
        if (op.isInplace()) {
            v1.setStorage(newstorage);
            res = v1;
        } else {
            res = new IntDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newstorage);
        }
    }
    return res;
}
Also used : AngelException(com.tencent.angel.exception.AngelException) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 22 with IntDoubleVectorStorage

use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.

the class IntDoubleVector method std.

public double std() {
    IntDoubleVectorStorage dstorage = (IntDoubleVectorStorage) storage;
    if (dstorage.size() == 0)
        return 0;
    double sumval = 0.0;
    double sumval2 = 0.0;
    if (dstorage.isSparse()) {
        DoubleIterator iter = dstorage.valueIterator();
        while (iter.hasNext()) {
            double val = iter.nextDouble();
            sumval += val;
            sumval2 += val * val;
        }
    } else {
        for (double val : dstorage.getValues()) {
            sumval += val;
            sumval2 += val * val;
        }
    }
    sumval /= getDim();
    sumval2 /= getDim();
    return Math.sqrt(sumval2 - sumval * sumval);
}
Also used : IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) DoubleIterator(it.unimi.dsi.fastutil.doubles.DoubleIterator)

Example 23 with IntDoubleVectorStorage

use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.

the class IntDoubleVector method average.

public double average() {
    IntDoubleVectorStorage dstorage = (IntDoubleVectorStorage) storage;
    if (dstorage.size() == 0)
        return 0;
    double sumval = 0.0;
    if (dstorage.isSparse()) {
        DoubleIterator iter = dstorage.valueIterator();
        while (iter.hasNext()) {
            sumval += iter.nextDouble();
        }
    } else {
        for (double val : dstorage.getValues()) {
            sumval += val;
        }
    }
    sumval /= getDim();
    return sumval;
}
Also used : IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) DoubleIterator(it.unimi.dsi.fastutil.doubles.DoubleIterator)

Example 24 with IntDoubleVectorStorage

use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.

the class IntDoubleVector method numZeros.

public int numZeros() {
    IntDoubleVectorStorage dstorage = (IntDoubleVectorStorage) storage;
    if (dstorage.size() == 0)
        return (int) dim;
    int numZero = 0;
    if (dstorage.isSparse()) {
        DoubleIterator iter = dstorage.valueIterator();
        while (iter.hasNext()) {
            if (iter.nextDouble() != 0) {
                numZero += 1;
            }
        }
    } else {
        for (double val : dstorage.getValues()) {
            if (val != 0) {
                numZero += 1;
            }
        }
    }
    return (int) getDim() - numZero;
}
Also used : IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) DoubleIterator(it.unimi.dsi.fastutil.doubles.DoubleIterator)

Example 25 with IntDoubleVectorStorage

use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.

the class CompIntDoubleRowUpdateSplit method serialize.

@Override
public void serialize(ByteBuf buf) {
    super.serialize(buf);
    IntDoubleVectorStorage storage = split.getStorage();
    if (storage instanceof IntDoubleSparseVectorStorage) {
        buf.writeInt(storage.size());
        ObjectIterator<Int2DoubleMap.Entry> iter = storage.entryIterator();
        Int2DoubleMap.Entry entry;
        while (iter.hasNext()) {
            entry = iter.next();
            buf.writeInt(entry.getIntKey());
            buf.writeDouble(entry.getDoubleValue());
        }
    } else if (storage instanceof IntDoubleSortedVectorStorage) {
        buf.writeInt(storage.size());
        int[] indices = storage.getIndices();
        double[] values = storage.getValues();
        for (int i = 0; i < indices.length; i++) {
            buf.writeInt(indices[i]);
            buf.writeDouble(values[i]);
        }
    } else if (storage instanceof IntDoubleDenseVectorStorage) {
        double[] values = storage.getValues();
        int writeSize = values.length < maxItemNum ? values.length : maxItemNum;
        buf.writeInt(writeSize);
        for (int i = 0; i < writeSize; i++) {
            buf.writeDouble(values[i]);
        }
    } else {
        throw new UnsupportedOperationException("unsupport split for storage " + storage.getClass().getName());
    }
}
Also used : IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) IntDoubleDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleDenseVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)

Aggregations

IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)59 Int2DoubleMap (it.unimi.dsi.fastutil.ints.Int2DoubleMap)45 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)45 IntDoubleVector (com.tencent.angel.ml.math2.vector.IntDoubleVector)41 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)32 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)32 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)32 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)30 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)30 LongIntVectorStorage (com.tencent.angel.ml.math2.storage.LongIntVectorStorage)30 LongLongVectorStorage (com.tencent.angel.ml.math2.storage.LongLongVectorStorage)30 Storage (com.tencent.angel.ml.math2.storage.Storage)30 CompIntDoubleVector (com.tencent.angel.ml.math2.vector.CompIntDoubleVector)30 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)27 IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)23 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)20 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)20 IntIntSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage)20 IntIntSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage)20 IntLongSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage)20