You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mahout.apache.org by Dan Filimon <da...@gmail.com> on 2012/10/24 11:32:41 UTC
Streaming k-means as a MapReduce
Hi everyone,
After talking to Ted last week the next step in getting the new
streaming k-means implementation in Mahout is writing a MapReduce
version of the algorithm. I'm writing this e-mail mostly to Ted, but
everyone is more than welcome to provide feedback! :)
Since this is my first time writing a MapReduce using Hadoop, I looked
at the existing clustering code for a rough guide.
The current k-means code runs multiple mapreductions (one per iteration), where:
- mappers perform the classification, writing (index, cluster) pairs
for each input vector (the clusters accumulate the values of the
vectors that are assigned to them)
- reducers perform the new center estimation writing a new cluster for
each (index, [cluster1, cluster2, ...]) pair by accumulating all the
observations.
The new streaming k-means would only be 1 MapReduce in which:
- The points are distributed to the mappers as VectorWritables and
these would compute k clusters using streaming k-means and output k
Centroids (weighted vectors containing the cluster's center and the
number of points it contains). This would happen during the cleanup()
call at the end of a Map task.
- All of these (k * #Mappers) centroids are treated as a set of
weighted points that need to be reduced by the same reducer since we
want just k clusters at the end.
So, the keys output by the mappers should have the same integer value
(say, 0). The output of the Reduce task (there will only be one) is
(index, centroid) pairs, one for each of the final k clusters.
So, the generics' types would be:
- Mapper<WritableComparable<?>, VectorWritable, IntWritable, CentroidWritable>
- Reducer<IntWritable, CentroidWritable, IntWritable, CentroidWritable>
CentroidWritable is just a class wrapping around a Centroid
implementing Writable.
Since the same clustering algorithm would be used for both the mapper
and the reducer, it makes sense to share the code.
The current streaming k-means implementation gets an iterable
(cluster(Iterable<MatrixSlice...)) to the vectors and doesn't seem to
provide any way of adding just one point.
I thought new points could be added by using the UpdatableSearcher
object returned by clusterInternal, but the public API only gives back
a Searcher so, the current version doesn't seem to be designed for
this use case. Additionally, clustering is performed by passing in
SearchFactory's not possibly updated searchers.
So, this should be changed so a vector can be added directly
(refactor: basically move the inner loop from
StreamingKmeans.java:clusterInternal to another method).
So, the way I'm thinking of doing it is:
1. refactor (and prettify?) the existing code to support adding new
points directly (both weighted and unweighted) and update the tests;
2. write the wrapper CentroidWritable class and the MapReduce itself
using the new API from (1).
I also know that our goal is to integrate the code into Mahout so
maybe it might make some sense to pass around Clusters rather than
Centroids? While this makes sense, I feel that for now, not hooking
into the more complex clustering API for this first prototype would be
more straightforward.
Thoughts?
Re: Streaming k-means as a MapReduce
Posted by Ted Dunning <te...@gmail.com>.
On Wed, Oct 24, 2012 at 2:32 AM, Dan Filimon <da...@gmail.com>wrote:
> So, the way I'm thinking of doing it is:
> 1. refactor (and prettify?) the existing code to support adding new
> points directly (both weighted and unweighted) and update the tests;
> 2. write the wrapper CentroidWritable class and the MapReduce itself
> using the new API from (1).
>
Yes. These sound good.
> I also know that our goal is to integrate the code into Mahout so
> maybe it might make some sense to pass around Clusters rather than
> Centroids? While this makes sense, I feel that for now, not hooking
> into the more complex clustering API for this first prototype would be
> more straightforward.
>
This also sounds very good. Working and simple first. Fully integrated
and compatible after.
(sorry to be terse, I am time crunched today)