Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions dlib/svm/svm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -937,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_type>(trainer,folds);
}

Expand Down
8 changes: 6 additions & 2 deletions dlib/svm/svm_abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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.
!*/

// ----------------------------------------------------------------------------------------
Expand Down
31 changes: 31 additions & 0 deletions dlib/test/probabilistic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,37 @@ 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<sample_type> few_x;
std::vector<scalar_type> 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]);
}
}
DLIB_TEST(sum(mat(few_y) > 0) == 3);
DLIB_TEST(sum(mat(few_y) < 0) == 47);
for (int sign : {1, -1})
{
std::vector<scalar_type> 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;

Expand Down
2 changes: 2 additions & 0 deletions docs/docs/release_notes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading