<s> """<STR_LIT>""" <EOL> import logging <EOL> import numpy <EOL> from theano . compat . six . moves import xrange <EOL> from pylearn2 . blocks import Block <EOL> from pylearn2 . models . model import Model <EOL> from pylearn2 . space import VectorSpace <EOL> from pylearn2 . utils import sharedX <EOL> from pylearn2 . utils . mem import improve_memory_error_message <EOL> from pylearn2 . utils import wraps <EOL> from pylearn2 . utils import contains_nan <EOL> import warnings <EOL> try : <EOL> import milk <EOL> except ImportError : <EOL> milk = None <EOL> warnings . warn ( """<STR_LIT>""" ) <EOL> logger = logging . getLogger ( __name__ ) <EOL> class KMeans ( Block , Model ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , k , nvis , convergence_th = <NUM_LIT> , max_iter = None , <EOL> verbose = False ) : <EOL> Block . __init__ ( self ) <EOL> Model . __init__ ( self ) <EOL> self . input_space = VectorSpace ( nvis ) <EOL> self . k = k <EOL> self . mu = None <EOL> self . convergence_th = convergence_th <EOL> if max_iter : <EOL> if max_iter < <NUM_LIT:0> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . max_iter = max_iter <EOL> else : <EOL> self . max_iter = float ( '<STR_LIT>' ) <EOL> self . verbose = verbose <EOL> def train_all ( self , dataset , mu = None ) : <EOL> """<STR_LIT>""" <EOL> X = dataset . get_design_matrix ( ) <EOL> n , m = X . shape <EOL> k = self . k <EOL> if milk is not None : <EOL> cluster_ids , mu = milk . kmeans ( X , k ) <EOL> else : <EOL> if mu is not None : <EOL> if not len ( mu ) == k : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( len ( mu ) , k ) ) <EOL> else : <EOL> indices = numpy . random . randint ( X . shape [ <NUM_LIT:0> ] , size = k ) <EOL> mu = X [ indices ] <EOL> try : <EOL> dists = numpy . zeros ( ( n , k ) ) <EOL> except MemoryError as e : <EOL> improve_memory_error_message ( e , "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( n , k ) ) <EOL> old_kills = { } <EOL> iter = <NUM_LIT:0> <EOL> mmd = prev_mmd = float ( '<STR_LIT>' ) <EOL> while True : <EOL> if self . verbose : <EOL> logger . info ( '<STR_LIT>' . format ( iter ) ) <EOL> if contains_nan ( mu ) : <EOL> logger . info ( '<STR_LIT>' ) <EOL> return X <EOL> for i in xrange ( k ) : <EOL> dists [ : , i ] = numpy . square ( ( X - mu [ i , : ] ) ) . sum ( axis = <NUM_LIT:1> ) <EOL> if iter > <NUM_LIT:0> : <EOL> prev_mmd = mmd <EOL> min_dists = dists . min ( axis = <NUM_LIT:1> ) <EOL> mmd = min_dists . mean ( ) <EOL> logger . info ( '<STR_LIT>' . format ( mmd ) ) <EOL> if iter > <NUM_LIT:0> and ( iter >= self . max_iter or <EOL> abs ( mmd - prev_mmd ) < self . convergence_th ) : <EOL> break <EOL> min_dist_inds = dists . argmin ( axis = <NUM_LIT:1> ) <EOL> i = <NUM_LIT:0> <EOL> blacklist = [ ] <EOL> new_kills = { } <EOL> while i < k : <EOL> b = min_dist_inds == i <EOL> if not numpy . any ( b ) : <EOL> killed_on_prev_iter = True <EOL> if i in old_kills : <EOL> d = old_kills [ i ] - <NUM_LIT:1> <EOL> if d == <NUM_LIT:0> : <EOL> d = <NUM_LIT:50> <EOL> new_kills [ i ] = d <EOL> else : <EOL> d = <NUM_LIT:5> <EOL> mu [ i , : ] = <NUM_LIT:0> <EOL> for j in xrange ( d ) : <EOL> idx = numpy . argmax ( min_dists ) <EOL> min_dists [ idx ] = <NUM_LIT:0> <EOL> mu [ i , : ] += X [ idx , : ] <EOL> blacklist . append ( idx ) <EOL> mu [ i , : ] /= float ( d ) <EOL> dists [ : , i ] = numpy . square ( ( X - mu [ i , : ] ) ) . sum ( axis = <NUM_LIT:1> ) <EOL> min_dists = dists . min ( axis = <NUM_LIT:1> ) <EOL> for idx in blacklist : <EOL> min_dists [ idx ] = <NUM_LIT:0> <EOL> min_dist_inds = dists . argmin ( axis = <NUM_LIT:1> ) <EOL> i += <NUM_LIT:1> <EOL> else : <EOL> mu [ i , : ] = numpy . mean ( X [ b , : ] , axis = <NUM_LIT:0> ) <EOL> if contains_nan ( mu ) : <EOL> logger . info ( '<STR_LIT>' . format ( i ) ) <EOL> return X <EOL> i += <NUM_LIT:1> <EOL> old_kills = new_kills <EOL> iter += <NUM_LIT:1> <EOL> self . mu = sharedX ( mu ) <EOL> self . _params = [ self . mu ] <EOL> @ wraps ( Model . continue_learning ) <EOL> def continue_learning ( self ) : <EOL> return False <EOL> def get_params ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self . mu , '<STR_LIT>' ) : <EOL> self . mu = sharedX ( self . mu ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _params = [ self . mu ] <EOL> return [ param for param in self . _params ] <EOL> def __call__ ( self , X ) : <EOL> """<STR_LIT>""" <EOL> n , m = X . shape <EOL> k = self . k <EOL> mu = self . mu <EOL> dists = numpy . zeros ( ( n , k ) ) <EOL> for i in xrange ( k ) : <EOL> dists [ : , i ] = numpy . square ( ( X - mu [ i , : ] ) ) . sum ( axis = <NUM_LIT:1> ) <EOL> return dists / dists . sum ( axis = <NUM_LIT:1> ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def get_weights ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . mu <EOL> def get_weights_format ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT:h>' , '<STR_LIT:v>' ] <EOL> get_input_space = Model . get_input_space <EOL> get_output_space = Model . get_output_space </s>
