Java Code Examples for com.jstarcraft.ai.data.DataInstance#getQualityFeature()

The following examples show how to use com.jstarcraft.ai.data.DataInstance#getQualityFeature() . 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: BUCMRankingModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float value = 0F;
    for (int topicIndex = 0; topicIndex < factorSize; ++topicIndex) {
        float sum = 0F;
        for (Entry<Float, Integer> term : scoreIndexes.entrySet()) {
            double score = term.getKey();
            if (score > meanScore) {
                sum += topicItemScoreProbabilities[topicIndex][itemIndex][term.getValue()];
            }
        }
        value += userTopicProbabilities.getValue(userIndex, topicIndex) * topicItemProbabilities.getValue(topicIndex, itemIndex) * sum;
    }
    instance.setQuantityMark(value);
}
 
Example 2
Source File: BHFreeRankingModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float value = 0F;
    for (Entry<Float, Integer> entry : scoreIndexes.entrySet()) {
        float score = entry.getKey();
        float probability = 0F;
        for (int userTopic = 0; userTopic < userTopicSize; userTopic++) {
            for (int itemTopic = 0; itemTopic < itemTopicSize; itemTopic++) {
                probability += user2TopicProbabilities.getValue(userIndex, userTopic) * userTopic2ItemTopicProbabilities.getValue(userTopic, itemTopic) * userTopic2ItemTopicItemSums[userTopic][itemTopic][itemIndex] * userTopic2ItemTopicScoreProbabilities[userTopic][itemTopic][entry.getValue()];
            }
        }
        value += score * probability;
    }
    instance.setQuantityMark(value);
}
 
Example 3
Source File: DataSorterTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Test
public void testSort() {
    DataModule module = getDataModule();
    DataSorter sorter = new DataSorter() {

        @Override
        public int sort(DataInstance left, DataInstance right) {
            return right.getQualityFeature(0) - left.getQualityFeature(0);
        }

    };
    module = sorter.sort(module);
    for (int index = 0; index < instanceCapacity; index++) {
        Assert.assertEquals(instanceCapacity - 1 - index, module.getInstance(index).getQualityFeature(0));
    }
}
 
Example 4
Source File: UserClusterModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float value = 0F;
    for (int topicIndex = 0; topicIndex < factorSize; topicIndex++) {
        float topicProbability = userTopicProbabilities.getValue(userIndex, topicIndex);
        float topicValue = 0F;
        for (Entry<Float, Integer> entry : scoreIndexes.entrySet()) {
            float score = entry.getKey();
            float probability = topicScoreMatrix.getValue(topicIndex, entry.getValue());
            topicValue += score * probability;
        }
        value += topicProbability * topicValue;
    }
    instance.setQuantityMark(value);
}
 
Example 5
Source File: FISMRMSEModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    DefaultScalar scalar = DefaultScalar.getInstance();
    float bias = userBiases.getValue(userIndex) + itemBiases.getValue(itemIndex);
    float sum = 0F;
    int count = 0;
    for (VectorScalar term : scoreMatrix.getRowVector(userIndex)) {
        int index = term.getIndex();
        // for test, i and j will be always unequal as j is unrated
        if (index != itemIndex) {
            DenseVector userVector = userFactors.getRowVector(index);
            DenseVector itemVector = itemFactors.getRowVector(itemIndex);
            sum += scalar.dotProduct(userVector, itemVector).getValue();
            count++;
        }
    }
    sum *= (float) (count > 0 ? Math.pow(count, -alpha) : 0F);
    instance.setQuantityMark(bias + sum);
}
 
Example 6
Source File: RankGeoFMModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    DefaultScalar scalar = DefaultScalar.getInstance();
    float value = scalar.dotProduct(explicitUserFactors.getRowVector(userIndex), itemFactors.getRowVector(itemIndex)).getValue();
    value += scalar.dotProduct(implicitUserFactors.getRowVector(userIndex), geoInfluences.getRowVector(itemIndex)).getValue();
    instance.setQuantityMark(value);
}
 
