Java Code Examples for com.jstarcraft.core.utility.RandomUtility#randomInteger()

The following examples show how to use com.jstarcraft.core.utility.RandomUtility#randomInteger() . 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: RandomDataSorter.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public ReferenceModule sort(DataModule module) {
    int size = module.getSize();
    IntegerArray reference = new IntegerArray(size, size);
    for (int index = 0; index < size; index++) {
        reference.associateData(index);
    }
    int from = 0;
    int to = size;
    for (int index = from; index < to; index++) {
        int random = RandomUtility.randomInteger(from, to);
        int data = reference.getData(index);
        reference.setData(index, reference.getData(random));
        reference.setData(random, data);
    }
    return new ReferenceModule(reference, module);
}
 
Example 2
Source File: DelayedTransienceStrategyTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testPerformance() throws Exception {
    Map<String, String> configuration = new HashMap<>();
    configuration.put(DelayedTransienceStrategy.PARAMETER_EXPIRE, String.valueOf(EXPIRE_SECONDS));
    configuration.put(DelayedTransienceStrategy.PARAMETER_SEGMENT, "3");
    DelayedTransienceStrategy strategy = new DelayedTransienceStrategy("stratiegy", configuration);
    strategy.start();
    TransienceManager manager = strategy.getTransienceManager(null);

    // 多线程并发读写操作
    int threadSize = 100;
    AtomicBoolean run = new AtomicBoolean(true);
    AtomicLong operationCount = new AtomicLong();
    for (int index = 0; index < threadSize; index++) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (run.get()) {
                    int readId = RandomUtility.randomInteger(0, DATA_SIZE);
                    int wirteId = RandomUtility.randomInteger(0, DATA_SIZE);
                    manager.retrieveInstance(readId);
                    manager.createInstance(wirteId, MockEntityObject.instanceOf(wirteId, "birdy" + wirteId, "hong", 0, 0));
                    operationCount.incrementAndGet();
                }
            }
        });
        thread.setDaemon(true);
        thread.start();
    }

    Thread.sleep(TimeUnit.MILLISECONDS.convert(EXPIRE_SECONDS, TimeUnit.SECONDS));
    run.set(false);
    if (manager.getSize() == 0) {
        Assert.fail();
    }

    String message = StringUtility.format("{}策略{}条线程在{}秒内执行{}次读写操作", strategy.getName(), threadSize, EXPIRE_SECONDS, operationCount.get());
    logger.debug(message);
}
 
Example 3
Source File: LeastRecentlyUsedTransienceStrategyTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testPerformance() throws Exception {
    Map<String, String> configuration = new HashMap<>();
    configuration.put(LeastRecentlyUsedTransienceStrategy.PARAMETER_MINIMUN_SIZE, String.valueOf(MINIMUN_SIZE));
    configuration.put(LeastRecentlyUsedTransienceStrategy.PARAMETER_MAXIMUN_SIZE, String.valueOf(MAXIMUN_SIZE));
    configuration.put(LeastRecentlyUsedTransienceStrategy.PARAMETER_CONCURRENCY_LEVEL, String.valueOf(THREAD_SIZE));
    LeastRecentlyUsedTransienceStrategy strategy = new LeastRecentlyUsedTransienceStrategy("stratiegy", configuration);
    strategy.start();
    TransienceManager manager = strategy.getTransienceManager(null);

    // 多线程并发读写操作
    int threadSize = 100;
    AtomicBoolean run = new AtomicBoolean(true);
    AtomicLong operationCount = new AtomicLong();
    for (int index = 0; index < threadSize; index++) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (run.get()) {
                    int readId = RandomUtility.randomInteger(0, MAXIMUN_SIZE + MINIMUN_SIZE);
                    int wirteId = RandomUtility.randomInteger(0, MAXIMUN_SIZE + MINIMUN_SIZE);
                    manager.createInstance(wirteId, MockEntityObject.instanceOf(wirteId, "birdy" + wirteId, "hong", 0, 0));
                    manager.retrieveInstance(readId);
                    operationCount.incrementAndGet();
                }
            }
        });
        thread.setDaemon(true);
        thread.start();
    }

    Thread.sleep(TimeUnit.MILLISECONDS.convert(EXPIRE_SECONDS, TimeUnit.SECONDS));
    run.set(false);
    if (manager.getSize() == 0) {
        Assert.fail();
    }

    String message = StringUtility.format("{}策略{}条线程在{}秒内执行{}次读写操作", strategy.getName(), threadSize, EXPIRE_SECONDS, operationCount.get());
    logger.debug(message);
}
 
Example 4
Source File: MockDataFactory.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
/**
 * user(离散:1:稠密)-item(离散:1:稠密)-score(连续:1:稠密)-instant(离散:1:稠密)-location(离散:2:稠密)-comment(连续:n:稀疏)
 */
