Commit 960f9cde authored by Davis King's avatar Davis King

Added a per node loss option to the structural_svm_graph_labeling_problem's

interface.
parent 374459b4
...@@ -180,7 +180,8 @@ namespace dlib ...@@ -180,7 +180,8 @@ namespace dlib
); );
structural_svm_graph_labeling_problem<graph_type> prob(samples, labels, num_threads); std::vector<std::vector<double> > losses;
structural_svm_graph_labeling_problem<graph_type> prob(samples, labels, losses, num_threads);
if (verbose) if (verbose)
prob.be_verbose(); prob.be_verbose();
......
...@@ -83,6 +83,46 @@ namespace dlib ...@@ -83,6 +83,46 @@ namespace dlib
return true; return true;
} }
// ----------------------------------------------------------------------------------------
template <
typename T,
typename U
>
bool sizes_match (
const std::vector<std::vector<T> >& lhs,
const std::vector<std::vector<U> >& rhs
)
{
if (lhs.size() != rhs.size())
return false;
for (unsigned long i = 0; i < lhs.size(); ++i)
{
if (lhs[i].size() != rhs[i].size())
return false;
}
return true;
}
// ----------------------------------------------------------------------------------------
inline bool all_values_are_nonnegative (
const std::vector<std::vector<double> >& x
)
{
for (unsigned long i = 0; i < x.size(); ++i)
{
for (unsigned long j = 0; j < x[i].size(); ++j)
{
if (x[i][j] < 0)
return false;
}
}
return true;
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -135,18 +175,25 @@ namespace dlib ...@@ -135,18 +175,25 @@ namespace dlib
structural_svm_graph_labeling_problem( structural_svm_graph_labeling_problem(
const dlib::array<sample_type>& samples_, const dlib::array<sample_type>& samples_,
const std::vector<label_type>& labels_, const std::vector<label_type>& labels_,
const std::vector<std::vector<double> >& losses_,
unsigned long num_threads = 2 unsigned long num_threads = 2
) : ) :
structural_svm_problem_threaded<matrix_type,feature_vector_type>(num_threads), structural_svm_problem_threaded<matrix_type,feature_vector_type>(num_threads),
samples(samples_), samples(samples_),
labels(labels_) labels(labels_),
losses(losses_)
{ {
// make sure requires clause is not broken // make sure requires clause is not broken
DLIB_ASSERT(is_graph_labeling_problem(samples, labels) == true, DLIB_ASSERT(is_graph_labeling_problem(samples, labels) == true &&
(losses.size() == 0 || sizes_match(labels, losses) == true) &&
all_values_are_nonnegative(losses) == true,
"\t structural_svm_graph_labeling_problem::structural_svm_graph_labeling_problem()" "\t structural_svm_graph_labeling_problem::structural_svm_graph_labeling_problem()"
<< "\n\t Invalid inputs were given to this function." << "\n\t Invalid inputs were given to this function."
<< "\n\t samples.size(): " << samples.size() << "\n\t samples.size(): " << samples.size()
<< "\n\t labels.size(): " << labels.size() << "\n\t labels.size(): " << labels.size()
<< "\n\t losses.size(): " << losses.size()
<< "\n\t sizes_match(labels,losses): " << sizes_match(labels,losses)
<< "\n\t all_values_are_nonnegative(losses): " << all_values_are_nonnegative(losses)
<< "\n\t this: " << this ); << "\n\t this: " << this );
loss_pos = 1.0; loss_pos = 1.0;
...@@ -168,6 +215,9 @@ namespace dlib ...@@ -168,6 +215,9 @@ namespace dlib
} }
} }
const std::vector<std::vector<double> >& get_losses (
) const { return losses; }
long get_num_edge_weights ( long get_num_edge_weights (
) const ) const
{ {
...@@ -179,8 +229,8 @@ namespace dlib ...@@ -179,8 +229,8 @@ namespace dlib
) )
{ {
// make sure requires clause is not broken // make sure requires clause is not broken
DLIB_ASSERT(loss >= 0, DLIB_ASSERT(loss >= 0 && get_losses().size() == 0,
"\t structural_svm_graph_labeling_problem::set_loss_on_positive_class()" "\t void structural_svm_graph_labeling_problem::set_loss_on_positive_class()"
<< "\n\t Invalid inputs were given to this function." << "\n\t Invalid inputs were given to this function."
<< "\n\t loss: " << loss << "\n\t loss: " << loss
<< "\n\t this: " << this ); << "\n\t this: " << this );
...@@ -193,8 +243,8 @@ namespace dlib ...@@ -193,8 +243,8 @@ namespace dlib
) )
{ {
// make sure requires clause is not broken // make sure requires clause is not broken
DLIB_ASSERT(loss >= 0, DLIB_ASSERT(loss >= 0 && get_losses().size() == 0,
"\t structural_svm_graph_labeling_problem::set_loss_on_negative_class()" "\t void structural_svm_graph_labeling_problem::set_loss_on_negative_class()"
<< "\n\t Invalid inputs were given to this function." << "\n\t Invalid inputs were given to this function."
<< "\n\t loss: " << loss << "\n\t loss: " << loss
<< "\n\t this: " << this ); << "\n\t this: " << this );
...@@ -203,10 +253,28 @@ namespace dlib ...@@ -203,10 +253,28 @@ namespace dlib
} }
double get_loss_on_negative_class ( double get_loss_on_negative_class (
) const { return loss_neg; } ) const
{
// make sure requires clause is not broken
DLIB_ASSERT(get_losses().size() == 0,
"\t double structural_svm_graph_labeling_problem::get_loss_on_negative_class()"
<< "\n\t Invalid inputs were given to this function."
<< "\n\t this: " << this );
return loss_neg;
}
double get_loss_on_positive_class ( double get_loss_on_positive_class (
) const { return loss_pos; } ) const
{
// make sure requires clause is not broken
DLIB_ASSERT(get_losses().size() == 0,
"\t double structural_svm_graph_labeling_problem::get_loss_on_positive_class()"
<< "\n\t Invalid inputs were given to this function."
<< "\n\t this: " << this );
return loss_pos;
}
private: private:
...@@ -386,7 +454,9 @@ namespace dlib ...@@ -386,7 +454,9 @@ namespace dlib
const bool true_label = labels[sample_idx][node_idx]; const bool true_label = labels[sample_idx][node_idx];
if (true_label != predicted_label) if (true_label != predicted_label)
{ {
if (true_label == true) if (losses.size() != 0)
return losses[sample_idx][node_idx];
else if (true_label == true)
return loss_pos; return loss_pos;
else else
return loss_neg; return loss_neg;
...@@ -400,6 +470,7 @@ namespace dlib ...@@ -400,6 +470,7 @@ namespace dlib
const dlib::array<sample_type>& samples; const dlib::array<sample_type>& samples;
const std::vector<label_type>& labels; const std::vector<label_type>& labels;
const std::vector<std::vector<double> >& losses;
long node_dims; long node_dims;
long edge_dims; long edge_dims;
......
...@@ -55,6 +55,36 @@ namespace dlib ...@@ -55,6 +55,36 @@ namespace dlib
- All vectors have non-zero size. That is, they have more than 0 dimensions. - All vectors have non-zero size. That is, they have more than 0 dimensions.
!*/ !*/
// ----------------------------------------------------------------------------------------
template <
typename T,
typename U
>
bool sizes_match (
const std::vector<std::vector<T> >& lhs,
const std::vector<std::vector<U> >& rhs
);
/*!
ensures
- returns true if the sizes of lhs and rhs, as well as their constituent vectors
all match. In particular, we return true if all of the following conditions are
met and false otherwise:
- lhs.size() == rhs.size()
- for all valid i:
- lhs[i].size() == rhs[i].size()
!*/
// ----------------------------------------------------------------------------------------
bool all_values_are_nonnegative (
const std::vector<std::vector<double> >& x
);
/*!
ensures
- returns true if all the double values contained in x are >= 0.
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -87,11 +117,15 @@ namespace dlib ...@@ -87,11 +117,15 @@ namespace dlib
structural_svm_graph_labeling_problem( structural_svm_graph_labeling_problem(
const dlib::array<sample_type>& samples, const dlib::array<sample_type>& samples,
const std::vector<label_type>& labels, const std::vector<label_type>& labels,
const std::vector<std::vector<double> >& losses,
unsigned long num_threads unsigned long num_threads
); );
/*! /*!
requires requires
- is_graph_labeling_problem(samples,labels) == true - is_graph_labeling_problem(samples,labels) == true
- if (losses.size() != 0) then
- sizes_match(labels, losses) == true
- all_values_are_nonnegative(losses) == true
ensures ensures
- This object attempts to learn a mapping from the given samples to the - This object attempts to learn a mapping from the given samples to the
given labels. In particular, it attempts to learn to predict labels[i] given labels. In particular, it attempts to learn to predict labels[i]
...@@ -107,8 +141,29 @@ namespace dlib ...@@ -107,8 +141,29 @@ namespace dlib
- This object will use num_threads threads during the optimization - This object will use num_threads threads during the optimization
procedure. You should set this parameter equal to the number of procedure. You should set this parameter equal to the number of
available processing cores on your machine. available processing cores on your machine.
- #get_loss_on_positive_class() == 1.0 - if (losses.size() == 0) then
- #get_loss_on_negative_class() == 1.0 - #get_loss_on_positive_class() == 1.0
- #get_loss_on_negative_class() == 1.0
- #get_losses().size() == 0
- the losses argument is effectively ignored if its size is zero.
- else
- #get_losses() == losses
- Each node in the training data has its own loss value defined by
the corresponding entry of losses. In particular, this means that
the node with label labels[i][j] incurs a loss of losses[i][j] if
it is incorrectly labeled.
- The get_loss_on_positive_class() and get_loss_on_negative_class()
parameters are ignored. Only get_losses() is used in this case.
!*/
const std::vector<std::vector<double> >& get_losses (
) const;
/*!
ensures
- returns the losses vector given to this object's constructor.
This vector defines the per sample loss values used. If the vector
is empty then the loss values defined by get_loss_on_positive_class() and
get_loss_on_positive_class() are used instead.
!*/ !*/
long get_num_edge_weights ( long get_num_edge_weights (
...@@ -128,6 +183,7 @@ namespace dlib ...@@ -128,6 +183,7 @@ namespace dlib
/*! /*!
requires requires
- loss >= 0 - loss >= 0
- get_losses().size() == 0
ensures ensures
- #get_loss_on_positive_class() == loss - #get_loss_on_positive_class() == loss
!*/ !*/
...@@ -138,6 +194,7 @@ namespace dlib ...@@ -138,6 +194,7 @@ namespace dlib
/*! /*!
requires requires
- loss >= 0 - loss >= 0
- get_losses().size() == 0
ensures ensures
- #get_loss_on_negative_class() == loss - #get_loss_on_negative_class() == loss
!*/ !*/
...@@ -145,6 +202,8 @@ namespace dlib ...@@ -145,6 +202,8 @@ namespace dlib
double get_loss_on_positive_class ( double get_loss_on_positive_class (
) const; ) const;
/*! /*!
requires
- get_losses().size() == 0
ensures ensures
- returns the loss incurred when a graph node which is supposed to have - returns the loss incurred when a graph node which is supposed to have
a label of true gets misclassified. This value controls how much we care a label of true gets misclassified. This value controls how much we care
...@@ -155,6 +214,8 @@ namespace dlib ...@@ -155,6 +214,8 @@ namespace dlib
double get_loss_on_negative_class ( double get_loss_on_negative_class (
) const; ) const;
/*! /*!
requires
- get_losses().size() == 0
ensures ensures
- returns the loss incurred when a graph node which is supposed to have - returns the loss incurred when a graph node which is supposed to have
a label of false gets misclassified. This value controls how much we care a label of false gets misclassified. This value controls how much we care
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment