com.jstarcraft.core.utility.KeyValue Java Examples

The following examples show how to use com.jstarcraft.core.utility.KeyValue. 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: MinusVertex.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
public void doCache(KeyValue<MathMatrix, MathMatrix>... samples) {
    super.doCache(samples);

    // 检查样本的数量是否一样
    int rowSize = samples[0].getKey().getRowSize();
    for (int position = 1; position < samples.length; position++) {
        if (rowSize != samples[position].getKey().getRowSize()) {
            throw new IllegalArgumentException();
        }
    }

    // 检查样本的维度是否一样
    int columnSize = samples[0].getKey().getColumnSize();
    for (int position = 1; position < samples.length; position++) {
        if (columnSize != samples[position].getKey().getColumnSize()) {
            throw new IllegalArgumentException();
        }
    }

    // TODO 考虑支持CompositeMatrix.
    MathMatrix outputData = factory.makeMatrix(rowSize, columnSize);
    outputKeyValue.setKey(outputData);
    MathMatrix innerError = factory.makeMatrix(rowSize, columnSize);
    outputKeyValue.setValue(innerError);
}
 
Example #2
Source File: AbstractLayer.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
public void doCache(MathCache factory, KeyValue<MathMatrix, MathMatrix> samples) {
    inputKeyValue = samples;
    int rowSize = inputKeyValue.getKey().getRowSize();
    int columnSize = inputKeyValue.getKey().getColumnSize();

    // 检查维度
    if (columnSize != numberOfInputs) {
        throw new IllegalArgumentException();
    }

    middleKeyValue = new KeyValue<>(null, null);
    outputKeyValue = new KeyValue<>(null, null);

    MathMatrix middleData = factory.makeMatrix(rowSize, numberOfOutputs);
    middleKeyValue.setKey(middleData);
    MathMatrix middleError = factory.makeMatrix(rowSize, numberOfOutputs);
    middleKeyValue.setValue(middleError);

    MathMatrix outputData = factory.makeMatrix(rowSize, numberOfOutputs);
    outputKeyValue.setKey(outputData);
    MathMatrix innerError = factory.makeMatrix(rowSize, numberOfOutputs);
    outputKeyValue.setValue(innerError);
}
 
Example #3
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, I, T extends IdentityObject<K>> List<T> queryInstances(Class<T> clazz, String name, StorageCondition<I> condition) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query;
	{
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(name);
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, metadata.getPrimaryName(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), condition.getType(), condition.getValues());
	}
	KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, 0, Integer.MAX_VALUE);
	List<Document> documents = retrieve.getKey();
	List<T> list = new ArrayList<>(BATCH_SIZE);
	for (Document document : documents) {
		list.add((T) metadata.decodeDocument(document));
	}
	return list;
}
 
Example #4
Source File: FMLayer.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public void doCache(MathCache factory, KeyValue<MathMatrix, MathMatrix> samples) {
    inputKeyValue = samples;
    int rowSize = inputKeyValue.getKey().getRowSize();
    int columnSize = inputKeyValue.getKey().getColumnSize();

    // 检查维度
    if (this.dimensionSizes.length != columnSize) {
        throw new IllegalArgumentException();
    }

    middleKeyValue = new KeyValue<>(null, null);
    outputKeyValue = new KeyValue<>(null, null);

    MathMatrix middleData = factory.makeMatrix(rowSize, numberOfOutputs);
    middleKeyValue.setKey(middleData);
    MathMatrix middleError = factory.makeMatrix(rowSize, numberOfOutputs);
    middleKeyValue.setValue(middleError);

    MathMatrix outputData = factory.makeMatrix(rowSize, numberOfOutputs);
    outputKeyValue.setKey(outputData);
    MathMatrix innerError = factory.makeMatrix(rowSize, numberOfOutputs);
    outputKeyValue.setValue(innerError);
}
 
Example #5
Source File: PropertyFormatAdapterTestCase.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
/**
 * 测试仓储访问器
 */
