From eb45cd8d1b6015564d1a685f80dd347ae74f9e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Arrufat?= Date: Tue, 3 Nov 2026 21:59:29 +0100 Subject: [PATCH 1/2] Require each Platt fold to hold out both classes --- dlib/svm/svm.h | 12 +++++++----- dlib/svm/svm_abstract.h | 8 ++++++-- dlib/test/probabilistic.cpp | 16 ++++++++++++++++ docs/docs/release_notes.xml | 2 ++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/dlib/svm/svm.h b/dlib/svm/svm.h index 5785e2fe7c..134b4fcdd6 100644 --- a/dlib/svm/svm.h +++ b/dlib/svm/svm.h @@ -788,21 +788,23 @@ namespace dlib by Hsuan-Tien Lin, Chih-Jen Lin, and Ruby C. Weng */ + // count the number of positive and negative examples + const long num_pos = (long)sum(mat(y) > 0); + const long num_neg = (long)sum(mat(y) < 0); + // make sure requires clause is not broken DLIB_ASSERT(is_binary_classification_problem(x,y) == true && - 1 < folds && folds <= (long)x.size(), + 1 < folds && folds <= std::min(num_pos,num_neg), "\tprobabilistic_decision_function train_probabilistic_decision_function()" << "\n\t invalid inputs were given to this function" << "\n\t x.size(): " << x.size() << "\n\t y.size(): " << y.size() << "\n\t folds: " << folds + << "\n\t num_pos: " << num_pos + << "\n\t num_neg: " << num_neg << "\n\t is_binary_classification_problem(x,y): " << is_binary_classification_problem(x,y) ); - // count the number of positive and negative examples - const long num_pos = (long)sum(mat(y) > 0); - const long num_neg = (long)sum(mat(y) < 0); - // figure out how many positive and negative examples we will have in each fold const long num_pos_test_samples = num_pos/folds; const long num_pos_train_samples = num_pos - num_pos_test_samples; diff --git a/dlib/svm/svm_abstract.h b/dlib/svm/svm_abstract.h index ec92cf55b0..b09a2dfab0 100644 --- a/dlib/svm/svm_abstract.h +++ b/dlib/svm/svm_abstract.h @@ -315,8 +315,9 @@ namespace dlib ); /*! requires - - 1 < folds <= x.size() - is_binary_classification_problem(x,y) == true + - 1 < folds <= std::min(sum(mat(y)>0),sum(mat(y)<0)) + (i.e. at least as many samples of each class as folds) - x and y must be std::vector objects or types with a compatible interface. - trainer_type == some kind of batch trainer object (e.g. svm_nu_trainer) ensures @@ -325,6 +326,8 @@ namespace dlib - The parameters of the probability model are estimated by performing k-fold cross validation. - The number of folds used is given by the folds argument. + - Each fold holds out consecutive samples of each class, in the order of x, + so call randomize_samples(x,y) first if similar samples sit together. - This function is implemented using learn_platt_scaling() throws - any exceptions thrown by trainer.train() @@ -342,11 +345,12 @@ namespace dlib ); /*! requires - - 1 < folds <= x.size() + - 1 < folds - trainer_type == some kind of batch trainer object (e.g. svm_nu_trainer) ensures - returns a trainer adapter TA such that calling TA.train(samples, labels) returns the same object as calling train_probabilistic_decision_function(trainer,samples,labels,folds). + So TA.train(samples, labels) has the requirements of that function. !*/ // ---------------------------------------------------------------------------------------- diff --git a/dlib/test/probabilistic.cpp b/dlib/test/probabilistic.cpp index e8a24829a9..121acd6fe2 100644 --- a/dlib/test/probabilistic.cpp +++ b/dlib/test/probabilistic.cpp @@ -115,6 +115,22 @@ namespace rs_pos.clear(); rs_neg.clear(); + // There can be as many folds as there are samples of the smaller class. + print_spinner(); + std::vector few_x; + std::vector few_y; + long num_pos = 0, num_neg = 0; + for (unsigned long i = 0; i < x.size(); ++i) + { + if ((y[i] > 0 && num_pos++ < 3) || (y[i] < 0 && num_neg++ < 47)) + { + few_x.push_back(x[i]); + few_y.push_back(y[i]); + } + } + df = train_probabilistic_decision_function(krr_trainer, few_x, few_y, 3); + DLIB_TEST(std::isfinite(df.alpha) && std::isfinite(df.beta)); + } } a; diff --git a/docs/docs/release_notes.xml b/docs/docs/release_notes.xml index 6f863137eb..ef727e8203 100644 --- a/docs/docs/release_notes.xml +++ b/docs/docs/release_notes.xml @@ -58,6 +58,8 @@ Non-Backwards Compatible Changes: reference. OBJECT_PART_NOT_PRESENT is also a dpoint. Serialized objects use version 2, which older dlib versions cannot read. (PR #3095) - Image width steps now use size_t instead of long. (PR #3091) + - train_probabilistic_decision_function() now requires at least as many samples of + each class as folds. - bpe_tokenizer::train() now takes max_bytes as its third argument and verbose as its fourth. Change train(text, size, true) to train(text, size, 0, true). (PR #3078) - The BPE tokenizer serialization format and token IDs have changed. Saved v20.0 From 2d8e36ef8e70154799d06566dfb80009405c5881 Mon Sep 17 00:00:00 2001 From: Davis King Date: Fri, 6 Nov 2026 18:55:00 -0500 Subject: [PATCH 2/2] A bit more checking/testing --- dlib/svm/svm.h | 5 +++++ dlib/test/probabilistic.cpp | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/dlib/svm/svm.h b/dlib/svm/svm.h index 134b4fcdd6..560f0ebc29 100644 --- a/dlib/svm/svm.h +++ b/dlib/svm/svm.h @@ -939,6 +939,11 @@ namespace dlib const long folds ) { + DLIB_ASSERT(folds > 1, + "\t trainer_adapter_probabilistic probabilistic()" + << "\n\t folds must be greater than 1" + << "\n\t folds: " << folds + ); return trainer_adapter_probabilistic(trainer,folds); } diff --git a/dlib/test/probabilistic.cpp b/dlib/test/probabilistic.cpp index 121acd6fe2..8aab09371e 100644 --- a/dlib/test/probabilistic.cpp +++ b/dlib/test/probabilistic.cpp @@ -128,8 +128,23 @@ namespace few_y.push_back(y[i]); } } - df = train_probabilistic_decision_function(krr_trainer, few_x, few_y, 3); - DLIB_TEST(std::isfinite(df.alpha) && std::isfinite(df.beta)); + DLIB_TEST(sum(mat(few_y) > 0) == 3); + DLIB_TEST(sum(mat(few_y) < 0) == 47); + for (int sign : {1, -1}) + { + std::vector labels = few_y; + for (auto& label : labels) + label *= sign; + df = train_probabilistic_decision_function(krr_trainer, few_x, labels, 3); + DLIB_TEST(std::isfinite(df.alpha) && std::isfinite(df.beta)); + const auto adapted = probabilistic(krr_trainer, 3).train(few_x, labels); + for (const auto& sample : few_x) + { + const auto probability = df(sample); + DLIB_TEST(probability >= 0 && probability <= 1); + DLIB_TEST(std::abs(probability - adapted(sample)) < 1e-12); + } + } } } a;