Java Code Examples for com.jstarcraft.ai.data.DataModule#getInstance()
The following examples show how to use
com.jstarcraft.ai.data.DataModule#getInstance() .
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: DataSplitter.java From jstarcraft-ai with Apache License 2.0 | 6 votes |
/** * 划分 * * @param module * @param number * @return */ default ReferenceModule[] split(DataModule module, int number) { int size = module.getSize(); int maximum = size; int minimum = 1000; if (maximum < minimum) { maximum = minimum; } IntegerArray[] references = new IntegerArray[number]; for (int index = 0; index < number; index++) { references[index] = new IntegerArray(minimum, maximum); } int cursor = 0; DataInstance instance = module.getInstance(cursor); while (cursor < size) { instance.setCursor(cursor); references[split(instance) % number].associateData(cursor); cursor++; } ReferenceModule[] modules = new ReferenceModule[number]; for (int index = 0; index < number; index++) { modules[index] = new ReferenceModule(references[index], module); } return modules; }
Example 2
Source File: DataSelector.java From jstarcraft-ai with Apache License 2.0 | 6 votes |
/** * 选择 * * @param module * @return */ default ReferenceModule select(DataModule module) { int size = module.getSize(); if (size == 0) { return new ReferenceModule(module); } int maximum = size; int minimum = 1000; if (maximum < minimum) { maximum = minimum; } IntegerArray reference = new IntegerArray(minimum, maximum); int cursor = 0; DataInstance instance = module.getInstance(cursor); while (cursor < size) { instance.setCursor(cursor); if (select(instance)) { reference.associateData(cursor); } cursor++; } return new ReferenceModule(reference, module); }
Example 3
Source File: DataSorter.java From jstarcraft-ai with Apache License 2.0 | 5 votes |
/** * 排序 * * @param module * @return */ default ReferenceModule sort(DataModule module) { int size = module.getSize(); if (size <= 1) { return new ReferenceModule(module); } IntegerArray reference = new IntegerArray(size, size); for (int index = 0; index < size; index++) { reference.associateData(index); } int cursor = 0; DataInstance leftInstance = module.getInstance(cursor); DataInstance rightInstance = module.getInstance(cursor); for (int left = 0; left < size - 1; left++) { leftInstance.setCursor(left); for (int right = left + 1; right < size; right++) { // TODO 注意:此处存在0的情况. rightInstance.setCursor(right); if (sort(leftInstance, rightInstance) > 0) { reference.setData(left, reference.getData(left) ^ reference.getData(right)); reference.setData(right, reference.getData(right) ^ reference.getData(left)); reference.setData(left, reference.getData(left) ^ reference.getData(right)); } } } return new ReferenceModule(reference, module); }
Example 4
Source File: LambdaFMStaticModel.java From jstarcraft-rns with Apache License 2.0 | 4 votes |
@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 5
Source File: LambdaFMWeightModel.java From jstarcraft-rns with Apache License 2.0 | 4 votes |
@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 6
Source File: ReferenceInstance.java From jstarcraft-ai with Apache License 2.0 | 4 votes |
public ReferenceInstance(int cursor, IntegerArray references, DataModule module) { this.cursor = cursor; this.references = references; this.instance = module.getInstance(references.getData(cursor)); }