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);
}
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();
}
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);
}
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();
}
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);
}
Aggregations