Java Code Examples for it.unimi.dsi.fastutil.ints.Int2FloatMap#Entry

The following examples show how to use it.unimi.dsi.fastutil.ints.Int2FloatMap#Entry . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SlimUDTF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
private static double predict(final int user, final int itemI,
        @Nonnull final Int2ObjectMap<Int2FloatMap> knnItems, final int excludeIndex,
        @Nonnull final FloatMatrix weightMatrix) {
    final Int2FloatMap kNNu = knnItems.get(user);
    if (kNNu == null) {
        return 0.d;
    }

    double pred = 0.d;
    for (Int2FloatMap.Entry e : Fastutil.fastIterable(kNNu)) {
        final int itemK = e.getIntKey();
        if (itemK == excludeIndex) {
            continue;
        }
        float ruk = e.getFloatValue();
        pred += ruk * weightMatrix.get(itemI, itemK, 0.d);
    }
    return pred;
}
 
Example 2
Source File: LogarithmTermFrequencyTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected Int2FloatMap calculateTermFrequency(Int2FloatMap keyValues) {
    for (Int2FloatMap.Entry term : keyValues.int2FloatEntrySet()) {
        term.setValue((float) FastMath.log(1F + term.getFloatValue()));
    }
    return keyValues;
}
 
Example 3
Source File: BinaryTermFrequencyTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected Int2FloatMap calculateTermFrequency(Int2FloatMap keyValues) {
    for (Int2FloatMap.Entry term : keyValues.int2FloatEntrySet()) {
        term.setValue(1F);
    }
    return keyValues;
}
 
Example 4
Source File: MaximumInverseDocumentFrequencyTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected Int2FloatMap calculateInverseDocumentFrequency(Int2FloatMap keyValues) {
    for (Int2FloatMap.Entry term : keyValues.int2FloatEntrySet()) {
        term.setValue((float) FastMath.log(2F / (1F + term.getFloatValue())));
    }
    return keyValues;
}
 
Example 5
Source File: SmoothInverseDocumentFrequencyTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected Int2FloatMap calculateInverseDocumentFrequency(Int2FloatMap keyValues) {
    for (Int2FloatMap.Entry term : keyValues.int2FloatEntrySet()) {
        term.setValue((float) FastMath.log(1F + 2F / term.getFloatValue()));
    }
    return keyValues;
}
 
Example 6
Source File: UnaryInverseDocumentFrequencyTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected Int2FloatMap calculateInverseDocumentFrequency(Int2FloatMap keyValues) {
    for (Int2FloatMap.Entry term : keyValues.int2FloatEntrySet()) {
        term.setValue(1F);
    }
    return keyValues;
}
 
Example 7
Source File: NaturalInverseDocumentFrequencyTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected Int2FloatMap calculateInverseDocumentFrequency(Int2FloatMap keyValues) {
    for (Int2FloatMap.Entry term : keyValues.int2FloatEntrySet()) {
        term.setValue((float) FastMath.log(2F / term.getFloatValue()));
    }
    return keyValues;
}
 
Example 8
Source File: ProbabilisticInverseDocumentFrequencyTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected Int2FloatMap calculateInverseDocumentFrequency(Int2FloatMap keyValues) {
    for (Int2FloatMap.Entry term : keyValues.int2FloatEntrySet()) {
        term.setValue((float) FastMath.log((2F - term.getFloatValue()) / term.getFloatValue()));
    }
    return keyValues;
}
 
Example 9
Source File: HashInstance.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public HashInstance iterateQuantityFeatures(QuantityAccessor accessor) {
    for (Int2FloatMap.Entry term : this.quantityFeatures.int2FloatEntrySet()) {
        accessor.accessorFeature(term.getIntKey(), term.getFloatValue());
    }
    return this;
}
 
Example 10
Source File: SlimUDTF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
private void train(final int itemI, @Nonnull final Int2FloatMap ri,
        @Nonnull final Int2ObjectMap<Int2FloatMap> kNNi, final int itemJ,
        @Nonnull final Int2FloatMap rj) {
    final FloatMatrix W = _weightMatrix;

    final int N = rj.size();
    if (N == 0) {
        return;
    }

    double gradSum = 0.d;
    double rateSum = 0.d;
    double lossSum = 0.d;

    for (Int2FloatMap.Entry e : Fastutil.fastIterable(rj)) {
        int user = e.getIntKey();
        double ruj = e.getFloatValue();
        double rui = ri.get(user); // ri.getOrDefault(user, 0.f);

        double eui = rui - predict(user, itemI, kNNi, itemJ, W);
        gradSum += ruj * eui;
        rateSum += ruj * ruj;
        lossSum += eui * eui;
    }

    gradSum /= N;
    rateSum /= N;

    double wij = W.get(itemI, itemJ, 0.d);
    double loss = lossSum / N + 0.5d * l2 * wij * wij + l1 * wij;
    _cvState.incrLoss(loss);

    W.set(itemI, itemJ, getUpdateTerm(gradSum, rateSum, l1, l2));
}
 
Example 11
Source File: Fastutil.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ObjectIterable<Int2FloatMap.Entry> fastIterable(@Nonnull final Int2FloatMap map) {
    final ObjectSet<Int2FloatMap.Entry> entries = map.int2FloatEntrySet();
    return entries instanceof Int2FloatMap.FastEntrySet
            ? new ObjectIterable<Int2FloatMap.Entry>() {
                public ObjectIterator<Int2FloatMap.Entry> iterator() {
                    return ((Int2FloatMap.FastEntrySet) entries).fastIterator();
                }
            }
            : entries;
}
 
Example 12
Source File: SlimUDTF.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
private void recordTrainingInput(final int itemI,
        @Nonnull final Int2ObjectMap<Int2FloatMap> knnItems, final int numKNNItems)
        throws HiveException {
    ByteBuffer buf = this._inputBuf;
    NioStatefulSegment dst = this._fileIO;

    if (buf == null) {
        // invoke only at task node (initialize is also invoked in compilation)
        final File file;
        try {
            file = File.createTempFile("hivemall_slim", ".sgmt"); // to save KNN data
            file.deleteOnExit();
            if (!file.canWrite()) {
                throw new UDFArgumentException(
                    "Cannot write a temporary file: " + file.getAbsolutePath());
            }
        } catch (IOException ioe) {
            throw new UDFArgumentException(ioe);
        }

        this._inputBuf = buf = ByteBuffer.allocateDirect(8 * 1024 * 1024); // 8MB
        this._fileIO = dst = new NioStatefulSegment(file, false);
    }

    int recordBytes = SizeOf.INT + SizeOf.INT + SizeOf.INT * 2 * knnItems.size()
            + (SizeOf.INT + SizeOf.FLOAT) * numKNNItems;
    int requiredBytes = SizeOf.INT + recordBytes; // need to allocate space for "recordBytes" itself

    int remain = buf.remaining();
    if (remain < requiredBytes) {
        writeBuffer(buf, dst);
    }

    buf.putInt(recordBytes);
    buf.putInt(itemI);
    buf.putInt(knnItems.size());

    for (Int2ObjectMap.Entry<Int2FloatMap> e1 : Fastutil.fastIterable(knnItems)) {
        int user = e1.getIntKey();
        buf.putInt(user);

        Int2FloatMap ru = e1.getValue();
        buf.putInt(ru.size());
        for (Int2FloatMap.Entry e2 : Fastutil.fastIterable(ru)) {
            buf.putInt(e2.getIntKey());
            buf.putFloat(e2.getFloatValue());
        }
    }
}