@Test
public void mockUserItemScoreInstantLocationComment() throws Exception {
    File file = new File("data/mock/user-item-score-instant-location-comment");
    FileUtils.deleteQuietly(file);
    file.getParentFile().mkdirs();
    file.createNewFile();
    StringBuilder buffer = new StringBuilder();
    try (FileWriter writer = new FileWriter(file); BufferedWriter out = new BufferedWriter(writer);) {
        for (int leftIndex = 0; leftIndex < userSize; leftIndex++) {
            for (int rightIndex = 0; rightIndex < itemSize; rightIndex++) {
                // 此处故意选择特定的数据(TODO 考虑改为利用正态分布)
                if (rightIndex < 10 || RandomUtility.randomFloat(1F) < ratio) {
                    // 得分
                    float score = RandomUtility.randomFloat(scoreScope);
                    // 时间
                    int instant = RandomUtility.randomInteger(instantSize);
                    // 地点(经度)
                    int longitude = RandomUtility.randomInteger(locationSize);
                    // 地点(纬度)
                    int latitude = RandomUtility.randomInteger(locationSize);
                    buffer.setLength(0);
                    for (int commentIndex = 0; commentIndex < commentSize; commentIndex++) {
                        if (RandomUtility.randomFloat(1F) < ratio) {
                            float degree = RandomUtility.randomFloat(commentScope);
                            buffer.append(degree);
                        }
                        buffer.append(" ");
                    }
                    // 评论
                    String comment = buffer.substring(0, buffer.length() - 1);
                    out.write(StringUtility.format("{} {} {} {} {} {} {}", leftIndex, rightIndex, score, instant, longitude, latitude, comment));
                    out.newLine();
                }

            }
        }
    }
}
 
Example 5
Source File: BHFreeModel.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);
    userTopicSize = configuration.getInteger("recommender.bhfree.user.topic.number", 10);
    itemTopicSize = configuration.getInteger("recommender.bhfree.item.topic.number", 10);
    initAlpha = configuration.getFloat("recommender.bhfree.alpha", 1.0f / userTopicSize);
    initBeta = configuration.getFloat("recommender.bhfree.beta", 1.0f / itemTopicSize);
    initGamma = configuration.getFloat("recommender.bhfree.gamma", 1.0f / scoreSize);
    initSigma = configuration.getFloat("recommender.sigma", 1.0f / itemSize);
    scoreSize = scoreIndexes.size();

    // TODO 考虑重构(整合为UserTopic对象)
    user2TopicNumbers = DenseMatrix.valueOf(userSize, userTopicSize);
    userNumbers = DenseVector.valueOf(userSize);

    userTopic2ItemTopicNumbers = DenseMatrix.valueOf(userTopicSize, itemTopicSize);
    userTopicNumbers = DenseVector.valueOf(userTopicSize);

    userTopic2ItemTopicScoreNumbers = new int[userTopicSize][itemTopicSize][scoreSize];
    userTopic2ItemTopicItemNumbers = new int[userTopicSize][itemTopicSize][itemSize];

    topicMatrix = new SparseTable<>(true, userSize, itemSize, new Int2ObjectRBTreeMap<>());

    for (MatrixScalar term : scoreMatrix) {
        int userIndex = term.getRow();
        int itemIndex = term.getColumn();
        float score = term.getValue();
        int scoreIndex = scoreIndexes.get(score);
        int userTopic = RandomUtility.randomInteger(userTopicSize); // user's
        // topic
        // k
        int itemTopic = RandomUtility.randomInteger(itemTopicSize); // item's
        // topic
        // l

        user2TopicNumbers.shiftValue(userIndex, userTopic, 1F);
        userNumbers.shiftValue(userIndex, 1F);
        userTopic2ItemTopicNumbers.shiftValue(userTopic, itemTopic, 1F);
        userTopicNumbers.shiftValue(userTopic, 1F);
        userTopic2ItemTopicScoreNumbers[userTopic][itemTopic][scoreIndex]++;
        userTopic2ItemTopicItemNumbers[userTopic][itemTopic][itemIndex]++;
        TopicTerm topic = new TopicTerm(userTopic, itemTopic, scoreIndex);
        topicMatrix.setValue(userIndex, itemIndex, topic);
    }

    // parameters
    // TODO 考虑重构为一个对象
    user2TopicSums = DenseMatrix.valueOf(userSize, userTopicSize);
    userTopic2ItemTopicSums = DenseMatrix.valueOf(userTopicSize, itemTopicSize);
    userTopic2ItemTopicScoreSums = new double[userTopicSize][itemTopicSize][scoreSize];
    userTopic2ItemTopicScoreProbabilities = new double[userTopicSize][itemTopicSize][scoreSize];
    userTopic2ItemTopicItemSums = new double[userTopicSize][itemTopicSize][itemSize];
    userTopic2ItemTopicItemProbabilities = new double[userTopicSize][itemTopicSize][itemSize];

    topicProbabilities = DenseMatrix.valueOf(userTopicSize, itemTopicSize);
    userProbabilities = DenseVector.valueOf(userTopicSize);
    itemProbabilities = DenseVector.valueOf(itemTopicSize);
}
 