@Test
public void testAssemblage() {
    // 保证@StorageAccessor注解的接口与类型能被自动装配
    Assert.assertThat(manager, CoreMatchers.notNullValue());
    Assert.assertThat(person, CoreMatchers.notNullValue());

    // 检查仓储访问
    Assert.assertThat(manager.getAll().size(), CoreMatchers.equalTo(3));
    Assert.assertThat(manager.getInstance(2, false), CoreMatchers.sameInstance(person));

    // 检查实例访问
    Assert.assertThat(person.isSex(), CoreMatchers.equalTo(sex));
    KeyValue<?, ?> keyValue = new KeyValue<>("key", "value");
    Assert.assertThat(person.getObject(), CoreMatchers.equalTo(keyValue));
    keyValue = new KeyValue<>(1, "1");
    Assert.assertThat(person.getArray()[1], CoreMatchers.equalTo(keyValue));
    Assert.assertThat(person.getMap().get("1"), CoreMatchers.equalTo(keyValue));
    Assert.assertThat(person.getList().get(1), CoreMatchers.equalTo(keyValue));

    // 检查属性访问
    Assert.assertTrue(sex);
    Assert.assertThat(description, CoreMatchers.notNullValue());
}
 
Example #6
Source File: JiebaSegmentFactory.java    From jstarcraft-nlp with Apache License 2.0 6 votes vote down vote up
@Override
public KeyValue<JiebaSegmenter, SegMode> build(Map<String, String> configurations) {
    String configuration = get(configurations, "mode", "search");
    SegMode mode;
    switch (configuration) {
    case "index":
        mode = SegMode.INDEX;
        break;
    case "search":
        mode = SegMode.SEARCH;
        break;
    default:
        throw new IllegalArgumentException();
    }

    JiebaSegmenter segmenter = new JiebaSegmenter();

    return new KeyValue<>(segmenter, mode);
}
 
Example #7
Source File: HorizontalAttachVertex.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
public void doCache(KeyValue<MathMatrix, MathMatrix>... samples) {
    super.doCache(samples);

    // 检查样本的数量是否一样
    int rowSize = samples[0].getKey().getRowSize();
    for (int position = 1; position < samples.length; position++) {
        if (rowSize != samples[position].getKey().getRowSize()) {
            throw new IllegalArgumentException();
        }
    }

    // 获取样本的维度
    MathMatrix[] keys = new MathMatrix[inputKeyValues.length];
    MathMatrix[] values = new MathMatrix[inputKeyValues.length];
    for (int position = 0; position < inputKeyValues.length; position++) {
        keys[position] = inputKeyValues[position].getKey();
        values[position] = inputKeyValues[position].getValue();
    }

    MathMatrix outputData = ColumnGlobalMatrix.attachOf(keys);
    outputKeyValue.setKey(outputData);
    MathMatrix innerError = ColumnGlobalMatrix.attachOf(values);
    outputKeyValue.setValue(innerError);
}
 
Example #8
Source File: ShareLayer.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
public void doCache(MathCache factory, KeyValue<MathMatrix, MathMatrix> samples) {
    inputKeyValue = samples;
    int rowSize = inputKeyValue.getKey().getRowSize();
    int columnSize = inputKeyValue.getKey().getColumnSize();

    // 检查维度
    if (columnSize != numberOfInputs * numberOfShares) {
        throw new IllegalArgumentException();
    }

    columnSize = numberOfOutputs * numberOfShares;
    middleKeyValue = new KeyValue<>(null, null);
    outputKeyValue = new KeyValue<>(null, null);

    // TODO 此处需要改为CompositeMatrix
    MathMatrix middleData = ColumnGlobalMatrix.attachOf(getMatrixes(factory, rowSize, numberOfOutputs, numberOfShares));
    middleKeyValue.setKey(middleData);
    MathMatrix middleError = ColumnGlobalMatrix.attachOf(getMatrixes(factory, rowSize, numberOfOutputs, numberOfShares));
    middleKeyValue.setValue(middleError);

    MathMatrix outputData = ColumnGlobalMatrix.attachOf(getMatrixes(factory, rowSize, numberOfOutputs, numberOfShares));
    outputKeyValue.setKey(outputData);
    MathMatrix innerError = ColumnGlobalMatrix.attachOf(getMatrixes(factory, rowSize, numberOfOutputs, numberOfShares));
    outputKeyValue.setValue(innerError);
}
 
Example #9
Source File: IRRGModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
/**
 * Compute group-to-item AR and store them into map itemCorrsGAR
 */
