Commit df415d7e authored by Davis King's avatar Davis King

I changed the sequence labeling stuff to allow a user defined

sequence type.  So these changes are basically just renaming some
template arguments and minor changes to some typedefs.
parent 11113836
......@@ -16,11 +16,11 @@ namespace dlib
template <
typename sequence_labeler_type,
typename sample_type
typename sequence_type
>
const matrix<double> test_sequence_labeler (
const sequence_labeler_type& labeler,
const std::vector<std::vector<sample_type> >& samples,
const std::vector<sequence_type>& samples,
const std::vector<std::vector<unsigned long> >& labels
)
{
......@@ -59,11 +59,11 @@ namespace dlib
template <
typename trainer_type,
typename sample_type
typename sequence_type
>
const matrix<double> cross_validate_sequence_labeler (
const trainer_type& trainer,
const std::vector<std::vector<sample_type> >& samples,
const std::vector<sequence_type>& samples,
const std::vector<std::vector<unsigned long> >& labels,
const long folds
)
......@@ -102,7 +102,7 @@ namespace dlib
const long num_in_test = samples.size()/folds;
const long num_in_train = samples.size() - num_in_test;
std::vector<std::vector<sample_type> > x_test, x_train;
std::vector<sequence_type> x_test, x_train;
std::vector<std::vector<unsigned long> > y_test, y_train;
......
......@@ -15,11 +15,11 @@ namespace dlib
template <
typename sequence_labeler_type,
typename sample_type
typename sequence_type
>
const matrix<double> test_sequence_labeler (
const sequence_labeler_type& labeler,
const std::vector<std::vector<sample_type> >& samples,
const std::vector<sequence_type>& samples,
const std::vector<std::vector<unsigned long> >& labels
);
/*!
......@@ -43,11 +43,11 @@ namespace dlib
template <
typename trainer_type,
typename sample_type
typename sequence_type
>
const matrix<double> cross_validate_sequence_labeler (
const trainer_type& trainer,
const std::vector<std::vector<sample_type> >& samples,
const std::vector<sequence_type>& samples,
const std::vector<std::vector<unsigned long> >& labels,
const long folds
);
......
......@@ -39,11 +39,11 @@ namespace dlib
double value;
};
template <typename feature_extractor, typename EXP, typename sample_type, typename EXP2>
template <typename feature_extractor, typename EXP, typename sequence_type, typename EXP2>
double dot(
const matrix_exp<EXP>& lambda,
const feature_extractor& fe,
const std::vector<sample_type>& sequence,
const sequence_type& sequence,
const matrix_exp<EXP2>& candidate_labeling,
unsigned long position
)
......@@ -61,7 +61,7 @@ namespace dlib
{
template <
typename T,
bool (T::*funct)(const std::vector<typename T::sample_type>&, const matrix_exp<matrix<unsigned long> >&, unsigned long)const
bool (T::*funct)(const typename T::sequence_type&, const matrix_exp<matrix<unsigned long> >&, unsigned long)const
>
struct hrlh_helper
{
......@@ -100,10 +100,10 @@ namespace dlib
};
template <typename feature_extractor, typename EXP, typename sample_type>
template <typename feature_extractor, typename EXP, typename sequence_type>
typename enable_if<has_reject_labeling<feature_extractor>,bool>::type call_reject_labeling_if_exists (
const feature_extractor& fe,
const std::vector<sample_type>& x,
const sequence_type& x,
const matrix_exp<EXP>& y,
unsigned long position
)
......@@ -111,10 +111,10 @@ namespace dlib
return fe.reject_labeling(x, y, position);
}
template <typename feature_extractor, typename EXP, typename sample_type>
template <typename feature_extractor, typename EXP, typename sequence_type>
typename disable_if<has_reject_labeling<feature_extractor>,bool>::type call_reject_labeling_if_exists (
const feature_extractor& ,
const std::vector<sample_type>& ,
const sequence_type& ,
const matrix_exp<EXP>& ,
unsigned long
)
......@@ -131,7 +131,7 @@ namespace dlib
>
typename enable_if<dlib::impl::has_reject_labeling<feature_extractor>,bool>::type contains_invalid_labeling (
const feature_extractor& fe,
const std::vector<typename feature_extractor::sample_type>& x,
const typename feature_extractor::sequence_type& x,
const std::vector<unsigned long>& y
)
{
......@@ -160,7 +160,7 @@ namespace dlib
>
typename disable_if<dlib::impl::has_reject_labeling<feature_extractor>,bool>::type contains_invalid_labeling (
const feature_extractor& ,
const std::vector<typename feature_extractor::sample_type>& x,
const typename feature_extractor::sequence_type& x,
const std::vector<unsigned long>& y
)
{
......@@ -177,7 +177,7 @@ namespace dlib
>
bool contains_invalid_labeling (
const feature_extractor& fe,
const std::vector<std::vector<typename feature_extractor::sample_type> >& x,
const std::vector<typename feature_extractor::sequence_type>& x,
const std::vector<std::vector<unsigned long> >& y
)
{
......@@ -201,8 +201,7 @@ namespace dlib
class sequence_labeler
{
public:
typedef typename feature_extractor::sample_type sample_type;
typedef std::vector<sample_type> sample_sequence_type;
typedef typename feature_extractor::sequence_type sample_sequence_type;
typedef std::vector<unsigned long> labeled_sequence_type;
private:
......
......@@ -42,8 +42,9 @@ namespace dlib
!*/
public:
// This should be the type of elements in the input sequence
typedef the_type_of_elements_of_x sample_type;
// This should be the type used to represent an input sequence. It can be
// anything so long as it has a .size() which returns the length of the sequence.
typedef the_type_used_to_represent_a_sequence sequence_type;
example_feature_extractor (
);
......@@ -81,7 +82,7 @@ namespace dlib
template <typename EXP>
bool reject_labeling (
const std::vector<sample_type>& x,
const sequence_type& x,
const matrix_exp<EXP>& y,
unsigned long position
) const;
......@@ -110,7 +111,7 @@ namespace dlib
template <typename feature_setter, typename EXP>
void get_features (
feature_setter& set_feature,
const std::vector<sample_type>& x,
const sequence_type& x,
const matrix_exp<EXP>& y,
unsigned long position
) const;
......@@ -173,7 +174,7 @@ namespace dlib
>
bool contains_invalid_labeling (
const feature_extractor& fe,
const std::vector<typename feature_extractor::sample_type>& x,
const typename feature_extractor::sequence_type& x,
const std::vector<unsigned long>& y
);
/*!
......@@ -195,7 +196,7 @@ namespace dlib
>
bool contains_invalid_labeling (
const feature_extractor& fe,
const std::vector<std::vector<typename feature_extractor::sample_type> >& x,
const std::vector<typename feature_extractor::sequence_type>& x,
const std::vector<std::vector<unsigned long> >& y
);
/*!
......@@ -243,8 +244,7 @@ namespace dlib
!*/
public:
typedef typename feature_extractor::sample_type sample_type;
typedef std::vector<sample_type> sample_sequence_type;
typedef typename feature_extractor::sequence_type sample_sequence_type;
typedef std::vector<unsigned long> labeled_sequence_type;
sequence_labeler(
......
......@@ -20,8 +20,7 @@ namespace dlib
class structural_sequence_labeling_trainer
{
public:
typedef typename feature_extractor::sample_type sample_type;
typedef std::vector<sample_type> sample_sequence_type;
typedef typename feature_extractor::sequence_type sample_sequence_type;
typedef std::vector<unsigned long> labeled_sequence_type;
typedef sequence_labeler<feature_extractor> trained_function_type;
......
......@@ -36,8 +36,7 @@ namespace dlib
!*/
public:
typedef typename feature_extractor::sample_type sample_type;
typedef std::vector<sample_type> sample_sequence_type;
typedef typename feature_extractor::sequence_type sample_sequence_type;
typedef std::vector<unsigned long> labeled_sequence_type;
typedef sequence_labeler<feature_extractor> trained_function_type;
......
......@@ -44,11 +44,11 @@ namespace dlib
// ----------------------------------------------------------------------------------------
template <typename feature_extractor, typename sample_type, typename EXP2>
template <typename feature_extractor, typename sequence_type, typename EXP2>
void get_feature_vector(
std::vector<std::pair<unsigned long, double> >& feats,
const feature_extractor& fe,
const std::vector<sample_type>& sequence,
const sequence_type& sequence,
const matrix_exp<EXP2>& candidate_labeling,
unsigned long position
)
......@@ -71,10 +71,10 @@ namespace dlib
typedef matrix<double,0,1> matrix_type;
typedef std::vector<std::pair<unsigned long, double> > feature_vector_type;
typedef typename feature_extractor::sample_type sample_type;
typedef typename feature_extractor::sequence_type sequence_type;
structural_svm_sequence_labeling_problem(
const std::vector<std::vector<sample_type> >& samples_,
const std::vector<sequence_type>& samples_,
const std::vector<std::vector<unsigned long> >& labels_,
const feature_extractor& fe_,
unsigned long num_threads = 2
......@@ -130,7 +130,7 @@ namespace dlib
}
void get_joint_feature_vector (
const std::vector<sample_type>& sample,
const sequence_type& sample,
const std::vector<unsigned long>& label,
feature_vector_type& psi
) const
......@@ -163,7 +163,7 @@ namespace dlib
unsigned long num_states() const { return fe.num_labels(); }
map_prob(
const std::vector<sample_type>& sequence_,
const sequence_type& sequence_,
const std::vector<unsigned long>& label_,
const feature_extractor& fe_,
const matrix<double,0,1>& weights_
......@@ -199,7 +199,7 @@ namespace dlib
return fe_helpers::dot(weights, fe, sequence, node_states, node_id) + loss;
}
const std::vector<sample_type>& sequence;
const sequence_type& sequence;
const std::vector<unsigned long>& label;
const feature_extractor& fe;
const matrix<double,0,1>& weights;
......@@ -225,7 +225,7 @@ namespace dlib
get_joint_feature_vector(samples[idx], y, psi);
}
const std::vector<std::vector<sample_type> >& samples;
const std::vector<sequence_type>& samples;
const std::vector<std::vector<unsigned long> >& labels;
const feature_extractor& fe;
};
......
......@@ -40,10 +40,10 @@ namespace dlib
!*/
public:
typedef typename feature_extractor::sample_type sample_type;
typedef typename feature_extractor::sequence_type sequence_type;
structural_svm_sequence_labeling_problem(
const std::vector<std::vector<sample_type> >& samples,
const std::vector<sequence_type>& samples,
const std::vector<std::vector<unsigned long> >& labels,
const feature_extractor& fe,
unsigned long num_threads = 2
......
......@@ -178,10 +178,10 @@ namespace dlib
// ----------------------------------------------------------------------------------------
template <
typename sample_type
typename sequence_type
>
bool is_sequence_labeling_problem (
const std::vector<std::vector<sample_type> >& samples,
const std::vector<sequence_type>& samples,
const std::vector<std::vector<unsigned long> >& labels
)
{
......
......@@ -63,10 +63,10 @@ namespace dlib
!*/
template <
typename sample_type
typename sequence_type
>
bool is_sequence_labeling_problem (
const std::vector<std::vector<sample_type> >& samples,
const std::vector<sequence_type>& samples,
const std::vector<std::vector<unsigned long> >& labels
);
/*!
......
......@@ -24,12 +24,26 @@ namespace
const unsigned long num_label_states = 3; // the "hidden" states
const unsigned long num_sample_states = 3;
// ----------------------------------------------------------------------------------------
struct funny_sequence
{
std::vector<unsigned long> item;
unsigned long size() const { return item.size(); }
};
funny_sequence make_funny_sequence(const std::vector<unsigned long>& item)
{
funny_sequence temp;
temp.item = item;
return temp;
}
// ----------------------------------------------------------------------------------------
class feature_extractor
{
public:
typedef unsigned long sample_type;
typedef funny_sequence sequence_type;
unsigned long num_features() const
{
......@@ -49,7 +63,7 @@ namespace
template <typename feature_setter, typename EXP>
void get_features (
feature_setter& set_feature,
const std::vector<sample_type>& x,
const sequence_type& x,
const matrix_exp<EXP>& y,
unsigned long position
) const
......@@ -58,14 +72,14 @@ namespace
set_feature(y(1)*num_label_states + y(0));
set_feature(num_label_states*num_label_states +
y(0)*num_sample_states + x[position]);
y(0)*num_sample_states + x.item[position]);
}
};
class feature_extractor_partial
{
public:
typedef unsigned long sample_type;
typedef funny_sequence sequence_type;
unsigned long num_features() const
{
......@@ -85,7 +99,7 @@ namespace
template <typename feature_setter, typename EXP>
void get_features (
feature_setter& set_feature,
const std::vector<sample_type>& x,
const sequence_type& x,
const matrix_exp<EXP>& y,
unsigned long position
) const
......@@ -97,9 +111,9 @@ namespace
}
set_feature(num_label_states*num_label_states +
y(0)*num_sample_states + x[position],0.4);
y(0)*num_sample_states + x.item[position],0.4);
set_feature(num_label_states*num_label_states +
y(0)*num_sample_states + x[position],0.6);
y(0)*num_sample_states + x.item[position],0.6);
}
};
......@@ -107,7 +121,7 @@ namespace
class feature_extractor2
{
public:
typedef unsigned long sample_type;
typedef funny_sequence sequence_type;
unsigned long num_features() const
{
......@@ -126,7 +140,7 @@ namespace
template <typename EXP>
bool reject_labeling (
const std::vector<sample_type>& ,
const sequence_type& ,
const matrix_exp<EXP>& ,
unsigned long
) const
......@@ -138,7 +152,7 @@ namespace
template <typename feature_setter, typename EXP>
void get_features (
feature_setter& set_feature,
const std::vector<sample_type>& x,
const sequence_type& x,
const matrix_exp<EXP>& y,
unsigned long position
) const
......@@ -147,7 +161,7 @@ namespace
set_feature(y(1)*num_label_states + y(0));
set_feature(num_label_states*num_label_states +
y(0)*num_sample_states + x[position]);
y(0)*num_sample_states + x.item[position]);
}
};
......@@ -207,7 +221,7 @@ namespace
void make_dataset (
const matrix<double>& transition_probabilities,
const matrix<double>& emission_probabilities,
std::vector<std::vector<unsigned long> >& samples,
std::vector<funny_sequence>& samples,
std::vector<std::vector<unsigned long> >& labels,
unsigned long dataset_size
)
......@@ -258,7 +272,7 @@ namespace
previous_label = next_label;
}
samples.push_back(sample);
samples.push_back(make_funny_sequence(sample));
labels.push_back(label);
}
}
......@@ -283,7 +297,7 @@ namespace
print_spinner();
std::vector<std::vector<unsigned long> > samples;
std::vector<funny_sequence> samples;
std::vector<std::vector<unsigned long> > labels;
make_dataset(transition_probabilities,emission_probabilities,
samples, labels, 1000);
......@@ -294,7 +308,7 @@ namespace
for (int i = 0; i < 10; ++i)
{
dlog << LINFO << "hidden states: " << trans(vector_to_matrix(labels[i]));
dlog << LINFO << "observed states: " << trans(vector_to_matrix(samples[i]));
dlog << LINFO << "observed states: " << trans(vector_to_matrix(samples[i].item));
dlog << LINFO << "******************************";
}
......@@ -377,7 +391,7 @@ namespace
*/
print_spinner();
std::vector<std::vector<unsigned long> > samples;
std::vector<funny_sequence> samples;
std::vector<std::vector<unsigned long> > labels;
matrix<double> transition_probabilities(num_label_states, num_label_states);
......
......@@ -67,9 +67,10 @@ class feature_extractor
*/
public:
// This defines the type used to represent the elements of an observed
// sequence. You can use any type here.
typedef unsigned long sample_type;
// This defines the type used to represent the observed sequence. You can use
// any type here so long as it has a .size() which returns the number of things
// in the sequence.
typedef std::vector<unsigned long> sequence_type;
unsigned long num_features() const
/*!
......@@ -111,7 +112,7 @@ public:
template <typename feature_setter, typename EXP>
void get_features (
feature_setter& set_feature,
const std::vector<sample_type>& x,
const sequence_type& x,
const matrix_exp<EXP>& y,
unsigned long position
) const
......
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