Example 6
Source File: QueuePersistenceStrategyTestCase.java    From jstarcraft-core with Apache License 2.0 4 votes vote down vote up
@Test
public void testInhibit() throws Exception {
    int size = 1000;
    PersistenceStrategy strategy = getPersistenceStrategy("strategy", getPersistenceConfiguration());
    strategy.start(accessor, cacheInformations);
    PersistenceManager<Integer, MockEntityObject> manager = strategy.getPersistenceManager(MockEntityObject.class);

    // 创建数据
    long begin = System.currentTimeMillis();
    for (int index = 0; index < size; index++) {
        manager.createInstance(MockEntityObject.instanceOf(index, "birdy" + index, "hong", index, index));
    }
    while (true) {
        if (manager.getWaitSize() == 0) {
            break;
        }
        Thread.sleep(1000);
    }
    long end = System.currentTimeMillis();
    String message = StringUtility.format("创建{}数据的时间:{}毫秒", size, end - begin);
    logger.debug(message);

    // 修改数据
    int times = 10000;
    begin = System.currentTimeMillis();
    for (int index = 0; index < times; index++) {
        int id = RandomUtility.randomInteger(0, 5);
        manager.updateInstance(MockEntityObject.instanceOf(id, "xiao" + index, "xiao", index * index, index * index));
        // 有抑制才需要检查等待数量的大小
        if (manager.getWaitSize() > 5) {
            Assert.fail();
        }
    }
    while (true) {
        if (manager.getWaitSize() == 0) {
            break;
        }
        Thread.sleep(1000);
    }
    end = System.currentTimeMillis();
    message = StringUtility.format("修改{}数据的时间:{}毫秒", times, end - begin);
    logger.debug(message);

    // 删除数据
    begin = System.currentTimeMillis();
    for (int index = 0; index < size; index++) {
        manager.deleteInstance(index);
    }
    while (true) {
        if (manager.getWaitSize() == 0) {
            break;
        }
        Thread.sleep(1000);
    }
    end = System.currentTimeMillis();
    message = StringUtility.format("删除{}数据的时间:{}毫秒", size, end - begin);
    logger.debug(message);

    strategy.stop();
}
 
Example 7
Source File: MinHashFamilyTestCase.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
@Override
protected float getRandomData() {
    return RandomUtility.randomInteger(2);
}
 
Example 8
Source File: SimHashFamilyTestCase.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
@Override
protected float getRandomData() {
	return RandomUtility.randomInteger(2);
}
 
Example 9
Source File: RowArrayMatrixTestCase.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
@Test
public void testNotify() {
    int dimension = 10;
    RowArrayMatrix matrix = getRandomMatrix(dimension);
    matrix.setValues(1F);

    try {
        matrix.getColumnVector(RandomUtility.randomInteger(dimension));
        Assert.fail();
    } catch (UnsupportedOperationException exception) {
    }

    ArrayVector vector = matrix.getRowVector(RandomUtility.randomInteger(dimension));
    int oldSize = vector.getElementSize();
    int newSize = RandomUtility.randomInteger(oldSize);
    int[] indexes = new int[newSize];
    for (int index = 0; index < newSize; index++) {
        indexes[index] = index;
    }
    SumMessage message = new SumMessage(false);
    matrix.attachMonitor((iterator, oldElementSize, newElementSize, oldKnownSize, newKnownSize, oldUnknownSize, newUnknownSize) -> {
        Assert.assertThat(newElementSize - oldElementSize, CoreMatchers.equalTo(newSize - oldSize));
        message.accumulateValue(oldSize + newSize);
    });
    vector.modifyIndexes(indexes);
    vector.setValues(1F);
    Assert.assertThat(message.getValue(), CoreMatchers.equalTo(oldSize + newSize + 0F));
    Assert.assertThat(matrix.getSum(false), CoreMatchers.equalTo(matrix.getElementSize() + 0F));

    message.accumulateValue(-message.getValue());
    matrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        message.accumulateValue(scalar.getValue());
    });
    Assert.assertThat(message.getValue(), CoreMatchers.equalTo(matrix.getSum(false)));

    message.accumulateValue(-message.getValue());
    for (MatrixScalar term : matrix) {
        message.accumulateValue(term.getValue());
    }
    Assert.assertThat(message.getValue(), CoreMatchers.equalTo(matrix.getSum(false)));
}
 
