Commit 66a3e292 authored by Davis King's avatar Davis King

Added an additional stopping condition to the kkmeans object. It is now

possible to tell it to top when a certain fraction of centers don't change.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402482
parent de24dbb0
...@@ -31,7 +31,8 @@ namespace dlib ...@@ -31,7 +31,8 @@ namespace dlib
kkmeans ( kkmeans (
const kcentroid<kernel_type>& kc_ const kcentroid<kernel_type>& kc_
): ):
kc(kc_) kc(kc_),
min_change(0.01)
{ {
set_number_of_centers(1); set_number_of_centers(1);
} }
...@@ -127,6 +128,19 @@ namespace dlib ...@@ -127,6 +128,19 @@ namespace dlib
return label; return label;
} }
void set_min_change (
scalar_type min_change_
)
{
min_change = min_change_;
}
const scalar_type get_min_change (
) const
{
return min_change;
}
void swap ( void swap (
kkmeans& item kkmeans& item
) )
...@@ -134,6 +148,7 @@ namespace dlib ...@@ -134,6 +148,7 @@ namespace dlib
centers.swap(item.centers); centers.swap(item.centers);
kc.swap(item.kc); kc.swap(item.kc);
assignments.swap(item.assignments); assignments.swap(item.assignments);
exchange(min_change, item.min_change);
} }
friend void serialize(const kkmeans& item, std::ostream& out) friend void serialize(const kkmeans& item, std::ostream& out)
...@@ -144,7 +159,7 @@ namespace dlib ...@@ -144,7 +159,7 @@ namespace dlib
serialize(*item.centers[i], out); serialize(*item.centers[i], out);
} }
serialize(item.kc, out); serialize(item.kc, out);
serialize(item.assignments, out); serialize(item.min_change, out);
} }
friend void deserialize(kkmeans& item, std::istream& in) friend void deserialize(kkmeans& item, std::istream& in)
...@@ -160,7 +175,7 @@ namespace dlib ...@@ -160,7 +175,7 @@ namespace dlib
} }
deserialize(item.kc, in); deserialize(item.kc, in);
deserialize(item.assignments, in); deserialize(item.min_change, in);
} }
private: private:
...@@ -246,6 +261,7 @@ namespace dlib ...@@ -246,6 +261,7 @@ namespace dlib
typename array<scoped_ptr<kcentroid<kernel_type> > >::expand_1b_c centers; typename array<scoped_ptr<kcentroid<kernel_type> > >::expand_1b_c centers;
kcentroid<kernel_type> kc; kcentroid<kernel_type> kc;
scalar_type min_change;
// temp variables // temp variables
array<unsigned long>::expand_1b_c assignments; array<unsigned long>::expand_1b_c assignments;
......
...@@ -25,6 +25,7 @@ namespace dlib ...@@ -25,6 +25,7 @@ namespace dlib
INITIAL VALUE INITIAL VALUE
- number_of_centers() == 1 - number_of_centers() == 1
- get_min_change() == 0.01
WHAT THIS OBJECT REPRESENTS WHAT THIS OBJECT REPRESENTS
This is an implementation of a kernelized k-means clustering algorithm. This is an implementation of a kernelized k-means clustering algorithm.
...@@ -42,6 +43,7 @@ namespace dlib ...@@ -42,6 +43,7 @@ namespace dlib
/*! /*!
ensures ensures
- #number_of_centers() == 1 - #number_of_centers() == 1
- #get_min_change() == 0.01
- #get_kcentroid(0) == a copy of kc_ - #get_kcentroid(0) == a copy of kc_
!*/ !*/
...@@ -105,7 +107,7 @@ namespace dlib ...@@ -105,7 +107,7 @@ namespace dlib
void train ( void train (
const matrix_type& samples, const matrix_type& samples,
const matrix_type2& initial_centers, const matrix_type2& initial_centers,
long max_iter = 1000000 long max_iter = 1000
); );
/*! /*!
requires requires
...@@ -119,8 +121,9 @@ namespace dlib ...@@ -119,8 +121,9 @@ namespace dlib
ensures ensures
- performs k-means clustering of the given set of samples. The initial center points - performs k-means clustering of the given set of samples. The initial center points
are taken from the initial_centers argument. are taken from the initial_centers argument.
- loops over the data and continues to refine the clustering until either the cluster centers - loops over the data and continues to refine the clustering until either less than
don't move or we have done max_iter iterations over the data. get_min_change() fraction of the cluster centers move or we have done max_iter iterations
over the data.
- After this function finishes you can call the operator() function below - After this function finishes you can call the operator() function below
to determine which centroid a given sample is closest to. to determine which centroid a given sample is closest to.
!*/ !*/
...@@ -136,6 +139,24 @@ namespace dlib ...@@ -136,6 +139,24 @@ namespace dlib
sample. sample.
!*/ !*/
void set_min_change (
scalar_type min_change
);
/*!
requires
- 0 <= min_change < 1
ensures
- #get_min_change() == min_change
!*/
const scalar_type get_min_change (
) const;
/*!
ensures
- returns the minimum fraction of centers that need to change
in an iteration of kmeans for the algorithm to keep going.
!*/
void swap ( void swap (
kkmeans& item kkmeans& item
); );
......
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