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

The following examples show how to use com.jstarcraft.core.utility.RandomUtility#randomFloat() . 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: CDAEModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
protected void doPractice() {
    Graph graph = getComputationGraph();
    for (int epocheIndex = 0; epocheIndex < epocheSize; epocheIndex++) {
        inputData.getArray().assign(labelData.getArray());
        for (MatrixScalar term : scoreMatrix) {
            if (RandomUtility.randomFloat(1F) < 0.2F) {
                inputData.setValue(term.getRow(), term.getColumn(), 0F);
            }
        }
        totalError = graph.practice(1, new MathMatrix[] { inputData }, new MathMatrix[] { labelData });
        if (isConverged(epocheIndex) && isConverged) {
            break;
        }
        currentError = totalError;
    }
    graph.predict(new MathMatrix[] { labelData }, new MathMatrix[] { outputData });
}
 
Example 2
Source File: RandomSeparator.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
public RandomSeparator(DataSpace space, DataModule dataModule, String matchField, float random) {
    this.dataModule = dataModule;
    ReferenceModule[] modules;
    if (matchField == null) {
        modules = new ReferenceModule[] { new ReferenceModule(dataModule) };
    } else {
        int matchDimension = dataModule.getQualityInner(matchField);
        DataSplitter splitter = new QualityFeatureDataSplitter(matchDimension);
        int size = space.getQualityAttribute(matchField).getSize();
        modules = splitter.split(dataModule, size);
    }
    this.trainReference = new IntegerArray();
    this.testReference = new IntegerArray();
    for (ReferenceModule module : modules) {
        IntegerArray reference = module.getReference();
        for (int cursor = 0, length = reference.getSize(); cursor < length; cursor++) {
            if (RandomUtility.randomFloat(1F) < random) {
                this.trainReference.associateData(reference.getData(cursor));
            } else {
                this.testReference.associateData(reference.getData(cursor));
            }
        }
    }
}
 
Example 3
Source File: MockDataFactory.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
/**
 * user(离散:1:稠密)-profile(连续:n:稀疏)
 * 
 * <pre>
 * 可以当作user(离散:1:稠密)-user(离散:1:稠密)-degree(连续:1:稠密)
 * </pre>
 * 
 * >
 */
@Test
public void mockUserProfile() throws Exception {
    File file = new File("data/mock/user-profile");
    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++) {
            buffer.setLength(0);
            for (int rightIndex = 0; rightIndex < profileSize; rightIndex++) {
                if (RandomUtility.randomFloat(1F) < ratio) {
                    float degree = RandomUtility.randomFloat(profileScope);
                    buffer.append(degree);
                }
                buffer.append(" ");
            }
            String profile = buffer.substring(0, buffer.length() - 1);
            out.write(StringUtility.format("{} {}", leftIndex, profile));
            out.newLine();
        }
    }
}
 
Example 4
Source File: MockDataFactory.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
/**
 * item(离散:1:稠密)-profile(连续:n:稀疏)
 * 
 * <pre>
 * 可以当作item(离散:1:稠密)-item(离散:1:稠密)-degree(连续:1:稠密)
 * </pre>
 */
@Test
public void mockItemProfile() throws Exception {
    File file = new File("data/mock/item-profile");
    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 < itemSize; leftIndex++) {
            buffer.setLength(0);
            for (int rightIndex = 0; rightIndex < profileSize; rightIndex++) {
                if (RandomUtility.randomFloat(1F) < ratio) {
                    float degree = RandomUtility.randomFloat(profileScope);
                    buffer.append(degree);
                }
                buffer.append(" ");
            }
            String profile = buffer.substring(0, buffer.length() - 1);
            out.write(StringUtility.format("{} {}", leftIndex, profile));
            out.newLine();
        }
    }
}
 
Example 5
Source File: AbstractEvaluatorTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    RandomUtility.setSeed(0L);
    int rowSize = 1000;
    int columnSize = 1000;
    HashMatrix featureTable = new HashMatrix(true, rowSize, columnSize, new Long2FloatRBTreeMap());
    for (int rowIndex = 0; rowIndex < rowSize; rowIndex++) {
        for (int columnIndex = 0; columnIndex < columnSize; columnIndex++) {
            if (RandomUtility.randomFloat(1F) < 0.5F) {
                featureTable.setValue(rowIndex, columnIndex, RandomUtility.randomFloat(1F));
            }
        }
    }
    SparseMatrix featureMatrix = SparseMatrix.valueOf(rowSize, columnSize, featureTable);
    Evaluator<L, R> evaluator = getEvaluator(featureMatrix);
    Integer2FloatKeyValue sum = evaluate(evaluator, featureMatrix);
    Assert.assertThat(sum.getValue() / sum.getKey(), CoreMatchers.equalTo(getMeasure()));
}
 
Example 6
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 7
Source File: AbstractRankingEvaluatorTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected IntSet getLeft(MathVector vector) {
    IntSet itemSet = new IntOpenHashSet();
    for (VectorScalar scalar : vector) {
        if (RandomUtility.randomFloat(1F) < 0.5F) {
            itemSet.add(scalar.getIndex());
        }
    }
    return itemSet;
}
 
