<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import multiprocessing as mp <EOL> from time import time <EOL> from . modules import make_hdf5 , _ireader , _iwriter , _prepare_fHH , _write_fHH <EOL> from tables import open_file <EOL> from . elm import ELM <EOL> class HPELM ( ELM ) : <EOL> """<STR_LIT>""" <EOL> def train ( self , fX , fT , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> X , T = self . _checkdata ( fX , fT ) <EOL> self . _train_parse_args ( args , kwargs ) <EOL> istart = <NUM_LIT:0> <EOL> icount = np . inf <EOL> if "<STR_LIT>" in kwargs . keys ( ) : <EOL> istart = max ( <NUM_LIT:0> , int ( kwargs [ "<STR_LIT>" ] ) ) <EOL> if "<STR_LIT>" in kwargs . keys ( ) : <EOL> icount = kwargs [ "<STR_LIT>" ] <EOL> self . add_data ( X , T , istart = istart , icount = icount ) <EOL> self . nnet . solve ( ) <EOL> def add_data ( self , fX , fT , istart = <NUM_LIT:0> , icount = np . inf , fHH = None , fHT = None ) : <EOL> """<STR_LIT>""" <EOL> assert len ( self . nnet . neurons ) > <NUM_LIT:0> , "<STR_LIT>" <EOL> X , T = self . _checkdata ( fX , fT ) <EOL> N = X . shape [ <NUM_LIT:0> ] <EOL> _prepare_fHH ( fHH , fHT , self . nnet , self . precision ) <EOL> icount = min ( icount , N - istart ) <EOL> nb = int ( np . ceil ( float ( icount ) / self . batch ) ) <EOL> if self . classification == "<STR_LIT>" and self . wc is None : <EOL> ns = np . zeros ( ( self . nnet . outputs , ) ) <EOL> for b in xrange ( nb ) : <EOL> start = b * self . batch + istart <EOL> stop = min ( ( b + <NUM_LIT:1> ) * self . batch + istart , icount + istart ) <EOL> ns += T [ start : stop ] . sum ( axis = <NUM_LIT:0> ) <EOL> ns = ns . astype ( self . precision ) <EOL> self . wc = ns . sum ( ) / ns <EOL> t = time ( ) <EOL> t0 = time ( ) <EOL> eta = <NUM_LIT:0> <EOL> wc_vector = None <EOL> for b in xrange ( nb ) : <EOL> start = b * self . batch + istart <EOL> stop = min ( ( b + <NUM_LIT:1> ) * self . batch + istart , icount + istart ) <EOL> Xb = X [ start : stop ] <EOL> Tb = T [ start : stop ] <EOL> if self . classification == "<STR_LIT>" : <EOL> wc_vector = self . wc [ np . where ( Tb == <NUM_LIT:1> ) [ <NUM_LIT:1> ] ] <EOL> self . nnet . add_batch ( Xb , Tb , wc_vector ) <EOL> eta = int ( ( ( time ( ) - t0 ) / ( b + <NUM_LIT:1> ) ) * ( nb - b - <NUM_LIT:1> ) ) <EOL> if time ( ) - t > self . tprint : <EOL> print ( "<STR_LIT>" % ( b + <NUM_LIT:1> , nb , eta / <NUM_LIT> , ( eta % <NUM_LIT> ) / <NUM_LIT> , eta % <NUM_LIT> ) ) <EOL> t = time ( ) <EOL> if fHH is not None and fHT is not None : <EOL> HH , HT = self . nnet . get_corr ( ) <EOL> HH [ np . diag_indices_from ( HH ) ] -= self . nnet . norm <EOL> _write_fHH ( fHH , fHT , HH , HT ) <EOL> def solve_corr ( self , fHH , fHT ) : <EOL> """<STR_LIT>""" <EOL> HH , HT = self . _checkcorr ( fHH , fHT ) <EOL> B = self . nnet . solve_corr ( HH , HT ) <EOL> self . nnet . set_B ( B ) <EOL> def predict ( self , fX , fY = None , istart = <NUM_LIT:0> , icount = np . inf ) : <EOL> """<STR_LIT>""" <EOL> assert len ( self . nnet . neurons ) > <NUM_LIT:0> , "<STR_LIT>" <EOL> assert self . nnet . B is not None , "<STR_LIT>" <EOL> X , _ = self . _checkdata ( fX , None ) <EOL> N = X . shape [ <NUM_LIT:0> ] <EOL> icount = min ( icount , N - istart ) <EOL> nb = int ( np . ceil ( float ( icount ) / self . batch ) ) <EOL> if isinstance ( fY , basestring ) : <EOL> make_hdf5 ( ( icount , self . nnet . outputs ) , fY , dtype = self . precision ) <EOL> h5 = open_file ( fY , "<STR_LIT:a>" ) <EOL> for Y in h5 . walk_nodes ( ) : <EOL> pass <EOL> elif fY is None : <EOL> Y = np . zeros ( ( icount , self . nnet . outputs ) , dtype = self . precision ) <EOL> else : <EOL> _ , Y = self . _checkdata ( None , fY ) <EOL> t = time ( ) <EOL> t0 = time ( ) <EOL> eta = <NUM_LIT:0> <EOL> for b in xrange ( <NUM_LIT:0> , nb ) : <EOL> start = b * self . batch + istart <EOL> stop = min ( ( b + <NUM_LIT:1> ) * self . batch + istart , icount + istart ) <EOL> Xb = X [ start : stop ] <EOL> Yb = self . nnet . _predict ( Xb ) <EOL> Y [ start - istart : stop - istart ] = Yb <EOL> eta = int ( ( ( time ( ) - t0 ) / ( b + <NUM_LIT:1> ) ) * ( nb - b - <NUM_LIT:1> ) ) <EOL> if time ( ) - t > self . tprint : <EOL> print ( "<STR_LIT>" % ( b + <NUM_LIT:1> , nb , eta / <NUM_LIT> , ( eta % <NUM_LIT> ) / <NUM_LIT> , eta % <NUM_LIT> ) ) <EOL> t = time ( ) <EOL> if isinstance ( fY , basestring ) : <EOL> h5 . flush ( ) <EOL> h5 . close ( ) <EOL> elif fY is None : <EOL> return Y <EOL> def project ( self , fX , fH = None , istart = <NUM_LIT:0> , icount = np . inf ) : <EOL> """<STR_LIT>""" <EOL> assert len ( self . nnet . neurons ) > <NUM_LIT:0> , "<STR_LIT>" <EOL> X , _ = self . _checkdata ( fX , None ) <EOL> N = X . shape [ <NUM_LIT:0> ] <EOL> icount = min ( icount , N - istart ) <EOL> nb = int ( np . ceil ( float ( icount ) / self . batch ) ) <EOL> if isinstance ( fH , basestring ) : <EOL> make_hdf5 ( ( icount , self . nnet . L ) , fH , dtype = self . precision ) <EOL> h5 = open_file ( fH , "<STR_LIT:a>" ) <EOL> for H in h5 . walk_nodes ( ) : <EOL> pass <EOL> elif fH is None : <EOL> H = np . zeros ( ( icount , self . nnet . L ) , dtype = self . precision ) <EOL> else : <EOL> assert fH . shape [ <NUM_LIT:0> ] >= icount , "<STR_LIT>" <EOL> assert fH . shape [ <NUM_LIT:1> ] == self . nnet . L , "<STR_LIT>" <EOL> H = fH <EOL> t = time ( ) <EOL> t0 = time ( ) <EOL> for b in xrange ( <NUM_LIT:0> , nb ) : <EOL> start = b * self . batch + istart <EOL> stop = min ( ( b + <NUM_LIT:1> ) * self . batch + istart , icount + istart ) <EOL> Xb = X [ start : stop ] <EOL> Hb = self . nnet . _project ( Xb ) <EOL> H [ start - start : stop - istart ] = Hb <EOL> eta = int ( ( ( time ( ) - t0 ) / ( b + <NUM_LIT:1> ) ) * ( nb - b - <NUM_LIT:1> ) ) <EOL> if time ( ) - t > self . tprint : <EOL> print ( "<STR_LIT>" % ( b + <NUM_LIT:1> , nb , eta / <NUM_LIT> , ( eta % <NUM_LIT> ) / <NUM_LIT> , eta % <NUM_LIT> ) ) <EOL> t = time ( ) <EOL> if isinstance ( fH , basestring ) : <EOL> h5 . flush ( ) <EOL> h5 . close ( ) <EOL> elif fH is None : <EOL> return H <EOL> def error ( self , fT , fY , istart = <NUM_LIT:0> , icount = np . inf ) : <EOL> """<STR_LIT>""" <EOL> _ , T = self . _checkdata ( None , fT ) <EOL> _ , Y = self . _checkdata ( None , fY ) <EOL> return self . _error ( T , Y , istart = istart , icount = icount ) <EOL> def _error ( self , T , Y , istart = <NUM_LIT:0> , icount = np . inf ) : <EOL> """<STR_LIT>""" <EOL> N = T . shape [ <NUM_LIT:0> ] <EOL> icount = min ( icount , N - istart ) <EOL> nb = int ( np . ceil ( float ( icount ) / self . batch ) ) <EOL> if self . classification == "<STR_LIT:c>" : <EOL> err = <NUM_LIT:0> <EOL> for b in xrange ( nb ) : <EOL> start = b * self . batch + istart <EOL> stop = min ( ( b + <NUM_LIT:1> ) * self . batch + istart , icount + istart ) <EOL> Tb = np . array ( T [ start : stop ] ) <EOL> Yb = np . array ( Y [ start : stop ] ) <EOL> errb = np . mean ( Yb . argmax ( <NUM_LIT:1> ) != Tb . argmax ( <NUM_LIT:1> ) ) <EOL> err += errb * float ( stop - start ) / icount <EOL> elif self . classification == "<STR_LIT>" : <EOL> c = T . shape [ <NUM_LIT:1> ] <EOL> errc = np . zeros ( c ) <EOL> countc = np . zeros ( c ) <EOL> for b in xrange ( nb ) : <EOL> start = b * self . batch + istart <EOL> stop = min ( ( b + <NUM_LIT:1> ) * self . batch + istart , icount + istart ) <EOL> Tb = np . array ( T [ start : stop ] ) <EOL> Yb = np . array ( Y [ start : stop ] ) <EOL> for i in xrange ( c ) : <EOL> idx = np . where ( Tb [ : , i ] == <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if len ( idx ) > <NUM_LIT:0> : <EOL> err1 = np . not_equal ( Yb [ idx ] . argmax ( <NUM_LIT:1> ) , i ) <EOL> errc [ i ] += err1 . sum ( ) <EOL> countc [ i ] += len ( idx ) <EOL> errc = errc / countc <EOL> err = np . sum ( errc * self . wc ) / np . sum ( self . wc ) <EOL> elif self . classification == "<STR_LIT>" : <EOL> err = <NUM_LIT:0> <EOL> for b in xrange ( nb ) : <EOL> start = b * self . batch + istart <EOL> stop = min ( ( b + <NUM_LIT:1> ) * self . batch + istart , icount + istart ) <EOL> Tb = np . array ( T [ start : stop ] ) <EOL> Yb = np . array ( Y [ start : stop ] ) <EOL> errb = np . not_equal ( Yb > <NUM_LIT:0.5> , Tb > <NUM_LIT:0.5> ) . mean ( ) <EOL> err += errb * float ( stop - start ) / icount <EOL> else : <EOL> err = <NUM_LIT:0> <EOL> for b in xrange ( nb ) : <EOL> start = b * self . batch + istart <EOL> stop = min ( ( b + <NUM_LIT:1> ) * self . batch + istart , icount + istart ) <EOL> Tb = T [ start : stop ] <EOL> Yb = Y [ start : stop ] <EOL> errb = np . mean ( ( Tb - Yb ) ** <NUM_LIT:2> ) <EOL> err += errb * float ( stop - start ) / icount <EOL> return err <EOL> def validation_corr ( self , fHH , fHT , fXv , fTv , steps = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> X , T = self . _checkdata ( fXv , fTv ) <EOL> HH , HT = self . _checkcorr ( fHH , fHT ) <EOL> N = X . shape [ <NUM_LIT:0> ] <EOL> L = self . nnet . L <EOL> classification = self . classification is not None <EOL> Ls = np . logspace ( np . log ( <NUM_LIT:3> ) , np . log ( L ) , steps , base = np . e , endpoint = True ) <EOL> Ls = np . ceil ( Ls ) . astype ( np . int ) <EOL> Ls = np . unique ( Ls ) <EOL> k = Ls . shape [ <NUM_LIT:0> ] <EOL> errs = np . zeros ( ( k , ) ) <EOL> nb = int ( np . ceil ( float ( N ) / self . batch ) ) <EOL> Betas = [ ] <EOL> confs = [ ] <EOL> for l in Ls : <EOL> Betas . append ( self . nnet . solve_corr ( HH [ : l , : l ] , HT [ : l , : ] ) ) <EOL> if classification : confs . append ( np . zeros ( ( self . nnet . outputs , self . nnet . outputs ) ) ) <EOL> t = time ( ) <EOL> t0 = time ( ) <EOL> for b in xrange ( nb ) : <EOL> start = b * self . batch <EOL> stop = min ( ( b + <NUM_LIT:1> ) * self . batch , N ) <EOL> Tb = np . array ( T [ start : stop ] ) <EOL> Xb = np . array ( X [ start : stop ] ) <EOL> Hb = self . nnet . _project ( Xb ) <EOL> for i in xrange ( k ) : <EOL> hb1 = Hb [ : , : Ls [ i ] ] <EOL> Yb = np . dot ( hb1 , Betas [ i ] ) <EOL> errs [ i ] += self . _error ( Tb , Yb ) * float ( stop - start ) / N <EOL> if classification : confs [ i ] += self . confusion ( Tb , Yb ) <EOL> eta = int ( ( ( time ( ) - t0 ) / ( b + <NUM_LIT:1> ) ) * ( nb - b - <NUM_LIT:1> ) ) <EOL> if time ( ) - t > self . tprint : <EOL> print ( "<STR_LIT>" % ( b + <NUM_LIT:1> , nb , eta / <NUM_LIT> , ( eta % <NUM_LIT> ) / <NUM_LIT> , eta % <NUM_LIT> ) ) <EOL> t = time ( ) <EOL> k_opt = np . argmin ( errs ) <EOL> best_L = Ls [ k_opt ] <EOL> self . nnet . _prune ( np . arange ( best_L ) ) <EOL> self . nnet . set_B ( Betas [ k_opt ] ) <EOL> del Betas <EOL> print ( "<STR_LIT>" % ( best_L , L ) ) <EOL> if best_L > L * <NUM_LIT> : <EOL> print ( "<STR_LIT>" ) <EOL> return Ls , errs , confs <EOL> def train_async ( self , fX , fT , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> X , T = self . _checkdata ( fX , fT ) <EOL> self . _train_parse_args ( args , kwargs ) <EOL> istart = <NUM_LIT:0> <EOL> icount = np . inf <EOL> if "<STR_LIT>" in kwargs . keys ( ) : <EOL> istart = max ( <NUM_LIT:0> , int ( kwargs [ "<STR_LIT>" ] ) ) <EOL> if "<STR_LIT>" in kwargs . keys ( ) : <EOL> icount = kwargs [ "<STR_LIT>" ] <EOL> self . add_data_async ( fX , fT , istart = istart , icount = icount ) <EOL> self . nnet . solve ( ) <EOL> def add_data_async ( self , fX , fT , istart = <NUM_LIT:0> , icount = np . inf , fHH = None , fHT = None ) : <EOL> """<STR_LIT>""" <EOL> assert len ( self . nnet . neurons ) > <NUM_LIT:0> , "<STR_LIT>" <EOL> X , T = self . _checkdata ( fX , fT ) <EOL> N = X . shape [ <NUM_LIT:0> ] <EOL> _prepare_fHH ( fHH , fHT , self . nnet , self . precision ) <EOL> icount = min ( icount , N - istart ) <EOL> nb = int ( np . ceil ( float ( icount ) / self . batch ) ) <EOL> if self . classification == "<STR_LIT>" and self . wc is None : <EOL> ns = np . zeros ( ( self . nnet . outputs , ) ) <EOL> for b in xrange ( nb ) : <EOL> start = b * self . batch + istart <EOL> stop = min ( ( b + <NUM_LIT:1> ) * self . batch + istart , icount + istart ) <EOL> ns += T [ start : stop ] . sum ( axis = <NUM_LIT:0> ) <EOL> ns = ns . astype ( self . precision ) <EOL> self . wc = ns . sum ( ) / ns <EOL> h5 = self . opened_hdf5 . pop ( ) <EOL> h5 . close ( ) <EOL> h5 = self . opened_hdf5 . pop ( ) <EOL> h5 . close ( ) <EOL> qX_in = mp . Queue ( ) <EOL> qX_out = mp . Queue ( <NUM_LIT:1> ) <EOL> readerX = mp . Process ( target = _ireader , args = ( fX , qX_in , qX_out ) ) <EOL> readerX . daemon = True <EOL> readerX . start ( ) <EOL> qT_in = mp . Queue ( ) <EOL> qT_out = mp . Queue ( <NUM_LIT:1> ) <EOL> readerT = mp . Process ( target = _ireader , args = ( fT , qT_in , qT_out ) ) <EOL> readerT . daemon = True <EOL> readerT . start ( ) <EOL> t = time ( ) <EOL> t0 = time ( ) <EOL> eta = <NUM_LIT:0> <EOL> wc_vector = None <EOL> for b in xrange ( <NUM_LIT:0> , nb + <NUM_LIT:1> ) : <EOL> start_next = b * self . batch + istart <EOL> stop_next = min ( ( b + <NUM_LIT:1> ) * self . batch + istart , icount + istart ) <EOL> qX_in . put ( ( start_next , stop_next ) ) <EOL> qT_in . put ( ( start_next , stop_next ) ) <EOL> if b > <NUM_LIT:0> : <EOL> Xb = qX_out . get ( ) <EOL> Tb = qT_out . get ( ) <EOL> if self . classification == "<STR_LIT>" : <EOL> wc_vector = self . wc [ np . where ( Tb == <NUM_LIT:1> ) [ <NUM_LIT:1> ] ] <EOL> self . nnet . add_batch ( Xb , Tb , wc_vector ) <EOL> eta = int ( ( ( time ( ) - t0 ) / ( b + <NUM_LIT:1> ) ) * ( nb - b - <NUM_LIT:1> ) ) <EOL> if time ( ) - t > self . tprint : <EOL> print ( "<STR_LIT>" % ( b + <NUM_LIT:1> , nb , eta / <NUM_LIT> , ( eta % <NUM_LIT> ) / <NUM_LIT> , eta % <NUM_LIT> ) ) <EOL> t = time ( ) <EOL> readerX . join ( ) <EOL> readerT . join ( ) <EOL> if fHH is not None and fHT is not None : <EOL> HH , HT = self . nnet . get_corr ( ) <EOL> HH [ np . diag_indices_from ( HH ) ] -= self . nnet . norm <EOL> _write_fHH ( fHH , fHT , HH , HT ) <EOL> def predict_async ( self , fX , fY , istart = <NUM_LIT:0> , icount = np . inf ) : <EOL> """<STR_LIT>""" <EOL> assert len ( self . nnet . neurons ) > <NUM_LIT:0> , "<STR_LIT>" <EOL> assert self . nnet . B is not None , "<STR_LIT>" <EOL> X , _ = self . _checkdata ( fX , None ) <EOL> N = X . shape [ <NUM_LIT:0> ] <EOL> icount = min ( icount , N - istart ) <EOL> nb = int ( np . ceil ( float ( icount ) / self . batch ) ) <EOL> make_hdf5 ( ( icount , self . nnet . outputs ) , fY ) <EOL> qr_in = mp . Queue ( ) <EOL> qr_out = mp . Queue ( <NUM_LIT:1> ) <EOL> reader = mp . Process ( target = _ireader , args = ( fX , qr_in , qr_out ) ) <EOL> reader . daemon = True <EOL> reader . start ( ) <EOL> qw_in = mp . Queue ( <NUM_LIT:1> ) <EOL> writer = mp . Process ( target = _iwriter , args = ( fY , qw_in ) ) <EOL> writer . daemon = True <EOL> writer . start ( ) <EOL> t = time ( ) <EOL> t0 = time ( ) <EOL> eta = <NUM_LIT:0> <EOL> for b in xrange ( <NUM_LIT:0> , nb + <NUM_LIT:1> ) : <EOL> start_next = b * self . batch + istart <EOL> stop_next = min ( ( b + <NUM_LIT:1> ) * self . batch + istart , icount + istart ) <EOL> qr_in . put ( ( start_next , stop_next ) ) <EOL> if b > <NUM_LIT:0> : <EOL> Xb = qr_out . get ( ) <EOL> Yb = self . nnet . _predict ( Xb ) <EOL> qw_in . put ( ( Yb , start - istart , stop - istart ) ) <EOL> start = start_next <EOL> stop = stop_next <EOL> eta = int ( ( ( time ( ) - t0 ) / ( b + <NUM_LIT:1> ) ) * ( nb - b - <NUM_LIT:1> ) ) <EOL> if time ( ) - t > self . tprint : <EOL> print ( "<STR_LIT>" % ( b + <NUM_LIT:1> , nb , eta / <NUM_LIT> , ( eta % <NUM_LIT> ) / <NUM_LIT> , eta % <NUM_LIT> ) ) <EOL> t = time ( ) <EOL> qw_in . put ( None ) <EOL> reader . join ( ) <EOL> writer . join ( ) <EOL> def _checkcorr ( self , fHH , fHT ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> h5 = open_file ( fHH , "<STR_LIT:r>" ) <EOL> except : <EOL> raise IOError ( "<STR_LIT>" % fHH ) <EOL> node = None <EOL> for node in h5 . walk_nodes ( ) : <EOL> pass <EOL> if node : <EOL> HH = node [ : ] <EOL> else : <EOL> raise IOError ( "<STR_LIT>" % fHH ) <EOL> h5 . close ( ) <EOL> try : <EOL> h5 = open_file ( fHT , "<STR_LIT:r>" ) <EOL> except : <EOL> raise IOError ( "<STR_LIT>" % fHT ) <EOL> node = None <EOL> for node in h5 . walk_nodes ( ) : <EOL> pass <EOL> if node : <EOL> HT = node [ : ] <EOL> else : <EOL> raise IOError ( "<STR_LIT>" % fHT ) <EOL> h5 . close ( ) <EOL> L = self . nnet . L <EOL> c = self . nnet . outputs <EOL> assert len ( self . nnet . neurons ) > <NUM_LIT:0> , "<STR_LIT>" <EOL> assert HH . shape [ <NUM_LIT:0> ] == L and HH . shape [ <NUM_LIT:1> ] == L , "<STR_LIT>" % ( L , L , HH . shape [ <NUM_LIT:0> ] , HH . shape [ <NUM_LIT:1> ] ) <EOL> assert HT . shape [ <NUM_LIT:0> ] == L and HT . shape [ <NUM_LIT:1> ] == c , "<STR_LIT>" % ( L , c , HH . shape [ <NUM_LIT:0> ] , HH . shape [ <NUM_LIT:1> ] ) <EOL> return HH , HT </s>