Example 10
Source File: ColumnArrayMatrixTestCase.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
@Test
public void testNotify() {
    int dimension = 10;
    ColumnArrayMatrix matrix = getRandomMatrix(dimension);
    matrix.setValues(1F);

    try {
        matrix.getRowVector(RandomUtility.randomInteger(dimension));
        Assert.fail();
    } catch (UnsupportedOperationException exception) {
    }

    ArrayVector vector = matrix.getColumnVector(RandomUtility.randomInteger(dimension));
    int oldSize = vector.getElementSize();
    int newSize = RandomUtility.randomInteger(oldSize);
    int[] indexes = new int[newSize];
    for (int index = 0; index < newSize; index++) {
        indexes[index] = index;
    }
    SumMessage message = new SumMessage(false);
    matrix.attachMonitor((iterator, oldElementSize, newElementSize, oldKnownSize, newKnownSize, oldUnknownSize, newUnknownSize) -> {
        Assert.assertThat(newElementSize - oldElementSize, CoreMatchers.equalTo(newSize - oldSize));
        message.accumulateValue(oldSize + newSize);
    });
    vector.modifyIndexes(indexes);
    vector.setValues(1F);
    Assert.assertThat(message.getValue(), CoreMatchers.equalTo(oldSize + newSize + 0F));
    Assert.assertThat(matrix.getSum(false), CoreMatchers.equalTo(matrix.getElementSize() + 0F));

    message.accumulateValue(-message.getValue());
    matrix.iterateElement(MathCalculator.SERIAL, (scalar) -> {
        message.accumulateValue(scalar.getValue());
    });
    Assert.assertThat(message.getValue(), CoreMatchers.equalTo(matrix.getSum(false)));

    message.accumulateValue(-message.getValue());
    for (MatrixScalar term : matrix) {
        message.accumulateValue(term.getValue());
    }
    Assert.assertThat(message.getValue(), CoreMatchers.equalTo(matrix.getSum(false)));
}
 
Example 11
Source File: LDCCModel.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);
    numberOfStatistics = 0;

    numberOfUserTopics = configuration.getInteger("recommender.pgm.number.users", 10);
    numberOfItemTopics = configuration.getInteger("recommender.pgm.number.items", 10);

    userAlpha = configuration.getFloat("recommender.pgm.user.alpha", 1F / numberOfUserTopics);
    itemAlpha = configuration.getFloat("recommender.pgm.item.alpha", 1F / numberOfItemTopics);
    ratingBeta = configuration.getFloat("recommender.pgm.rating.beta", 1F / actionSize);

    userTopicTimes = DenseMatrix.valueOf(userSize, numberOfUserTopics);
    itemTopicTimes = DenseMatrix.valueOf(itemSize, numberOfItemTopics);
    userScoreTimes = DenseVector.valueOf(userSize);
    itemScoreTimes = DenseVector.valueOf(itemSize);

    rateTopicTimes = new int[numberOfUserTopics][numberOfItemTopics][actionSize];
    topicTimes = DenseMatrix.valueOf(numberOfUserTopics, numberOfItemTopics);
    topicProbabilities = DenseMatrix.valueOf(numberOfUserTopics, numberOfItemTopics);
    userProbabilities = DenseVector.valueOf(numberOfUserTopics);
    itemProbabilities = DenseVector.valueOf(numberOfItemTopics);

    userTopics = new Int2IntRBTreeMap();
    itemTopics = new Int2IntRBTreeMap();

    for (MatrixScalar term : scoreMatrix) {
        int userIndex = term.getRow();
        int itemIndex = term.getColumn();
        float score = term.getValue();
        int scoreIndex = scoreIndexes.get(score);

        int userTopic = RandomUtility.randomInteger(numberOfUserTopics);
        int itemTopic = RandomUtility.randomInteger(numberOfItemTopics);

        userTopicTimes.shiftValue(userIndex, userTopic, 1);
        userScoreTimes.shiftValue(userIndex, 1);

        itemTopicTimes.shiftValue(itemIndex, itemTopic, 1);
        itemScoreTimes.shiftValue(itemIndex, 1);

        rateTopicTimes[userTopic][itemTopic][scoreIndex]++;
        topicTimes.shiftValue(userTopic, itemTopic, 1);

        userTopics.put(userIndex * itemSize + itemIndex, userTopic);
        itemTopics.put(userIndex * itemSize + itemIndex, itemTopic);
    }

    // parameters
    userTopicSums = DenseMatrix.valueOf(userSize, numberOfUserTopics);
    itemTopicSums = DenseMatrix.valueOf(itemSize, numberOfItemTopics);
    rateTopicProbabilities = new float[numberOfUserTopics][numberOfItemTopics][actionSize];
    rateTopicSums = new float[numberOfUserTopics][numberOfItemTopics][actionSize];
}
 