private void computeAssociationRuleByGroup(int groupIndex, LinkedList<KeyValue<Integer, Integer>> itemList) {
    List<KeyValue<KeyValue<Integer, Integer>, Float>> coefficientList = new LinkedList<>();

    for (KeyValue<Integer, Integer> keyValue : itemList) {
        int leftIndex = keyValue.getKey();
        int rightIndex = keyValue.getValue();
        SparseVector groupVector = scoreMatrix.getColumnVector(groupIndex);
        int count = 0;
        for (VectorScalar term : groupVector) {
            int userIndex = term.getIndex();
            if (dataTable.contains(userIndex, leftIndex) && dataTable.contains(userIndex, rightIndex)) {
                count++;
            }
        }
        if (count > 0) {
            float shrink = count / (count + reliability);
            int co_bc = itemCount.get(leftIndex, rightIndex);
            float coefficient = shrink * (count + 0F) / co_bc;
            coefficientList.add(new KeyValue<>(keyValue, coefficient));
        }
    }
    itemCorrsGAR.put(groupIndex, new ArrayList<>(coefficientList));
}
 
Example #10
Source File: SumVertex.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
public void doCache(KeyValue<MathMatrix, MathMatrix>... samples) {
    super.doCache(samples);

    // 检查样本的数量是否一样
    int rowSize = samples[0].getKey().getRowSize();
    for (int position = 1; position < samples.length; position++) {
        if (rowSize != samples[position].getKey().getRowSize()) {
            throw new IllegalArgumentException();
        }
    }

    MathMatrix outputData = factory.makeMatrix(rowSize, 1);
    outputKeyValue.setKey(outputData);
    MathMatrix innerError = factory.makeMatrix(rowSize, 1);
    outputKeyValue.setValue(innerError);
}
 
Example #11
Source File: IRRGModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
/**
 * Order group-to-item AR and store them into map itemCorrsGAR_Sorted
 */
private void sortAssociationRuleByGroup() {
    for (int groupIndex : itemCorrsGAR.keySet()) {
        List<KeyValue<KeyValue<Integer, Integer>, Float>> list = itemCorrsGAR.get(groupIndex);
        if (list.size() > neighborSize) {
            Collections.sort(list, (left, right) -> {
                return right.getValue().compareTo(left.getValue());
            });
            list = list.subList(0, neighborSize);
        }

        HashMatrix groupTable = new HashMatrix(true, itemSize, itemSize, new Long2FloatRBTreeMap());
        for (KeyValue<KeyValue<Integer, Integer>, Float> keyValue : list) {
            int leftItemIndex = keyValue.getKey().getKey();
            int rightItemIndex = keyValue.getKey().getValue();
            float correlation = keyValue.getValue();
            groupTable.setValue(leftItemIndex, rightItemIndex, correlation);
        }
        itemCorrsGAR_Sorted.put(groupIndex, SparseMatrix.valueOf(itemSize, itemSize, groupTable));
    }
}
 
Example #12
Source File: SparseModule.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
public SparseModule(String name, List<KeyValue<KeyValue<String, Boolean>, Integer>> definition, int capacity) {
    super(name, definition, capacity);
    this.qualityPoints = new IntegerArray(1000, capacity + 1);
    // 防止溢出
    if (qualityOrder > 1000) {
        this.qualityIndexes = new IntegerArray(1000, Integer.MAX_VALUE);
        this.qualityValues = new IntegerArray(1000, Integer.MAX_VALUE);
    } else {
        this.qualityIndexes = new IntegerArray(1000, capacity * qualityOrder);
        this.qualityValues = new IntegerArray(1000, capacity * qualityOrder);
    }
    this.quantityPoints = new IntegerArray(1000, capacity + 1);
    // 防止溢出
    if (quantityOrder > 1000) {
        this.quantityIndexes = new IntegerArray(1000, Integer.MAX_VALUE);
        this.quantityValues = new FloatArray(1000, Integer.MAX_VALUE);
    } else {
        this.quantityIndexes = new IntegerArray(1000, capacity * quantityOrder);
        this.quantityValues = new FloatArray(1000, capacity * quantityOrder);
    }
    this.qualityPoints.associateData(0);
    this.quantityPoints.associateData(0);
}
 