Example 8
Source File: AbstractRankingEvaluatorTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected IntList getRight(MathVector vector) {
    IntList recommendList = new IntArrayList(vector.getElementSize());
    for (VectorScalar scalar : vector) {
        if (RandomUtility.randomFloat(1F) < 0.5F) {
            recommendList.add(scalar.getIndex());
        }
    }
    return recommendList;
}
 
Example 9
Source File: AbstractRatingEvaluatorTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected FloatList getRight(MathVector vector) {
    FloatList recommendList = new FloatArrayList(vector.getElementSize());
    for (VectorScalar scalar : vector) {
        if (RandomUtility.randomFloat(1F) < 0.5F) {
            recommendList.add(scalar.getValue());
        } else {
            recommendList.add(scalar.getValue() * 0.5F);
        }
    }
    return recommendList;
}
 
Example 10
Source File: RBMModel.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);
    // TODO 此处可以重构
    epocheSize = configuration.getInteger("recommender.iterator.maximum", 10);
    sampleSize = configuration.getInteger("recommender.sample.mumber", 100);
    scoreSize = scoreIndexes.size() + 1;
    factorSize = configuration.getInteger("recommender.factor.number", 500);

    epsilonWeight = configuration.getFloat("recommender.epsilonw", 0.001F);
    epsilonExplicitBias = configuration.getFloat("recommender.epsilonvb", 0.001F);
    epsilonImplicitBias = configuration.getFloat("recommender.epsilonhb", 0.001F);
    steps = configuration.getInteger("recommender.tstep", 1);
    momentum = configuration.getFloat("recommender.momentum", 0F);
    lamtaWeight = configuration.getFloat("recommender.lamtaw", 0.001F);
    lamtaBias = configuration.getFloat("recommender.lamtab", 0F);
    predictionType = PredictionType.valueOf(configuration.getString("recommender.predictiontype", "mean").toUpperCase());

    weightProbabilities = new float[itemSize][scoreSize][factorSize];
    explicitBiasProbabilities = new float[itemSize][scoreSize];
    implicitBiasProbabilities = new float[factorSize];

    weightSums = new float[itemSize][scoreSize][factorSize];
    implicitBiasSums = new float[factorSize];
    explicitBiasSums = new float[itemSize][scoreSize];

    positiveWeights = new float[itemSize][scoreSize][factorSize];
    negativeWeights = new float[itemSize][scoreSize][factorSize];

    positiveImplicitActs = new float[factorSize];
    negativeImplicitActs = new float[factorSize];

    positiveExplicitActs = new float[itemSize][scoreSize];
    negativeExplicitActs = new float[itemSize][scoreSize];

    itemCount = new int[itemSize];

    // TODO 此处需要重构
    int[][] itemScoreCount = new int[itemSize][scoreSize];
    for (int userIndex = 0; userIndex < userSize; userIndex++) {
        SparseVector userVector = scoreMatrix.getRowVector(userIndex);
        if (userVector.getElementSize() == 0) {
            continue;
        }
        for (VectorScalar term : userVector) {
            int scoreIndex = scoreIndexes.get(term.getValue());
            itemScoreCount[term.getIndex()][scoreIndex]++;
        }
    }
    QuantityProbability distribution = new QuantityProbability(JDKRandomGenerator.class, 0, NormalDistribution.class, 0D, 0.01D);
    for (int itemIndex = 0; itemIndex < itemSize; itemIndex++) {
        for (int factorIndex = 0; factorIndex < factorSize; factorIndex++) {
            for (int scoreIndex = 0; scoreIndex < scoreSize; scoreIndex++) {
                weightProbabilities[itemIndex][scoreIndex][factorIndex] = distribution.sample().floatValue();
            }
        }
    }

    for (int itemIndex = 0; itemIndex < itemSize; itemIndex++) {
        double totalScore = 0D;
        for (int scoreIndex = 0; scoreIndex < scoreSize; scoreIndex++) {
            totalScore += itemScoreCount[itemIndex][scoreIndex];
        }
        for (int scoreIndex = 0; scoreIndex < scoreSize; scoreIndex++) {
            if (totalScore == 0D) {
                explicitBiasProbabilities[itemIndex][scoreIndex] = RandomUtility.randomFloat(0.001F);
            } else {
                explicitBiasProbabilities[itemIndex][scoreIndex] = (float) Math.log(itemScoreCount[itemIndex][scoreIndex] / totalScore);
                // visbiases[i][k] = Math.log(((moviecount[i][k]) + 1) /
                // (trainMatrix.columnSize(i)+ softmax));
            }
        }
    }
}
 
Example 11
Source File: EuclideanHashFamilyTestCase.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
@Override
protected float getRandomData() {
    return RandomUtility.randomFloat(1F);
}
 
Example 12
Source File: CosineHashFamilyTestCase.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
@Override
protected float getRandomData() {
	return RandomUtility.randomFloat(1F);
}
 
Example 13
Source File: ManhattanHashFamilyTestCase.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
@Override
protected float getRandomData() {
	return RandomUtility.randomFloat(1F);
}