Example 12
Source File: LLORMAModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
private void practiceLocalModels(DefaultScalar scalar) {
    // Pre-calculating similarity:
    int completeModelCount = 0;

    // TODO 此处的变量与矩阵可以整合到LLORMALearner,LLORMALearner变成任务.
    LLORMALearner[] learners = new LLORMALearner[numberOfThreads];

    int modelCount = 0;
    int[] runningThreadList = new int[numberOfThreads];
    int runningThreadCount = 0;
    int waitingThreadPointer = 0;
    int nextRunningSlot = 0;

    // Parallel training:
    while (completeModelCount < numberOfModels) {
        int randomUserIndex = RandomUtility.randomInteger(userSize);
        // TODO 考虑重构
        SparseVector userVector = scoreMatrix.getRowVector(randomUserIndex);
        if (userVector.getElementSize() == 0) {
            continue;
        }
        // TODO 此处的并发模型有问题,需要重构.否则当第一次runningThreadCount >=
        // numThreads之后,都是单线程执行.
        if (runningThreadCount < numberOfThreads && modelCount < numberOfModels) {
            // Selecting a new anchor point:
            int randomItemIndex = userVector.getIndex(RandomUtility.randomInteger(userVector.getElementSize()));
            anchorUsers[modelCount] = randomUserIndex;
            anchorItems[modelCount] = randomItemIndex;
            // Preparing weight vectors:
            DenseVector userWeights = kernelSmoothing(scalar, userSize, randomUserIndex, KernelSmoother.EPANECHNIKOV_KERNEL, 0.8F, false);
            DenseVector itemWeights = kernelSmoothing(scalar, itemSize, randomItemIndex, KernelSmoother.EPANECHNIKOV_KERNEL, 0.8F, true);
            DenseMatrix localUserFactors = DenseMatrix.valueOf(userSize, numberOfLocalFactors);
            localUserFactors.iterateElement(MathCalculator.SERIAL, (element) -> {
                element.setValue(distribution.sample().floatValue());
            });
            DenseMatrix localItemFactors = DenseMatrix.valueOf(itemSize, numberOfLocalFactors);
            localItemFactors.iterateElement(MathCalculator.SERIAL, (element) -> {
                element.setValue(distribution.sample().floatValue());
            });
            // Starting a new local model learning:
            learners[nextRunningSlot] = new LLORMALearner(modelCount, numberOfLocalFactors, localLearnRatio, localUserRegularization, localItemRegularization, localEpocheSize, localUserFactors, localItemFactors, userWeights, itemWeights, scoreMatrix);
            learners[nextRunningSlot].start();
            runningThreadList[runningThreadCount] = modelCount;
            runningThreadCount++;
            modelCount++;
            nextRunningSlot++;
        } else if (runningThreadCount > 0) {
            // Joining a local model which was done with learning:
            try {
                learners[waitingThreadPointer].join();
            } catch (InterruptedException ie) {
                logger.error("Join failed: " + ie);
            }
            LLORMALearner learner = learners[waitingThreadPointer];
            userMatrixes[learner.getIndex()] = learner.getUserFactors();
            itemMatrixes[learner.getIndex()] = learner.getItemFactors();
            nextRunningSlot = waitingThreadPointer;
            waitingThreadPointer = (waitingThreadPointer + 1) % numberOfThreads;
            runningThreadCount--;
            completeModelCount++;
        }
    }
}
 
Example 13
Source File: BUCMModel.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);
    // cumulative parameters
    // TODO 考虑重构
    userTopicSums = DenseMatrix.valueOf(userSize, factorSize);
    topicItemSums = DenseMatrix.valueOf(factorSize, itemSize);
    topicItemScoreSums = new float[factorSize][itemSize][scoreSize];

    // initialize count varialbes
    userTopicNumbers = DenseMatrix.valueOf(userSize, factorSize);
    userNumbers = DenseVector.valueOf(userSize);

    topicItemNumbers = DenseMatrix.valueOf(factorSize, itemSize);
    topicNumbers = DenseVector.valueOf(factorSize);

    topicItemScoreNumbers = new int[factorSize][itemSize][scoreSize];

    float initAlpha = configuration.getFloat("recommender.bucm.alpha", 1F / factorSize);
    alpha = DenseVector.valueOf(factorSize);
    alpha.setValues(initAlpha);

    float initBeta = configuration.getFloat("re.bucm.beta", 1F / itemSize);
    beta = DenseVector.valueOf(itemSize);
    beta.setValues(initBeta);

    float initGamma = configuration.getFloat("recommender.bucm.gamma", 1F / factorSize);
    gamma = DenseVector.valueOf(scoreSize);
    gamma.setValues(initGamma);

    // initialize topics
    topicAssignments = new Int2IntRBTreeMap();
    for (MatrixScalar term : scoreMatrix) {
        int userIndex = term.getRow();
        int itemIndex = term.getColumn();
        float score = term.getValue();
        int scoreIndex = scoreIndexes.get(score); // rating level 0 ~
                                                  // numLevels
        int topicIndex = RandomUtility.randomInteger(factorSize); // 0 ~
        // k-1

        // Assign a topic t to pair (u, i)
        topicAssignments.put(userIndex * itemSize + itemIndex, topicIndex);
        // for users
        userTopicNumbers.shiftValue(userIndex, topicIndex, 1F);
        userNumbers.shiftValue(userIndex, 1F);

        // for items
        topicItemNumbers.shiftValue(topicIndex, itemIndex, 1F);
        topicNumbers.shiftValue(topicIndex, 1F);

        // for ratings
        topicItemScoreNumbers[topicIndex][itemIndex][scoreIndex]++;
    }

    probabilities = DenseVector.valueOf(factorSize);
}
 
