Skip to content

Commit

Permalink
Merge pull request #518 from mrapp-ke/fix-csr-prediction
Browse files Browse the repository at this point in the history
Vorhersage auf Basis einer CsrFeatureMatrix beheben
  • Loading branch information
michael-rapp authored Oct 4, 2021
2 parents 517c537 + 44fd2a5 commit bdd0c4a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,21 @@ namespace boosting {
CContiguousView<uint8>& predictionMatrix,
const RuleModel& model, const LabelVectorSet* labelVectors) const {
uint32 numExamples = featureMatrix.getNumRows();
uint32 numFeatures = featureMatrix.getNumCols();
uint32 numLabels = predictionMatrix.getNumCols();
const CsrFeatureMatrix* featureMatrixPtr = &featureMatrix;
CContiguousView<uint8>* predictionMatrixPtr = &predictionMatrix;
const RuleModel* modelPtr = &model;
const ISimilarityMeasure* measurePtr = measurePtr_.get();

#pragma omp parallel for firstprivate(numExamples) firstprivate(numLabels) firstprivate(modelPtr) \
firstprivate(featureMatrixPtr) firstprivate(predictionMatrixPtr) firstprivate(measurePtr) \
firstprivate(labelVectors) schedule(dynamic) num_threads(numThreads_)
#pragma omp parallel for firstprivate(numExamples) firstprivate(numFeatures) firstprivate(numLabels) \
firstprivate(modelPtr) firstprivate(featureMatrixPtr) firstprivate(predictionMatrixPtr) \
firstprivate(measurePtr) firstprivate(labelVectors) schedule(dynamic) num_threads(numThreads_)
for (uint32 i = 0; i < numExamples; i++) {
float64 scoreVector[numLabels] = {};
applyRulesCsr(*modelPtr, featureMatrixPtr->row_indices_cbegin(i), featureMatrixPtr->row_indices_cend(i),
featureMatrixPtr->row_values_cbegin(i), featureMatrixPtr->row_values_cend(i),
&scoreVector[0]);
applyRulesCsr(*modelPtr, numFeatures, featureMatrixPtr->row_indices_cbegin(i),
featureMatrixPtr->row_indices_cend(i), featureMatrixPtr->row_values_cbegin(i),
featureMatrixPtr->row_values_cend(i), &scoreVector[0]);
const LabelVector* closestLabelVector = findClosestLabelVector(&scoreVector[0], &scoreVector[numLabels],
*measurePtr, labelVectors);
predictLabelVector(predictionMatrixPtr->row_begin(i), numLabels, closestLabelVector);
Expand Down Expand Up @@ -175,21 +176,23 @@ namespace boosting {
const CsrFeatureMatrix& featureMatrix, uint32 numLabels, const RuleModel& model,
const LabelVectorSet* labelVectors) const {
uint32 numExamples = featureMatrix.getNumRows();
uint32 numFeatures = featureMatrix.getNumCols();
std::unique_ptr<LilMatrix<uint8>> lilMatrixPtr = std::make_unique<LilMatrix<uint8>>(numExamples);
const CsrFeatureMatrix* featureMatrixPtr = &featureMatrix;
LilMatrix<uint8>* predictionMatrixPtr = lilMatrixPtr.get();
const RuleModel* modelPtr = &model;
const ISimilarityMeasure* measurePtr = measurePtr_.get();
uint32 numNonZeroElements = 0;

#pragma omp parallel for reduction(+:numNonZeroElements) firstprivate(numExamples) firstprivate(numLabels) \
firstprivate(modelPtr) firstprivate(featureMatrixPtr) firstprivate(predictionMatrixPtr) \
firstprivate(measurePtr) firstprivate(labelVectors) schedule(dynamic) num_threads(numThreads_)
#pragma omp parallel for reduction(+:numNonZeroElements) firstprivate(numExamples) firstprivate(numFeatures) \
firstprivate(numLabels) firstprivate(modelPtr) firstprivate(featureMatrixPtr) \
firstprivate(predictionMatrixPtr) firstprivate(measurePtr) firstprivate(labelVectors) schedule(dynamic) \
num_threads(numThreads_)
for (uint32 i = 0; i < numExamples; i++) {
float64 scoreVector[numLabels] = {};
applyRulesCsr(*modelPtr, featureMatrixPtr->row_indices_cbegin(i), featureMatrixPtr->row_indices_cend(i),
featureMatrixPtr->row_values_cbegin(i), featureMatrixPtr->row_values_cend(i),
&scoreVector[0]);
applyRulesCsr(*modelPtr, numFeatures, featureMatrixPtr->row_indices_cbegin(i),
featureMatrixPtr->row_indices_cend(i), featureMatrixPtr->row_values_cbegin(i),
featureMatrixPtr->row_values_cend(i), &scoreVector[0]);
const LabelVector* closestLabelVector = findClosestLabelVector(&scoreVector[0], &scoreVector[numLabels],
*measurePtr, labelVectors);
numNonZeroElements += predictLabelVector(predictionMatrixPtr->getRow(i), closestLabelVector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,20 @@ namespace boosting {
CContiguousView<uint8>& predictionMatrix,
const RuleModel& model, const LabelVectorSet* labelVectors) const {
uint32 numExamples = featureMatrix.getNumRows();
uint32 numFeatures = featureMatrix.getNumCols();
uint32 numLabels = predictionMatrix.getNumCols();
const CsrFeatureMatrix* featureMatrixPtr = &featureMatrix;
CContiguousView<uint8>* predictionMatrixPtr = &predictionMatrix;
const RuleModel* modelPtr = &model;

#pragma omp parallel for firstprivate(numExamples) firstprivate(numLabels) firstprivate(threshold_) \
firstprivate(modelPtr) firstprivate(featureMatrixPtr) firstprivate(predictionMatrixPtr) schedule(dynamic) \
num_threads(numThreads_)
#pragma omp parallel for firstprivate(numExamples) firstprivate(numFeatures) firstprivate(numLabels) \
firstprivate(threshold_) firstprivate(modelPtr) firstprivate(featureMatrixPtr) \
firstprivate(predictionMatrixPtr) schedule(dynamic) num_threads(numThreads_)
for (uint32 i = 0; i < numExamples; i++) {
float64 scoreVector[numLabels] = {};
applyRulesCsr(*modelPtr, featureMatrixPtr->row_indices_cbegin(i), featureMatrixPtr->row_indices_cend(i),
featureMatrixPtr->row_values_cbegin(i), featureMatrixPtr->row_values_cend(i),
&scoreVector[0]);
applyRulesCsr(*modelPtr, numFeatures, featureMatrixPtr->row_indices_cbegin(i),
featureMatrixPtr->row_indices_cend(i), featureMatrixPtr->row_values_cbegin(i),
featureMatrixPtr->row_values_cend(i), &scoreVector[0]);
applyThreshold(&scoreVector[0], predictionMatrixPtr->row_begin(i), numLabels, threshold_);
}
}
Expand Down Expand Up @@ -117,20 +118,21 @@ namespace boosting {
const CsrFeatureMatrix& featureMatrix, uint32 numLabels, const RuleModel& model,
const LabelVectorSet* labelVectors) const {
uint32 numExamples = featureMatrix.getNumRows();
uint32 numFeatures = featureMatrix.getNumCols();
std::unique_ptr<LilMatrix<uint8>> lilMatrixPtr = std::make_unique<LilMatrix<uint8>>(numExamples);
const CsrFeatureMatrix* featureMatrixPtr = &featureMatrix;
LilMatrix<uint8>* predictionMatrixPtr = lilMatrixPtr.get();
const RuleModel* modelPtr = &model;
uint32 numNonZeroElements = 0;

#pragma omp parallel for reduction(+:numNonZeroElements) firstprivate(numExamples) firstprivate(numLabels) \
firstprivate(threshold_) firstprivate(modelPtr) firstprivate(featureMatrixPtr) \
#pragma omp parallel for reduction(+:numNonZeroElements) firstprivate(numExamples) firstprivate(numFeatures) \
firstprivate(numLabels) firstprivate(threshold_) firstprivate(modelPtr) firstprivate(featureMatrixPtr) \
firstprivate(predictionMatrixPtr) schedule(dynamic) num_threads(numThreads_)
for (uint32 i = 0; i < numExamples; i++) {
float64 scoreVector[numLabels] = {};
applyRulesCsr(*modelPtr, featureMatrixPtr->row_indices_cbegin(i), featureMatrixPtr->row_indices_cend(i),
featureMatrixPtr->row_values_cbegin(i), featureMatrixPtr->row_values_cend(i),
&scoreVector[0]);
applyRulesCsr(*modelPtr, numFeatures, featureMatrixPtr->row_indices_cbegin(i),
featureMatrixPtr->row_indices_cend(i), featureMatrixPtr->row_values_cbegin(i),
featureMatrixPtr->row_values_cend(i), &scoreVector[0]);
numNonZeroElements += applyThreshold(&scoreVector[0], predictionMatrixPtr->getRow(i), numLabels,
threshold_);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ namespace boosting {
}
}

static inline void applyRulesCsr(const RuleModel& model, CsrFeatureMatrix::index_const_iterator featureIndicesBegin,
static inline void applyRulesCsr(const RuleModel& model, uint32 numFeatures,
CsrFeatureMatrix::index_const_iterator featureIndicesBegin,
CsrFeatureMatrix::index_const_iterator featureIndicesEnd,
CsrFeatureMatrix::value_const_iterator featureValuesBegin,
CsrFeatureMatrix::value_const_iterator featureValuesEnd,
CContiguousView<float64>::iterator scoreIterator) {
uint32 numFeatures = featureIndicesEnd - featureIndicesBegin;
float32 tmpArray1[numFeatures];
uint32 tmpArray2[numFeatures] = {};
uint32 n = 1;
Expand Down

0 comments on commit bdd0c4a

Please sign in to comment.