Example 7
Source File: RankVFCDModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    DefaultScalar scalar = DefaultScalar.getInstance();
    float score = 0F;
    if (scoreMatrix.getColumnVector(itemIndex).getElementSize() == 0) {
        score = scalar.dotProduct(userFactors.getRowVector(userIndex), factorMatrix.getColumnVector(itemIndex)).getValue();
    } else {
        score = scalar.dotProduct(userFactors.getRowVector(userIndex), explicitItemFactors.getRowVector(itemIndex)).getValue();
    }
    instance.setQuantityMark(score);
}
 
Example 8
Source File: SLIMModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
/**
 * predict a specific ranking score for user userIdx on item itemIdx.
 *
 * @param userIndex user index
 * @param itemIndex item index
 * @return predictive ranking score for user userIdx on item itemIdx
 * @throws ModelException if error occurs
 */
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    int[] neighbors = itemNeighbors[itemIndex];
    if (neighbors == null) {
        instance.setQuantityMark(0F);
        return;
    }
    instance.setQuantityMark(predict(userIndex, itemIndex, neighbors, -1));
}
 
Example 9
Source File: CCDModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    DefaultScalar scalar = DefaultScalar.getInstance();
    float score = scalar.dotProduct(userFactors.getRowVector(userIndex), itemFactors.getRowVector(itemIndex)).getValue();
    if (score == 0F) {
        score = meanScore;
    }
    instance.setQuantityMark(score);
}
 
Example 10
Source File: ItemBigramModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    List<Integer> items = userItemMap.get(userIndex);
    int scoreIndex = items == null ? itemSize : items.get(items.size() - 1); // last
    // rated
    // item
    float value = 0F;
    for (int topicIndex = 0; topicIndex < factorSize; topicIndex++) {
        value += userTopicProbabilities.getValue(userIndex, topicIndex) * topicItemBigramProbabilities[topicIndex][scoreIndex][itemIndex];
    }

    instance.setQuantityMark(value);
}
 
Example 11
Source File: DeepCrossModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    float value = outputData.getValue(userIndex, itemIndex);
    instance.setQuantityMark(value);
}
 
Example 12
Source File: VBPRModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    instance.setQuantityMark(predict(userIndex, itemIndex, featureVector.getValue(itemIndex), factorMatrix.getColumnVector(itemIndex)));
}
 
Example 13
Source File: TopicMFATModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
public void prepare(Configurator configuration, DataModule model, DataSpace space) {
    super.prepare(configuration, model, space);

    commentField = configuration.getString("data.model.fields.comment");
    commentDimension = model.getQualityInner(commentField);
    MemoryQualityAttribute attribute = (MemoryQualityAttribute) space.getQualityAttribute(commentField);
    Object[] documentValues = attribute.getDatas();

    // init hyper-parameters
    lambda = configuration.getFloat("recommender.regularization.lambda", 0.001F);
    lambdaU = configuration.getFloat("recommender.regularization.lambdaU", 0.001F);
    lambdaV = configuration.getFloat("recommender.regularization.lambdaV", 0.001F);
    lambdaB = configuration.getFloat("recommender.regularization.lambdaB", 0.001F);
    numberOfTopics = configuration.getInteger("recommender.topic.number", 10);
    learnRatio = configuration.getFloat("recommender.iterator.learnrate", 0.01F);
    epocheSize = configuration.getInteger("recommender.iterator.maximum", 10);

    numberOfDocuments = scoreMatrix.getElementSize();

    // count the number of words, build the word dictionary and
    // userItemToDoc dictionary
    Map<String, Integer> wordDictionaries = new HashMap<>();
    Table<Integer, Integer, Float> documentTable = HashBasedTable.create();
    // TODO rowCount改为documentIndex?
    int rowCount = 0;
    userItemToDocument = HashBasedTable.create();
    for (DataInstance sample : model) {
        int userIndex = sample.getQualityFeature(userDimension);
        int itemIndex = sample.getQualityFeature(itemDimension);
        int documentIndex = sample.getQualityFeature(commentDimension);
        userItemToDocument.put(userIndex, itemIndex, rowCount);
        // convert wordIds to wordIndices
        String data = (String) documentValues[documentIndex];
        String[] words = data.isEmpty() ? new String[0] : data.split(":");
        for (String word : words) {
            Integer wordIndex = wordDictionaries.get(word);
            if (wordIndex == null) {
                wordIndex = numberOfWords++;
                wordDictionaries.put(word, wordIndex);
            }
            Float oldValue = documentTable.get(rowCount, wordIndex);
            if (oldValue == null) {
                oldValue = 0F;
            }
            float newValue = oldValue + 1F / words.length;
            documentTable.put(rowCount, wordIndex, newValue);
        }
        rowCount++;
    }
    // build W
    W = SparseMatrix.valueOf(numberOfDocuments, numberOfWords, documentTable);

    userBiases = DenseVector.valueOf(userSize);
    userBiases.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(distribution.sample().floatValue());
    });
    itemBiases = DenseVector.valueOf(itemSize);
    itemBiases.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(distribution.sample().floatValue());
    });
    userFactors = DenseMatrix.valueOf(userSize, numberOfTopics);
    userFactors.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(distribution.sample().floatValue());
    });
    itemFactors = DenseMatrix.valueOf(itemSize, numberOfTopics);
    itemFactors.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(distribution.sample().floatValue());
    });

    K1 = initStd;
    K2 = initStd;

    topicVector = DenseVector.valueOf(numberOfTopics);
    function = new SoftMaxActivationFunction();

    // init theta and phi
    // TODO theta实际是documentFactors
    documentFactors = DenseMatrix.valueOf(numberOfDocuments, numberOfTopics);
    calculateTheta();
    // TODO phi实际是wordFactors
    wordFactors = DenseMatrix.valueOf(numberOfTopics, numberOfWords);
    wordFactors.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomFloat(0.01F));
    });

    logger.info("number of users : " + userSize);
    logger.info("number of Items : " + itemSize);
    logger.info("number of words : " + wordDictionaries.size());
}
 