Example 14
Source File: LambdaFMWeightModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
protected float getGradientValue(DataModule[] modules, ArrayInstance positive, ArrayInstance negative, DefaultScalar scalar) {
    int userIndex;
    float positiveScore;
    float negativeScore;
    while (true) {
        userIndex = RandomUtility.randomInteger(userSize);
        SparseVector userVector = scoreMatrix.getRowVector(userIndex);
        if (userVector.getElementSize() == 0 || userVector.getElementSize() == itemSize) {
            continue;
        }

        N = 0;
        Y = itemSize - scoreMatrix.getRowScope(userIndex);
        DataModule module = modules[userIndex];
        DataInstance instance = module.getInstance(0);
        int positivePosition = RandomUtility.randomInteger(module.getSize());
        instance.setCursor(positivePosition);
        positive.copyInstance(instance);
        positiveVector = getFeatureVector(positive);
        positiveScore = predict(scalar, positiveVector);
        do {
            N++;
            int negativeItemIndex = RandomUtility.randomInteger(itemSize - userVector.getElementSize());
            for (int position = 0, size = userVector.getElementSize(); position < size; position++) {
                if (negativeItemIndex >= userVector.getIndex(position)) {
                    negativeItemIndex++;
                    continue;
                }
                break;
            }
            // TODO 注意,此处为了故意制造负面特征.
            int negativePosition = RandomUtility.randomInteger(module.getSize());
            // TODO 注意,此处为了故意制造负面特征.
            instance.setCursor(negativePosition);
            negative.copyInstance(instance);
            negative.setQualityFeature(itemDimension, negativeItemIndex);
            negativeVector = getFeatureVector(negative);
            negativeScore = predict(scalar, negativeVector);
        } while ((positiveScore - negativeScore > epsilon) && N < Y - 1);
        break;
    }

    float error = positiveScore - negativeScore;

    // 由于pij_real默认为1,所以简化了loss的计算.
    // loss += -pij_real * Math.log(pij) - (1 - pij_real) *
    // Math.log(1 - pij);
    totalError += (float) -Math.log(LogisticUtility.getValue(error));
    float gradient = calaculateGradientValue(lossType, error);
    int orderIndex = (int) ((Y - 1) / N);
    float orderLoss = orderLosses[orderIndex];
    gradient = gradient * orderLoss;
    return gradient;
}
 
