From 11bc9a52889eb8ab0e5760650fea7470b4605803 Mon Sep 17 00:00:00 2001 From: Dirk Eddelbuettel Date: Tue, 19 Nov 2024 08:58:32 -0600 Subject: [PATCH 1/4] Update deprecated .max(ind), .min(ind) to .index_max(), .index_min() This is an Armadillo 14.2.0 change that creates a fair amount of compilation noise for mlpack right now. --- src/mlpack/methods/adaboost/adaboost_impl.hpp | 2 +- src/mlpack/methods/approx_kfn/drusilla_select_impl.hpp | 3 +-- src/mlpack/methods/decision_tree/decision_tree_impl.hpp | 3 +-- src/mlpack/methods/hmm/hmm_impl.hpp | 5 +++-- .../hoeffding_trees/binary_numeric_split_impl.hpp | 8 +++----- .../hoeffding_trees/hoeffding_categorical_split_impl.hpp | 6 ++---- .../hoeffding_trees/hoeffding_numeric_split_impl.hpp | 9 +++------ .../methods/kmeans/max_variance_new_cluster_impl.hpp | 3 +-- .../methods/naive_bayes/naive_bayes_classifier_impl.hpp | 3 +-- src/mlpack/methods/perceptron/perceptron_impl.hpp | 2 +- src/mlpack/methods/radical/radical_impl.hpp | 3 +-- src/mlpack/methods/random_forest/random_forest_impl.hpp | 3 +-- 12 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/mlpack/methods/adaboost/adaboost_impl.hpp b/src/mlpack/methods/adaboost/adaboost_impl.hpp index 7609ee77499..8cac31f9cba 100644 --- a/src/mlpack/methods/adaboost/adaboost_impl.hpp +++ b/src/mlpack/methods/adaboost/adaboost_impl.hpp @@ -204,7 +204,7 @@ void AdaBoost::Classify( for (size_t i = 0; i < predictedLabels.n_cols; ++i) { probabilities.col(i) /= accu(probabilities.col(i)); - probabilities.col(i).max(maxIndex); + maxIndex = probabilities.col(i).index_max(); predictedLabels(i) = maxIndex; } } diff --git a/src/mlpack/methods/approx_kfn/drusilla_select_impl.hpp b/src/mlpack/methods/approx_kfn/drusilla_select_impl.hpp index f66d7018931..89988b0a713 100644 --- a/src/mlpack/methods/approx_kfn/drusilla_select_impl.hpp +++ b/src/mlpack/methods/approx_kfn/drusilla_select_impl.hpp @@ -94,8 +94,7 @@ void DrusillaSelect::Train( for (size_t i = 0; i < l; ++i) { // Pick best index. - arma::uword maxIndex = 0; - norms.max(maxIndex); + arma::uword maxIndex = norms.index_max(); arma::vec line(refCopy.col(maxIndex) / norm(refCopy.col(maxIndex))); diff --git a/src/mlpack/methods/decision_tree/decision_tree_impl.hpp b/src/mlpack/methods/decision_tree/decision_tree_impl.hpp index c677e34c475..4bb8cfeb62c 100644 --- a/src/mlpack/methods/decision_tree/decision_tree_impl.hpp +++ b/src/mlpack/methods/decision_tree/decision_tree_impl.hpp @@ -1159,8 +1159,7 @@ void DecisionTree::Predict(const arma::mat& dataSeq, for (size_t j = 0; j < logTransition.n_rows; j++) { arma::vec prob = logStateProb.col(t - 1) + logTransition.row(j).t(); - logStateProb(j, t) = prob.max(index) + logProbs(t, j); + index = prob.index_max(); + logStateProb(j, t) = index + logProbs(t, j); stateSeqBack(j, t) = index; } } // Backtrack to find the most probable state sequence. - logStateProb.unsafe_col(dataSeq.n_cols - 1).max(index); + index = logStateProb.unsafe_col(dataSeq.n_cols - 1).index_max(); stateSeq[dataSeq.n_cols - 1] = index; for (size_t t = 2; t <= dataSeq.n_cols; t++) { diff --git a/src/mlpack/methods/hoeffding_trees/binary_numeric_split_impl.hpp b/src/mlpack/methods/hoeffding_trees/binary_numeric_split_impl.hpp index bd5cbb5de28..85dd641503c 100644 --- a/src/mlpack/methods/hoeffding_trees/binary_numeric_split_impl.hpp +++ b/src/mlpack/methods/hoeffding_trees/binary_numeric_split_impl.hpp @@ -141,10 +141,9 @@ void BinaryNumericSplit::Split( } // Calculate the majority classes of the children. - arma::uword maxIndex; - counts.unsafe_col(0).max(maxIndex); + arma::uword maxIndex = counts.unsafe_col(0).index_max(); childMajorities[0] = size_t(maxIndex); - counts.unsafe_col(1).max(maxIndex); + maxIndex = counts.unsafe_col(1).index_max(); childMajorities[1] = size_t(maxIndex); // Create the according SplitInfo object. @@ -155,8 +154,7 @@ template size_t BinaryNumericSplit::MajorityClass() const { - arma::uword maxIndex; - classCounts.max(maxIndex); + arma::uword maxIndex = classCounts.index_max(); return size_t(maxIndex); } diff --git a/src/mlpack/methods/hoeffding_trees/hoeffding_categorical_split_impl.hpp b/src/mlpack/methods/hoeffding_trees/hoeffding_categorical_split_impl.hpp index 6e35b5a1c30..810d7dc638e 100644 --- a/src/mlpack/methods/hoeffding_trees/hoeffding_categorical_split_impl.hpp +++ b/src/mlpack/methods/hoeffding_trees/hoeffding_categorical_split_impl.hpp @@ -64,8 +64,7 @@ void HoeffdingCategoricalSplit::Split( childMajorities.set_size(sufficientStatistics.n_cols); for (size_t i = 0; i < sufficientStatistics.n_cols; ++i) { - arma::uword maxIndex = 0; - sufficientStatistics.unsafe_col(i).max(maxIndex); + arma::uword maxIndex = sufficientStatistics.unsafe_col(i).index_max(); childMajorities[i] = size_t(maxIndex); } @@ -79,8 +78,7 @@ size_t HoeffdingCategoricalSplit::MajorityClass() const // Calculate the class that we have seen the most of. arma::Col classCounts = sum(sufficientStatistics, 1); - arma::uword maxIndex = 0; - classCounts.max(maxIndex); + arma::uword maxIndex = classCounts.index_max(); return size_t(maxIndex); } diff --git a/src/mlpack/methods/hoeffding_trees/hoeffding_numeric_split_impl.hpp b/src/mlpack/methods/hoeffding_trees/hoeffding_numeric_split_impl.hpp index 617ff288c72..e6b425a805d 100644 --- a/src/mlpack/methods/hoeffding_trees/hoeffding_numeric_split_impl.hpp +++ b/src/mlpack/methods/hoeffding_trees/hoeffding_numeric_split_impl.hpp @@ -122,8 +122,7 @@ void HoeffdingNumericSplit::Split( childMajorities.set_size(sufficientStatistics.n_cols); for (size_t i = 0; i < sufficientStatistics.n_cols; ++i) { - arma::uword maxIndex = 0; - sufficientStatistics.unsafe_col(i).max(maxIndex); + arma::uword maxIndex = sufficientStatistics.unsafe_col(i).index_max(); childMajorities[i] = size_t(maxIndex); } @@ -144,8 +143,7 @@ size_t HoeffdingNumericSplit:: for (size_t i = 0; i < samplesSeen; ++i) classes[labels[i]]++; - arma::uword majorityClass; - classes.max(majorityClass); + arma::uword majorityClass = classes.index_max(); return size_t(majorityClass); } else @@ -154,8 +152,7 @@ size_t HoeffdingNumericSplit:: // statistics. arma::Col classCounts = sum(sufficientStatistics, 1); - arma::uword maxIndex = 0; - classCounts.max(maxIndex); + arma::uword maxIndex = classCounts.index_max(); return size_t(maxIndex); } } diff --git a/src/mlpack/methods/kmeans/max_variance_new_cluster_impl.hpp b/src/mlpack/methods/kmeans/max_variance_new_cluster_impl.hpp index c3a86845c5b..87b4a9ca71e 100644 --- a/src/mlpack/methods/kmeans/max_variance_new_cluster_impl.hpp +++ b/src/mlpack/methods/kmeans/max_variance_new_cluster_impl.hpp @@ -35,8 +35,7 @@ void MaxVarianceNewCluster::EmptyCluster(const MatType& data, this->iteration = iteration; // Now find the cluster with maximum variance. - arma::uword maxVarCluster = 0; - variances.max(maxVarCluster); + arma::uword maxVarCluster = variances.index_max(); // If the cluster with maximum variance has variance of 0, then we can't // continue. All the points are the same. diff --git a/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp b/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp index 16343bc01c3..c39c4d41dcb 100644 --- a/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp +++ b/src/mlpack/methods/naive_bayes/naive_bayes_classifier_impl.hpp @@ -384,8 +384,7 @@ void NaiveBayesClassifier::Classify( // Now calculate maximum probabilities for each point. for (size_t i = 0; i < data.n_cols; ++i) { - arma::uword maxIndex = 0; - logLikelihoods.unsafe_col(i).max(maxIndex); + arma::uword maxIndex = logLikelihoods.unsafe_col(i).index_max(); predictions[i] = maxIndex; } } diff --git a/src/mlpack/methods/perceptron/perceptron_impl.hpp b/src/mlpack/methods/perceptron/perceptron_impl.hpp index c03533889cb..46312f542f9 100644 --- a/src/mlpack/methods/perceptron/perceptron_impl.hpp +++ b/src/mlpack/methods/perceptron/perceptron_impl.hpp @@ -322,7 +322,7 @@ void Perceptron::Classify( for (size_t i = 0; i < test.n_cols; ++i) { tempLabelMat = weights.t() * test.col(i) + biases; - tempLabelMat.max(maxIndex); + maxIndex = tempLabelMat.index_max(); predictedLabels(i) = maxIndex; } } diff --git a/src/mlpack/methods/radical/radical_impl.hpp b/src/mlpack/methods/radical/radical_impl.hpp index 6926919c990..8327a85d149 100644 --- a/src/mlpack/methods/radical/radical_impl.hpp +++ b/src/mlpack/methods/radical/radical_impl.hpp @@ -103,8 +103,7 @@ inline typename MatType::elem_type Radical::Apply2D(const MatType& matX, values(i) = Vasicek(candidateY1, m) + Vasicek(candidateY2, m); } - arma::uword indOpt = 0; - values.min(indOpt); // we ignore the return value; we don't care about it + arma::uword indOpt = values.index_min(); return (indOpt / (ElemType) angles) * M_PI / 2.0; } diff --git a/src/mlpack/methods/random_forest/random_forest_impl.hpp b/src/mlpack/methods/random_forest/random_forest_impl.hpp index 5946d1a66cb..22141111921 100644 --- a/src/mlpack/methods/random_forest/random_forest_impl.hpp +++ b/src/mlpack/methods/random_forest/random_forest_impl.hpp @@ -356,8 +356,7 @@ void RandomForest< // Find maximum element after renormalizing probabilities. probabilities /= trees.size(); - arma::uword maxIndex = 0; - probabilities.max(maxIndex); + arma::uword maxIndex = probabilities.index_max(); // Set prediction. prediction = (size_t) maxIndex; From 11dab87b59f749bbb1e4611350fdf925b0bb09ea Mon Sep 17 00:00:00 2001 From: Dirk Eddelbuettel Date: Wed, 20 Nov 2024 08:56:34 -0600 Subject: [PATCH 2/4] Correct use of index_max() result --- src/mlpack/methods/hmm/hmm_impl.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/hmm/hmm_impl.hpp b/src/mlpack/methods/hmm/hmm_impl.hpp index ef4f0a800e7..7589124cbd7 100644 --- a/src/mlpack/methods/hmm/hmm_impl.hpp +++ b/src/mlpack/methods/hmm/hmm_impl.hpp @@ -529,7 +529,7 @@ double HMM::Predict(const arma::mat& dataSeq, { arma::vec prob = logStateProb.col(t - 1) + logTransition.row(j).t(); index = prob.index_max(); - logStateProb(j, t) = index + logProbs(t, j); + logStateProb(j, t) = prob[index] + logProbs(t, j); stateSeqBack(j, t) = index; } } From 6efd91fe936206d2eff01f888290628d2f43bec1 Mon Sep 17 00:00:00 2001 From: Dirk Eddelbuettel Date: Wed, 20 Nov 2024 13:28:36 -0600 Subject: [PATCH 3/4] Revert perceptron_impl.hpp --- src/mlpack/methods/perceptron/perceptron_impl.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/perceptron/perceptron_impl.hpp b/src/mlpack/methods/perceptron/perceptron_impl.hpp index 46312f542f9..c03533889cb 100644 --- a/src/mlpack/methods/perceptron/perceptron_impl.hpp +++ b/src/mlpack/methods/perceptron/perceptron_impl.hpp @@ -322,7 +322,7 @@ void Perceptron::Classify( for (size_t i = 0; i < test.n_cols; ++i) { tempLabelMat = weights.t() * test.col(i) + biases; - maxIndex = tempLabelMat.index_max(); + tempLabelMat.max(maxIndex); predictedLabels(i) = maxIndex; } } From 9ed1f4b04d14cc69d6f6f7a516e39482d5fea432 Mon Sep 17 00:00:00 2001 From: conradsnicta Date: Fri, 22 Nov 2024 06:12:25 +0100 Subject: [PATCH 4/4] update to use .index_max() --- src/mlpack/methods/perceptron/perceptron_impl.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mlpack/methods/perceptron/perceptron_impl.hpp b/src/mlpack/methods/perceptron/perceptron_impl.hpp index c03533889cb..021ef656c92 100644 --- a/src/mlpack/methods/perceptron/perceptron_impl.hpp +++ b/src/mlpack/methods/perceptron/perceptron_impl.hpp @@ -243,8 +243,8 @@ void Perceptron< // Multiply for each variable and check whether the current weight vector // correctly classifies this. tempLabelMat = weights.t() * data.col(j) + biases; - - tempLabelMat.max(maxIndexRow, maxIndexCol); + + maxIndexRow = arma::ind2sub(arma::size(tempLabelMat), tempLabelMat.index_max())(0); // Check whether prediction is correct. if (maxIndexRow != labels(0, j)) @@ -289,7 +289,7 @@ size_t Perceptron::Classify( arma::uword maxIndex = 0; tempLabelVec = weights.t() * point + biases; - tempLabelVec.max(maxIndex); + maxIndex = tempLabelVec.index_max(); return size_t(maxIndex); } @@ -322,7 +322,7 @@ void Perceptron::Classify( for (size_t i = 0; i < test.n_cols; ++i) { tempLabelMat = weights.t() * test.col(i) + biases; - tempLabelMat.max(maxIndex); + maxIndex = tempLabelMat.index_max(); predictedLabels(i) = maxIndex; } }