Search in sources :

Example 16 with IntFloatVectorStorage

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

the class IntFloatVector method numZeros.

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

Example 17 with IntFloatVectorStorage

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

the class IntFloatVector method max.

public float max() {
    IntFloatVectorStorage idstorage = (IntFloatVectorStorage) storage;
    if (idstorage.size() == 0)
        return 0;
    float maxval = Float.MIN_VALUE;
    if (idstorage.isSparse()) {
        FloatIterator iter = idstorage.valueIterator();
        while (iter.hasNext()) {
            float val = iter.nextFloat();
            if (val > maxval) {
                maxval = val;
            }
        }
    } else {
        for (float val : idstorage.getValues()) {
            if (val > maxval) {
                maxval = val;
            }
        }
    }
    return maxval;
}
Also used : FloatIterator(it.unimi.dsi.fastutil.floats.FloatIterator) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)

Example 18 with IntFloatVectorStorage

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

the class IntFloatVector method average.

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

Example 19 with IntFloatVectorStorage

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

the class ColumnFormat method saveLongFloatRows.

private void saveLongFloatRows(ServerPartition part, ServerRow[] rows, MatrixPartitionMeta partMeta, PSMatrixSaveContext saveContext, DataOutputStream output) throws IOException {
    Vector vec = ServerRowUtils.getVector((ServerLongFloatRow) rows[0]);
    // int size = rows.size();
    long indexOffset = part.getPartitionKey().getStartCol();
    LongFloatsCol col = new LongFloatsCol(0, new float[rows.length]);
    if (vec instanceof IntFloatVector) {
        IntFloatVectorStorage storage = ((IntFloatVector) vec).getStorage();
        long startCol = rows[0].getStartCol();
        long endCol = rows[0].getEndCol();
        if (storage.isDense()) {
            for (long i = startCol; i < endCol; i++) {
                col.colId = i;
                for (int j = 0; j < rows.length; j++) {
                    col.colElems[j] = ((ServerLongFloatRow) (rows[j])).get(col.colId);
                }
                save(col, output);
            }
        } else {
            if (saveContext.sortFirst()) {
                int[] indices = storage.getIndices();
                Sort.quickSort(indices, 0, indices.length - 1);
                for (int i = 0; i < indices.length; i++) {
                    col.colId = indices[i] + indexOffset;
                    for (int j = 0; j < rows.length; j++) {
                        col.colElems[j] = ((ServerLongFloatRow) (rows[j])).get(col.colId);
                    }
                    save(col, output);
                }
            } else {
                ObjectIterator<Int2FloatMap.Entry> iter = storage.entryIterator();
                while (iter.hasNext()) {
                    col.colId = iter.next().getIntKey() + indexOffset;
                    for (int j = 0; j < rows.length; j++) {
                        col.colElems[j] = ((ServerLongFloatRow) (rows[j])).get(col.colId);
                    }
                    save(col, output);
                }
            }
        }
    } else {
        LongFloatVectorStorage storage = ((LongFloatVector) vec).getStorage();
        if (saveContext.sortFirst()) {
            long[] indices = storage.getIndices();
            Sort.quickSort(indices, 0, indices.length - 1);
            for (int i = 0; i < indices.length; i++) {
                col.colId = indices[i] + indexOffset;
                for (int j = 0; j < rows.length; j++) {
                    col.colElems[j] = ((ServerLongFloatRow) (rows[j])).get(col.colId);
                }
                save(col, output);
            }
        } else {
            ObjectIterator<Long2FloatMap.Entry> iter = storage.entryIterator();
            while (iter.hasNext()) {
                col.colId = iter.next().getLongKey() + indexOffset;
                for (int j = 0; j < rows.length; j++) {
                    col.colElems[j] = ((ServerLongFloatRow) (rows[j])).get(col.colId);
                }
                save(col, output);
            }
        }
    }
}
Also used : Vector(com.tencent.angel.ml.math2.vector.Vector)

Example 20 with IntFloatVectorStorage

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

the class StreamSerdeUtils method serializeIntFloatVector.

// IntFloatVector
private static void serializeIntFloatVector(DataOutputStream out, IntFloatVector vector) throws IOException {
    IntFloatVectorStorage storage = vector.getStorage();
    if (storage.isDense()) {
        serializeInt(out, DENSE_STORAGE_TYPE);
        serializeFloats(out, storage.getValues());
    } else if (storage.isSparse()) {
        serializeInt(out, SPARSE_STORAGE_TYPE);
        serializeInt(out, storage.size());
        ObjectIterator<Int2FloatMap.Entry> iter = storage.entryIterator();
        while (iter.hasNext()) {
            Int2FloatMap.Entry e = iter.next();
            serializeInt(out, e.getIntKey());
            serializeFloat(out, e.getFloatValue());
        }
    } else if (storage.isSorted()) {
        serializeInt(out, SORTED_STORAGE_TYPE);
        int[] indices = vector.getStorage().getIndices();
        float[] values = vector.getStorage().getValues();
        serializeInts(out, indices);
        serializeFloats(out, values);
    } else {
        throw new UnsupportedOperationException("Unsupport storage type " + vector.getStorage().getClass());
    }
}
Also used : IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) Entry(it.unimi.dsi.fastutil.ints.Int2DoubleMap.Entry) Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Aggregations

IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)52 Int2FloatMap (it.unimi.dsi.fastutil.ints.Int2FloatMap)40 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)38 IntFloatVector (com.tencent.angel.ml.math2.vector.IntFloatVector)33 IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)26 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)26 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)26 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)24 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)24 LongIntVectorStorage (com.tencent.angel.ml.math2.storage.LongIntVectorStorage)24 LongLongVectorStorage (com.tencent.angel.ml.math2.storage.LongLongVectorStorage)24 Storage (com.tencent.angel.ml.math2.storage.Storage)24 CompIntFloatVector (com.tencent.angel.ml.math2.vector.CompIntFloatVector)24 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)22 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)19 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)16 IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)16 IntIntSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage)16 IntIntSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage)16 IntLongSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage)16