Example 15
Source File: WARPMFModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
protected void doPractice() {
    int Y, N;

    for (int epocheIndex = 0; epocheIndex < epocheSize; epocheIndex++) {
        totalError = 0F;
        for (int sampleIndex = 0, sampleTimes = userSize * 100; sampleIndex < sampleTimes; sampleIndex++) {
            int userIndex, positiveItemIndex, negativeItemIndex;
            float positiveScore;
            float negativeScore;
            while (true) {
                userIndex = RandomUtility.randomInteger(userSize);
                SparseVector userVector = scoreMatrix.getRowVector(userIndex);
                if (userVector.getElementSize() == 0 || userVector.getElementSize() == itemSize) {
                    continue;
                }

                N = 0;
                Y = itemSize - scoreMatrix.getRowScope(userIndex);
                positiveItemIndex = userVector.getIndex(RandomUtility.randomInteger(userVector.getElementSize()));
                positiveScore = predict(userIndex, positiveItemIndex);
                do {
                    N++;
                    negativeItemIndex = RandomUtility.randomInteger(itemSize - userVector.getElementSize());
                    for (int index = 0, size = userVector.getElementSize(); index < size; index++) {
                        if (negativeItemIndex >= userVector.getIndex(index)) {
                            negativeItemIndex++;
                            continue;
                        }
                        break;
                    }
                    negativeScore = predict(userIndex, negativeItemIndex);
                } while ((positiveScore - negativeScore > epsilon) && N < Y - 1);
                break;
            }
            // update parameters
            float error = positiveScore - negativeScore;

            float gradient = calaculateGradientValue(lossType, error);
            int orderIndex = (int) ((Y - 1) / N);
            float orderLoss = orderLosses[orderIndex];
            gradient = gradient * orderLoss;

            totalError += -Math.log(LogisticUtility.getValue(error));

            for (int factorIndex = 0; factorIndex < factorSize; factorIndex++) {
                float userFactor = userFactors.getValue(userIndex, factorIndex);
                float positiveFactor = itemFactors.getValue(positiveItemIndex, factorIndex);
                float negativeFactor = itemFactors.getValue(negativeItemIndex, factorIndex);

                userFactors.shiftValue(userIndex, factorIndex, learnRatio * (gradient * (positiveFactor - negativeFactor) - userRegularization * userFactor));
                itemFactors.shiftValue(positiveItemIndex, factorIndex, learnRatio * (gradient * userFactor - itemRegularization * positiveFactor));
                itemFactors.shiftValue(negativeItemIndex, factorIndex, learnRatio * (gradient * (-userFactor) - itemRegularization * negativeFactor));
                totalError += userRegularization * userFactor * userFactor + itemRegularization * positiveFactor * positiveFactor + itemRegularization * negativeFactor * negativeFactor;
            }
        }

        if (isConverged(epocheIndex) && isConverged) {
            break;
        }
        isLearned(epocheIndex);
        currentError = totalError;
    }
}
 
Example 16
Source File: LambdaFMStaticModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
protected float getGradientValue(DataModule[] modules, ArrayInstance positive, ArrayInstance negative, DefaultScalar scalar) {
    int userIndex;
    while (true) {
        userIndex = RandomUtility.randomInteger(userSize);
        SparseVector userVector = scoreMatrix.getRowVector(userIndex);
        if (userVector.getElementSize() == 0 || userVector.getElementSize() == itemSize) {
            continue;
        }

        DataModule module = modules[userIndex];
        DataInstance instance = module.getInstance(0);
        int positivePosition = RandomUtility.randomInteger(module.getSize());
        instance.setCursor(positivePosition);
        positive.copyInstance(instance);

        // TODO 注意,此处为了故意制造负面特征.
        int negativeItemIndex = -1;
        while (negativeItemIndex == -1) {
            int position = SampleUtility.binarySearch(userVector, 0, userVector.getElementSize() - 1, RandomUtility.randomFloat(itemProbabilities.getValue(itemProbabilities.getElementSize() - 1)));
            int low;
            int high;
            if (position == -1) {
                low = userVector.getIndex(userVector.getElementSize() - 1);
                high = itemProbabilities.getElementSize() - 1;
            } else if (position == 0) {
                low = 0;
                high = userVector.getIndex(position);
            } else {
                low = userVector.getIndex(position - 1);
                high = userVector.getIndex(position);
            }
            negativeItemIndex = SampleUtility.binarySearch(itemProbabilities, low, high, RandomUtility.randomFloat(itemProbabilities.getValue(high)));
        }
        int negativePosition = RandomUtility.randomInteger(module.getSize());
        ;
        instance.setCursor(negativePosition);
        negative.copyInstance(instance);
        negative.setQualityFeature(itemDimension, negativeItemIndex);
        break;
    }

    positiveVector = getFeatureVector(positive);
    negativeVector = getFeatureVector(negative);

    float positiveScore = predict(scalar, positiveVector);
    float negativeScore = predict(scalar, negativeVector);

    float error = positiveScore - negativeScore;

    // 由于pij_real默认为1,所以简化了loss的计算.
    // loss += -pij_real * Math.log(pij) - (1 - pij_real) *
    // Math.log(1 - pij);
    totalError += (float) -Math.log(LogisticUtility.getValue(error));
    float gradient = calaculateGradientValue(lossType, error);
    return gradient;
}
 