Example 14
Source File: TFIDFModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    instance.setQuantityMark(predict(userIndex, itemIndex));
}
 
Example 15
Source File: URPModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    instance.setQuantityMark(predict(userIndex, itemIndex));
}
 
Example 16
Source File: TopicMFMTModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
public void prepare(Configurator configuration, DataModule model, DataSpace space) {
    super.prepare(configuration, model, space);

    commentField = configuration.getString("data.model.fields.comment");
    commentDimension = model.getQualityInner(commentField);
    MemoryQualityAttribute attribute = (MemoryQualityAttribute) space.getQualityAttribute(commentField);
    Object[] documentValues = attribute.getDatas();

    // init hyper-parameters
    lambda = configuration.getFloat("recommender.regularization.lambda", 0.001F);
    lambdaU = configuration.getFloat("recommender.regularization.lambdaU", 0.001F);
    lambdaV = configuration.getFloat("recommender.regularization.lambdaV", 0.001F);
    lambdaB = configuration.getFloat("recommender.regularization.lambdaB", 0.001F);
    numberOfTopics = configuration.getInteger("recommender.topic.number", 10);
    learnRatio = configuration.getFloat("recommender.iterator.learnrate", 0.01F);
    epocheSize = configuration.getInteger("recommender.iterator.maximum", 10);

    numberOfDocuments = scoreMatrix.getElementSize();

    // count the number of words, build the word dictionary and
    // userItemToDoc dictionary
    Map<String, Integer> wordDictionaries = new HashMap<>();
    Table<Integer, Integer, Float> documentTable = HashBasedTable.create();
    int rowCount = 0;
    userItemToDocument = HashBasedTable.create();
    for (DataInstance sample : model) {
        int userIndex = sample.getQualityFeature(userDimension);
        int itemIndex = sample.getQualityFeature(itemDimension);
        int documentIndex = sample.getQualityFeature(commentDimension);
        userItemToDocument.put(userIndex, itemIndex, rowCount);
        // convert wordIds to wordIndices
        String data = (String) documentValues[documentIndex];
        String[] words = data.isEmpty() ? new String[0] : data.split(":");
        for (String word : words) {
            Integer wordIndex = wordDictionaries.get(word);
            if (wordIndex == null) {
                wordIndex = numberOfWords++;
                wordDictionaries.put(word, wordIndex);
            }
            Float oldValue = documentTable.get(rowCount, wordIndex);
            if (oldValue == null) {
                oldValue = 0F;
            }
            float newValue = oldValue + 1F / words.length;
            documentTable.put(rowCount, wordIndex, newValue);
        }
        rowCount++;
    }
    // build W
    W = SparseMatrix.valueOf(numberOfDocuments, numberOfWords, documentTable);

    userBiases = DenseVector.valueOf(userSize);
    userBiases.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(distribution.sample().floatValue());
    });
    itemBiases = DenseVector.valueOf(itemSize);
    itemBiases.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(distribution.sample().floatValue());
    });
    userFactors = DenseMatrix.valueOf(userSize, numberOfTopics);
    userFactors.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(distribution.sample().floatValue());
    });
    itemFactors = DenseMatrix.valueOf(itemSize, numberOfTopics);
    itemFactors.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(distribution.sample().floatValue());
    });
    K = initStd;

    topicVector = DenseVector.valueOf(numberOfTopics);
    function = new SoftMaxActivationFunction();

    // init theta and phi
    // TODO theta实际是documentFactors
    documentFactors = DenseMatrix.valueOf(numberOfDocuments, numberOfTopics);
    calculateTheta();
    // TODO phi实际是wordFactors
    wordFactors = DenseMatrix.valueOf(numberOfTopics, numberOfWords);
    wordFactors.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        scalar.setValue(RandomUtility.randomFloat(0.01F));
    });

    logger.info("number of users : " + userSize);
    logger.info("number of Items : " + itemSize);
    logger.info("number of words : " + wordDictionaries.size());
}
 