Example #13
Source File: MultiplyVertex.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
public void doBackward() {
    MathMatrix innerError = outputKeyValue.getValue();
    int size = inputKeyValues.length;
    innerError.iterateElement(MathCalculator.PARALLEL, (scalar) -> {
        int row = scalar.getRow();
        int column = scalar.getColumn();
        float value = scalar.getValue();
        for (int thisIndex = 0; thisIndex < size; thisIndex++) {
            KeyValue<MathMatrix, MathMatrix> keyValue = inputKeyValues[thisIndex];
            MathMatrix outerError = keyValue.getValue();
            if (outerError != null) {
                float error = value;
                for (int thatIndex = 0; thatIndex < size; thatIndex++) {
                    if (thisIndex != thatIndex) {
                        error *= inputKeyValues[thatIndex].getKey().getValue(row, column);
                    }
                }
                // TODO 使用累计的方式计算
                // TODO 需要锁机制,否则并发计算会导致Bug
                outerError.shiftValue(row, column, error);
            }
        }
        return;
    });
}
 
Example #14
Source File: MovieService.java    From jstarcraft-example with Apache License 2.0 6 votes vote down vote up
/**
 * 个性化搜索
 * 
 * @param userIndex
 * @param searchKey
 * @return
 * @throws Exception
 */
@LockableMethod(strategy = HashLockableStrategy.class)
public Object2FloatMap<MovieItem> getSearchItems(@LockableParameter int userIndex, String searchKey) throws Exception {
    // 标识-得分映射
    Object2FloatMap<MovieItem> item2ScoreMap = new Object2FloatOpenHashMap<>();

    long current = System.currentTimeMillis();
    Query query = queryParser.parse(searchKey, MovieItem.TITLE);
    KeyValue<List<Document>, FloatList> search = engine.retrieveDocuments(query, null, 0, 1000);
    List<Document> documents = search.getKey();
    FloatList scores = search.getValue();
    for (int index = 0, size = documents.size(); index < size; index++) {
        Document document = documents.get(index);
        MovieItem item = items.get(document.getField(MovieItem.INDEX).numericValue().intValue());
        float score = scores.getFloat(index);
        item2ScoreMap.put(item, score);
    }
    String message = StringUtility.format("搜索数量:{},搜索耗时:{}", documents.size(), System.currentTimeMillis() - current);
    logger.info(message);

    return item2ScoreMap;
}
 
Example #15
Source File: Graph.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
/**
 * 预测
 * 
 * @param inputs
 * @param outputs
 */
public void predict(MathMatrix[] samples, MathMatrix[] marks) {
    doCache(samples, marks);
    for (int index = 0, size = marks.length; index < size; index++) {
        // 检查数量
        if (marks[index].getRowSize() != numberOfSamples) {
            throw new IllegalArgumentException();
        }
    }

    doForward();
    for (int index = 0, size = outputVertices.length; index < size; index++) {
        Vertex vertex = outputVertices[index];
        KeyValue<MathMatrix, MathMatrix> keyValue = vertex.getOutputKeyValue();
        MathMatrix outputData = keyValue.getKey();
        marks[index].iterateElement(MathCalculator.PARALLEL, (scalar) -> {
            scalar.setValue(outputData.getValue(scalar.getRow(), scalar.getColumn()));
        });
    }
}
 
Example #16
Source File: DataSelectorTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
private DataModule getDataModule() {
    List<KeyValue<KeyValue<String, Boolean>, Integer>> moduleDefinition = new LinkedList<>();
    for (int index = 0; index < order; index++) {
        moduleDefinition.add(new KeyValue<>(new KeyValue<>("quality", true), 1));
        moduleDefinition.add(new KeyValue<>(new KeyValue<>("ontinuous", false), 1));
    }
    DataModule module = new SparseModule(moduleName, moduleDefinition, instanceCapacity);
    Int2IntSortedMap qualityFeatures = new Int2IntAVLTreeMap();
    Int2FloatSortedMap quantityFeatures = new Int2FloatAVLTreeMap();
    for (int index = 0; index < instanceCapacity; index++) {
        qualityFeatures.clear();
        qualityFeatures.put(0, index);
        module.associateInstance(qualityFeatures, quantityFeatures);
    }
    return module;
}
 
Example #17
Source File: DataSplitterTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
private DataModule getDataModule() {
    List<KeyValue<KeyValue<String, Boolean>, Integer>> moduleDefinition = new LinkedList<>();
    for (int index = 0; index < order; index++) {
        moduleDefinition.add(new KeyValue<>(new KeyValue<>("quality", true), 1));
        moduleDefinition.add(new KeyValue<>(new KeyValue<>("ontinuous", false), 1));
    }
    DataModule module = new SparseModule(moduleName, moduleDefinition, instanceCapacity);
    Int2IntSortedMap qualityFeatures = new Int2IntAVLTreeMap();
    Int2FloatSortedMap quantityFeatures = new Int2FloatAVLTreeMap();
    for (int index = 0; index < instanceCapacity; index++) {
        qualityFeatures.clear();
        qualityFeatures.put(0, index);
        module.associateInstance(qualityFeatures, quantityFeatures);
    }
    return module;
}
 
