Search in sources :

Example 41 with IntStream

use of java.util.stream.IntStream in project pyramid by cheng-li.

the class RegTreeTrainer method splitNode.

/**
 * split a splitable node
 * @param leafToSplit
 * @param regTreeConfig
 * @param dataSet
 */
private static void splitNode(RegressionTree tree, Node leafToSplit, RegTreeConfig regTreeConfig, DataSet dataSet, double[] labels) {
    int numDataPoints = dataSet.getNumDataPoints();
    /**
     * split this leaf node
     */
    int featureIndex = leafToSplit.getFeatureIndex();
    double threshold = leafToSplit.getThreshold();
    Vector inputVector = dataSet.getColumn(featureIndex);
    Vector columnVector;
    if (inputVector.isDense()) {
        columnVector = inputVector;
    } else {
        columnVector = new DenseVector(inputVector);
    }
    /**
     * create children
     */
    Node leftChild = new Node();
    leftChild.setId(tree.numNodes);
    tree.numNodes += 1;
    Node rightChild = new Node();
    rightChild.setId(tree.numNodes);
    tree.numNodes += 1;
    double[] parentProbs = leafToSplit.getProbs();
    double[] leftProbs = new double[numDataPoints];
    double[] rightProbs = new double[numDataPoints];
    IntStream intStream = IntStream.range(0, numDataPoints);
    if (regTreeConfig.isParallel()) {
        intStream = intStream.parallel();
    }
    intStream.forEach(i -> {
        double featureValue = columnVector.get(i);
        if (Double.isNaN(featureValue)) {
            // go to both branches probabilistically
            leftProbs[i] = parentProbs[i] * leafToSplit.getLeftProb();
            rightProbs[i] = parentProbs[i] * leafToSplit.getRightProb();
        } else {
            // <= go left, > go right
            if (featureValue <= threshold) {
                leftProbs[i] = parentProbs[i];
                rightProbs[i] = 0;
            } else {
                leftProbs[i] = 0;
                rightProbs[i] = parentProbs[i];
            }
        }
    });
    leftChild.setProbs(leftProbs);
    rightChild.setProbs(rightProbs);
    // the last two leaves need not to be updated completely
    // as we don't need to split them later
    int maxNumLeaves = regTreeConfig.getMaxNumLeaves();
    if (tree.leaves.size() != maxNumLeaves - 1) {
        updateNode(leftChild, regTreeConfig, dataSet, labels);
        updateNode(rightChild, regTreeConfig, dataSet, labels);
    }
    /**
     * link left and right child to the parent
     */
    leafToSplit.setLeftChild(leftChild);
    leafToSplit.setRightChild(rightChild);
    /**
     * update leaves, remove the parent, and add children
     */
    leafToSplit.setLeaf(false);
    leafToSplit.clearProbs();
    tree.leaves.remove(leafToSplit);
    leftChild.setLeaf(true);
    rightChild.setLeaf(true);
    tree.leaves.add(leftChild);
    tree.leaves.add(rightChild);
    tree.allNodes.add(leftChild);
    tree.allNodes.add(rightChild);
}
Also used : DenseVector(org.apache.mahout.math.DenseVector) Vector(org.apache.mahout.math.Vector) IntStream(java.util.stream.IntStream) DenseVector(org.apache.mahout.math.DenseVector)

Example 42 with IntStream

use of java.util.stream.IntStream in project pyramid by cheng-li.

the class BlockwiseCD method getValueForAllData.

// check
private double getValueForAllData() {
    updateClassScoreMatrix();
    updateAssignmentScoreMatrix();
    IntStream intStream;
    if (isParallel) {
        intStream = IntStream.range(0, dataSet.getNumDataPoints()).parallel();
    } else {
        intStream = IntStream.range(0, dataSet.getNumDataPoints());
    }
    return intStream.mapToDouble(this::getValueForOneData).sum();
}
Also used : IntStream(java.util.stream.IntStream)

Example 43 with IntStream

use of java.util.stream.IntStream in project pyramid by cheng-li.

the class CMLCRFElasticNet method updateEmpiricalCounts.

// check
private void updateEmpiricalCounts() {
    IntStream intStream;
    if (isParallel) {
        intStream = IntStream.range(0, numParameters).parallel();
    } else {
        intStream = IntStream.range(0, numParameters);
    }
    intStream.forEach(this::calEmpiricalCount);
}
Also used : IntStream(java.util.stream.IntStream)

Example 44 with IntStream

use of java.util.stream.IntStream in project pyramid by cheng-li.

the class CRFLoss method getValueForAllData.

private double getValueForAllData() {
    updateClassScoreMatrix();
    updateAssignmentScoreMatrix();
    IntStream intStream;
    if (isParallel) {
        intStream = IntStream.range(0, dataSet.getNumDataPoints()).parallel();
    } else {
        intStream = IntStream.range(0, dataSet.getNumDataPoints());
    }
    return intStream.mapToDouble(this::getValueForOneData).sum();
}
Also used : IntStream(java.util.stream.IntStream)

Example 45 with IntStream

use of java.util.stream.IntStream in project pyramid by cheng-li.

the class KLLoss method initEmpiricalCounts.

private void initEmpiricalCounts() {
    IntStream intStream;
    if (isParallel) {
        intStream = IntStream.range(0, numParameters).parallel();
    } else {
        intStream = IntStream.range(0, numParameters);
    }
    intStream.forEach(this::calEmpiricalCount);
}
Also used : IntStream(java.util.stream.IntStream)

Aggregations

IntStream (java.util.stream.IntStream)80 Test (org.junit.Test)20 List (java.util.List)15 ArrayList (java.util.ArrayList)13 Arrays (java.util.Arrays)10 Stream (java.util.stream.Stream)9 Random (java.util.Random)7 Collectors (java.util.stream.Collectors)7 Map (java.util.Map)5 Pattern (java.util.regex.Pattern)5 DoubleStream (java.util.stream.DoubleStream)5 LongStream (java.util.stream.LongStream)5 DecimalBoxFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.decimalBox.definition.DecimalBoxFieldDefinition)5 MultipleSubFormFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.multipleSubform.definition.MultipleSubFormFieldDefinition)5 SubFormFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.subForm.definition.SubFormFieldDefinition)5 FieldDefinition (org.kie.workbench.common.forms.model.FieldDefinition)5 LayoutRow (org.uberfire.ext.layout.editor.api.editor.LayoutRow)5 LayoutTemplate (org.uberfire.ext.layout.editor.api.editor.LayoutTemplate)5 Assert (org.junit.Assert)4 Test (org.junit.jupiter.api.Test)4