Example 17
Source File: CDAEModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    instance.setQuantityMark(outputData.getFloat(userIndex, itemIndex));
}
 
Example 18
Source File: ItemKNNRatingModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    SparseVector userVector = userVectors[userIndex];
    MathVector neighbors = itemNeighbors[itemIndex];
    if (userVector.getElementSize() == 0 || neighbors.getElementSize() == 0) {
        instance.setQuantityMark(meanScore);
        return;
    }

    float sum = 0F, absolute = 0F;
    int count = 0;
    int leftCursor = 0, rightCursor = 0, leftSize = userVector.getElementSize(), rightSize = neighbors.getElementSize();
    Iterator<VectorScalar> leftIterator = userVector.iterator();
    VectorScalar leftTerm = leftIterator.next();
    Iterator<VectorScalar> rightIterator = neighbors.iterator();
    VectorScalar rightTerm = rightIterator.next();
    // 判断两个有序数组中是否存在相同的数字
    while (leftCursor < leftSize && rightCursor < rightSize) {
        if (leftTerm.getIndex() == rightTerm.getIndex()) {
            count++;
            float correlation = rightTerm.getValue();
            float score = leftTerm.getValue();
            sum += correlation * (score - itemMeans.getValue(rightTerm.getIndex()));
            absolute += Math.abs(correlation);
            if (leftIterator.hasNext()) {
                leftTerm = leftIterator.next();
            }
            if (rightIterator.hasNext()) {
                rightTerm = rightIterator.next();
            }
            leftCursor++;
            rightCursor++;
        } else if (leftTerm.getIndex() > rightTerm.getIndex()) {
            if (rightIterator.hasNext()) {
                rightTerm = rightIterator.next();
            }
            rightCursor++;
        } else if (leftTerm.getIndex() < rightTerm.getIndex()) {
            if (leftIterator.hasNext()) {
                leftTerm = leftIterator.next();
            }
            leftCursor++;
        }
    }

    if (count == 0) {
        instance.setQuantityMark(meanScore);
        return;
    }

    instance.setQuantityMark(absolute > 0 ? itemMeans.getValue(itemIndex) + sum / absolute : meanScore);
}
 
Example 19
Source File: BiasedMFModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
public void predict(DataInstance instance) {
    int userIndex = instance.getQualityFeature(userDimension);
    int itemIndex = instance.getQualityFeature(itemDimension);
    instance.setQuantityMark(predict(userIndex, itemIndex));
}
 
Example 20
Source File: QualityFeatureDataSplitter.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
public int split(DataInstance instance) {
    return instance.getQualityFeature(dimension);
}