Example #18
Source File: DataSorterTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
private DataModule getDataModule() {
    List<KeyValue<KeyValue<String, Boolean>, Integer>> moduleDefinition = new LinkedList<>();
    for (int index = 0; index < order; index++) {
        moduleDefinition.add(new KeyValue<>(new KeyValue<>("quality", true), 1));
        moduleDefinition.add(new KeyValue<>(new KeyValue<>("ontinuous", false), 1));
    }
    DataModule module = new SparseModule(moduleName, moduleDefinition, instanceCapacity);
    Int2IntSortedMap qualityFeatures = new Int2IntAVLTreeMap();
    Int2FloatSortedMap quantityFeatures = new Int2FloatAVLTreeMap();
    for (int index = 0; index < instanceCapacity; index++) {
        qualityFeatures.clear();
        qualityFeatures.put(0, index);
        module.associateInstance(qualityFeatures, quantityFeatures);
    }
    return module;
}
 
Example #19
Source File: LimitVertex.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
public void doBackward() {
    MathMatrix innerError = outputKeyValue.getValue();
    innerError.iterateElement(MathCalculator.PARALLEL, (scalar) -> {
        int row = scalar.getRow();
        int column = scalar.getColumn();
        float value = scalar.getValue();
        int index = (int) limitIndexes.getValue(row, column);
        KeyValue<MathMatrix, MathMatrix> keyValue = inputKeyValues[index];
        MathMatrix outerError = keyValue.getValue();
        if (outerError != null) {
            // TODO 使用累计的方式计算
            // TODO 需要锁机制,否则并发计算会导致Bug
            outerError.shiftValue(row, column, value);
        }
    });
}
 
Example #20
Source File: ScaleVertex.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
public void doCache(KeyValue<MathMatrix, MathMatrix>... samples) {
    super.doCache(samples);

    // 检查样本的数量是否一样
    int rowSize = samples[0].getKey().getRowSize();
    for (int position = 1; position < samples.length; position++) {
        if (rowSize != samples[position].getKey().getRowSize()) {
            throw new IllegalArgumentException();
        }
    }

    // 检查样本的维度是否一样
    int columnSize = samples[0].getKey().getColumnSize();
    for (int position = 1; position < samples.length; position++) {
        if (columnSize != samples[position].getKey().getColumnSize()) {
            throw new IllegalArgumentException();
        }
    }

    // TODO 考虑支持CompositeMatrix.
    MathMatrix outputData = factory.makeMatrix(rowSize, columnSize);
    outputKeyValue.setKey(outputData);
    MathMatrix innerError = factory.makeMatrix(rowSize, columnSize);
    outputKeyValue.setValue(innerError);
}
 
Example #21
Source File: ShiftVertex.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
public void doCache(KeyValue<MathMatrix, MathMatrix>... samples) {
    super.doCache(samples);

    // 检查样本的数量是否一样
    int rowSize = samples[0].getKey().getRowSize();
    for (int position = 1; position < samples.length; position++) {
        if (rowSize != samples[position].getKey().getRowSize()) {
            throw new IllegalArgumentException();
        }
    }

    // 检查样本的维度是否一样
    int columnSize = samples[0].getKey().getColumnSize();
    for (int position = 1; position < samples.length; position++) {
        if (columnSize != samples[position].getKey().getColumnSize()) {
            throw new IllegalArgumentException();
        }
    }

    // TODO 考虑支持CompositeMatrix.
    MathMatrix outputData = factory.makeMatrix(rowSize, columnSize);
    outputKeyValue.setKey(outputData);
    MathMatrix innerError = factory.makeMatrix(rowSize, columnSize);
    outputKeyValue.setValue(innerError);
}
 