Example 17
Source File: WBPRModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
protected void doPractice() {
    for (int epocheIndex = 0; epocheIndex < epocheSize; epocheIndex++) {
        totalError = 0F;
        for (int sampleIndex = 0, sampleTimes = userSize * 100; sampleIndex < sampleTimes; sampleIndex++) {
            // randomly draw (userIdx, posItemIdx, negItemIdx)
            int userIndex, positiveItemIndex, negativeItemIndex = 0;
            List<KeyValue<Integer, Double>> probabilities;
            while (true) {
                userIndex = RandomUtility.randomInteger(userSize);
                SparseVector userVector = scoreMatrix.getRowVector(userIndex);
                if (userVector.getElementSize() == 0) {
                    continue;
                }
                positiveItemIndex = userVector.getIndex(RandomUtility.randomInteger(userVector.getElementSize()));
                // sample j by popularity (probability)
                probabilities = itemProbabilities[userIndex];
                double random = RandomUtility.randomDouble(1D);
                for (KeyValue<Integer, Double> term : probabilities) {
                    if ((random -= term.getValue()) <= 0D) {
                        negativeItemIndex = term.getKey();
                        break;
                    }
                }
                break;
            }

            // update parameters
            float positiveScore = predict(userIndex, positiveItemIndex);
            float negativeScore = predict(userIndex, negativeItemIndex);
            float error = positiveScore - negativeScore;
            float value = (float) -Math.log(LogisticUtility.getValue(error));
            totalError += value;
            value = LogisticUtility.getValue(-error);

            // update bias
            float positiveBias = itemBiases.getValue(positiveItemIndex), negativeBias = itemBiases.getValue(negativeItemIndex);
            itemBiases.shiftValue(positiveItemIndex, learnRatio * (value - biasRegularization * positiveBias));
            itemBiases.shiftValue(negativeItemIndex, learnRatio * (-value - biasRegularization * negativeBias));
            totalError += biasRegularization * (positiveBias * positiveBias + negativeBias * negativeBias);

            // update user/item vectors
            for (int factorIndex = 0; factorIndex < factorSize; factorIndex++) {
                float userFactor = userFactors.getValue(userIndex, factorIndex);
                float positiveItemFactor = itemFactors.getValue(positiveItemIndex, factorIndex);
                float negativeItemFactor = itemFactors.getValue(negativeItemIndex, factorIndex);
                userFactors.shiftValue(userIndex, factorIndex, learnRatio * (value * (positiveItemFactor - negativeItemFactor) - userRegularization * userFactor));
                itemFactors.shiftValue(positiveItemIndex, factorIndex, learnRatio * (value * userFactor - itemRegularization * positiveItemFactor));
                itemFactors.shiftValue(negativeItemIndex, factorIndex, learnRatio * (value * (-userFactor) - itemRegularization * negativeItemFactor));
                totalError += userRegularization * userFactor * userFactor + itemRegularization * positiveItemFactor * positiveItemFactor + itemRegularization * negativeItemFactor * negativeItemFactor;
            }
        }
        if (isConverged(epocheIndex) && isConverged) {
            break;
        }
        isLearned(epocheIndex);
        currentError = totalError;
    }
}
 
Example 18
Source File: BPRModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
protected void doPractice() {
    for (int epocheIndex = 0; epocheIndex < epocheSize; epocheIndex++) {
        totalError = 0F;
        for (int sampleIndex = 0, sampleTimes = userSize * 100; sampleIndex < sampleTimes; sampleIndex++) {
            // randomly draw (userIdx, posItemIdx, negItemIdx)
            int userIndex, positiveItemIndex, negativeItemIndex;
            while (true) {
                userIndex = RandomUtility.randomInteger(userSize);
                SparseVector userVector = scoreMatrix.getRowVector(userIndex);
                if (userVector.getElementSize() == 0) {
                    continue;
                }
                positiveItemIndex = userVector.getIndex(RandomUtility.randomInteger(userVector.getElementSize()));
                negativeItemIndex = RandomUtility.randomInteger(itemSize - userVector.getElementSize());
                for (VectorScalar term : userVector) {
                    if (negativeItemIndex >= term.getIndex()) {
                        negativeItemIndex++;
                    } else {
                        break;
                    }
                }
                break;
            }

            // update parameters
            float positiveScore = predict(userIndex, positiveItemIndex);
            float negativeScore = predict(userIndex, negativeItemIndex);
            float error = positiveScore - negativeScore;
            float value = (float) -Math.log(LogisticUtility.getValue(error));
            totalError += value;
            value = LogisticUtility.getValue(-error);

            for (int factorIndex = 0; factorIndex < factorSize; factorIndex++) {
                float userFactor = userFactors.getValue(userIndex, factorIndex);
                float positiveFactor = itemFactors.getValue(positiveItemIndex, factorIndex);
                float negativeFactor = itemFactors.getValue(negativeItemIndex, factorIndex);
                userFactors.shiftValue(userIndex, factorIndex, learnRatio * (value * (positiveFactor - negativeFactor) - userRegularization * userFactor));
                itemFactors.shiftValue(positiveItemIndex, factorIndex, learnRatio * (value * userFactor - itemRegularization * positiveFactor));
                itemFactors.shiftValue(negativeItemIndex, factorIndex, learnRatio * (value * (-userFactor) - itemRegularization * negativeFactor));
                totalError += userRegularization * userFactor * userFactor + itemRegularization * positiveFactor * positiveFactor + itemRegularization * negativeFactor * negativeFactor;
            }
        }
        if (isConverged(epocheIndex) && isConverged) {
            break;
        }
        isLearned(epocheIndex);
        currentError = totalError;
    }
}