Example #22
Source File: ReferenceModuleTestCase.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
private DataModule getDataModule() {
    List<KeyValue<KeyValue<String, Boolean>, Integer>> moduleDefinition = new LinkedList<>();
    for (int index = 0; index < order; index++) {
        moduleDefinition.add(new KeyValue<>(new KeyValue<>("quality", true), 1));
        moduleDefinition.add(new KeyValue<>(new KeyValue<>("ontinuous", false), 1));
    }
    DataModule module = new SparseModule(moduleName, moduleDefinition, instanceCapacity);
    Int2IntSortedMap qualityFeatures = new Int2IntAVLTreeMap();
    Int2FloatSortedMap quantityFeatures = new Int2FloatAVLTreeMap();
    for (int index = 0; index < instanceCapacity; index++) {
        qualityFeatures.clear();
        qualityFeatures.put(index, index);
        module.associateInstance(qualityFeatures, quantityFeatures);
    }
    return module;
}
 
Example #23
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> List<T> queryIntersection(Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = null;
	BooleanQuery.Builder buffer = new BooleanQuery.Builder();
	for (Entry<String, Object> term : condition.entrySet()) {
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey());
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue());
		buffer.add(query, Occur.MUST);
	}
	query = buffer.build();
	int offset = pagination == null ? 0 : pagination.getFirst();
	int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize();
	KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, offset, size);
	List<Document> documents = retrieve.getKey();
	List<T> list = new ArrayList<>(BATCH_SIZE);
	for (Document document : documents) {
		list.add((T) metadata.decodeDocument(document));
	}
	return list;
}
 
Example #24
Source File: LimitVertex.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public void doCache(KeyValue<MathMatrix, MathMatrix>... samples) {
    super.doCache(samples);

    // 检查样本的数量是否一样
    int rowSize = samples[0].getKey().getRowSize();
    for (int position = 1; position < samples.length; position++) {
        if (rowSize != samples[position].getKey().getRowSize()) {
            throw new IllegalArgumentException();
        }
    }

    // 检查样本的维度是否一样
    int columnSize = samples[0].getKey().getColumnSize();
    for (int position = 1; position < samples.length; position++) {
        if (columnSize != samples[position].getKey().getColumnSize()) {
            throw new IllegalArgumentException();
        }
    }

    // TODO 考虑支持CompositeMatrix.
    MathMatrix outputData = factory.makeMatrix(rowSize, columnSize);
    outputKeyValue.setKey(outputData);
    MathMatrix innerError = factory.makeMatrix(rowSize, columnSize);
    outputKeyValue.setValue(innerError);

    limitIndexes = factory.makeMatrix(rowSize, 1);
}
 
Example #25
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> long countIntersection(Class<T> clazz, Map<String, Object> condition) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = null;
	BooleanQuery.Builder buffer = new BooleanQuery.Builder();
	for (Entry<String, Object> term : condition.entrySet()) {
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey());
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue());
		buffer.add(query, Occur.MUST);
	}
	query = buffer.build();
	return engine.countDocuments(query);
}
 
Example #26
Source File: SecurityUtility.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
/**
 * 获取RSA非对称秘钥
 * 
 * @param size
 * @return
 */
public static KeyValue<byte[], byte[]> getRsa(int size) {
    try {
        KeyPairGenerator rsa = KeyPairGenerator.getInstance(RSA);
        SecureRandom random = new SecureRandom();
        rsa.initialize(size, random);
        KeyPair keys = rsa.generateKeyPair();
        return new KeyValue<>(keys.getPrivate().getEncoded(), keys.getPublic().getEncoded());
    } catch (Exception exception) {
        String message = StringUtility.format("获取RAS密匙异常:[{}]");
        throw new RuntimeException(message, exception);
    }
}
 
Example #27
Source File: RandomLayer.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public void doCache(MathCache factory, KeyValue<MathMatrix, MathMatrix> samples) {
    this.factory = factory;
    inputKeyValue = samples;
    int rowSize = inputKeyValue.getKey().getRowSize();
    int columnSize = inputKeyValue.getKey().getColumnSize();

    // 检查维度
    if (columnSize != 1 + numberOfInputs * numberOfRandoms) {
        throw new IllegalArgumentException();
    }

    columnSize = numberOfOutputs * numberOfRandoms;
    middleKeyValue = new KeyValue<>(null, null);
    outputKeyValue = new KeyValue<>(null, null);

    int size = numberOfRandoms;
    MathMatrix[] middleDatas = new MathMatrix[size];
    MathMatrix[] middleErrors = new MathMatrix[size];
    MathMatrix[] outputDatas = new MathMatrix[size + 1];
    MathMatrix[] innerErrors = new MathMatrix[size + 1];
    outputDatas[0] = factory.makeMatrix(rowSize, 1);
    innerErrors[0] = factory.makeMatrix(rowSize, 1);
    for (int index = 0; index < size; index++) {
        middleDatas[index] = factory.makeMatrix(rowSize, numberOfOutputs);
        middleErrors[index] = factory.makeMatrix(rowSize, numberOfOutputs);
        outputDatas[index + 1] = factory.makeMatrix(rowSize, numberOfOutputs);
        innerErrors[index + 1] = factory.makeMatrix(rowSize, numberOfOutputs);
    }

    MathMatrix middleData = ColumnGlobalMatrix.attachOf(middleDatas);
    middleKeyValue.setKey(middleData);
    MathMatrix middleError = ColumnGlobalMatrix.attachOf(middleErrors);
    middleKeyValue.setValue(middleError);

    MathMatrix outputData = ColumnGlobalMatrix.attachOf(outputDatas);
    outputKeyValue.setKey(outputData);
    MathMatrix innerError = ColumnGlobalMatrix.attachOf(innerErrors);
    outputKeyValue.setValue(innerError);
}
 
Example #28
Source File: MovieService.java    From jstarcraft-example with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param userIndex
 * @param modelKey
 * @param queryKey
 * @param filterClicked
 * @return
 * @throws Exception
 */
@LockableMethod(strategy = HashLockableStrategy.class)
public Object2FloatMap<MovieItem> getItems(@LockableParameter int userIndex, String modelKey, String queryKey, boolean filterClicked) throws Exception {
    // 标识-得分映射
    Object2FloatMap<MovieItem> item2ScoreMap = new Object2FloatOpenHashMap<>();

    long current = System.currentTimeMillis();
    Model model = models.get(modelKey);
    ArrayInstance instance = new ArrayInstance(qualityOrder, quantityOrder);
    MovieUser user = users.get(userIndex);
    Query query = StringUtility.isBlank(queryKey) ? new MatchAllDocsQuery() : queryParser.parse(queryKey, MovieItem.TITLE);
    KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, 0, 1000);
    List<Document> documents = retrieve.getKey();
    for (int index = 0, size = documents.size(); index < size; index++) {
        Document document = documents.get(index);
        MovieItem item = items.get(document.getField(MovieItem.INDEX).numericValue().intValue());
        int itemIndex = item.getIndex();
        // 过滤条目
        if (filterClicked && user.isClicked(itemIndex)) {
            continue;
        }
        instance.setQualityFeature(userDimension, userIndex);
        instance.setQualityFeature(itemDimension, itemIndex);
        model.predict(instance);
        float score = instance.getQuantityMark();
        item2ScoreMap.put(item, score);
    }
    String message = StringUtility.format("预测数量:{},预测耗时:{}", modelKey, documents.size(), System.currentTimeMillis() - current);
    logger.info(message);

    return item2ScoreMap;
}
 
Example #29
Source File: OuterProductVertex.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public void doCache(KeyValue<MathMatrix, MathMatrix>... samples) {
    if (samples.length != 2) {
        throw new IllegalArgumentException();
    }

    super.doCache(samples);

    // 检查样本的数量是否一样
    rowSize = samples[0].getKey().getRowSize();
    for (int position = 1; position < samples.length; position++) {
        if (rowSize != samples[position].getKey().getRowSize()) {
            throw new IllegalArgumentException();
        }
    }

    // 检查样本的维度是否一样
    columnSize = samples[0].getKey().getColumnSize();
    for (int position = 1; position < samples.length; position++) {
        if (columnSize != samples[position].getKey().getColumnSize()) {
            throw new IllegalArgumentException();
        }
    }

    MathMatrix outputData = factory.makeMatrix(rowSize, columnSize * columnSize);
    outputKeyValue.setKey(outputData);
    MathMatrix innerError = factory.makeMatrix(rowSize, columnSize * columnSize);
    outputKeyValue.setValue(innerError);
}
 
Example #30
Source File: AbstractVertex.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public void doCache(KeyValue<MathMatrix, MathMatrix>... samples) {
    // 检查样本
    if (samples.length == 0) {
        throw new IllegalArgumentException();
    }

    this.inputKeyValues = samples;
    this.outputKeyValue = new KeyValue<>(null, null);
}