<s> import math <EOL> import numpy as np <EOL> from scipy . stats import kurtosis <EOL> from . . utils import check_random_state <EOL> class logger ( object ) : <EOL> @ staticmethod <EOL> def info ( * args , ** kwargs ) : <EOL> pass <EOL> def infomax ( data , weights = None , l_rate = None , block = None , w_change = <NUM_LIT> , <EOL> anneal_deg = <NUM_LIT> , anneal_step = <NUM_LIT> , extended = False , n_subgauss = <NUM_LIT:1> , <EOL> kurt_size = <NUM_LIT> , ext_blocks = <NUM_LIT:1> , max_iter = <NUM_LIT:200> , <EOL> random_state = None , verbose = None ) : <EOL> """<STR_LIT>""" <EOL> rng = check_random_state ( random_state ) <EOL> max_weight = <NUM_LIT> <EOL> restart_fac = <NUM_LIT> <EOL> min_l_rate = <NUM_LIT> <EOL> blowup = <NUM_LIT> <EOL> blowup_fac = <NUM_LIT:0.5> <EOL> n_small_angle = <NUM_LIT:20> <EOL> degconst = <NUM_LIT> / np . pi <EOL> extmomentum = <NUM_LIT:0.5> <EOL> signsbias = <NUM_LIT> <EOL> signcount_threshold = <NUM_LIT> <EOL> signcount_step = <NUM_LIT:2> <EOL> if ext_blocks > <NUM_LIT:0> : <EOL> n_subgauss = <NUM_LIT:1> <EOL> n_samples , n_features = data . shape <EOL> n_features_square = n_features ** <NUM_LIT:2> <EOL> if l_rate is None : <EOL> l_rate = <NUM_LIT> / math . log ( n_features ** <NUM_LIT> ) <EOL> if block is None : <EOL> block = int ( math . floor ( math . sqrt ( n_samples / <NUM_LIT> ) ) ) <EOL> logger . info ( '<STR_LIT>' % '<STR_LIT>' if extended is True <EOL> else '<STR_LIT:U+0020>' ) <EOL> nblock = n_samples // block <EOL> lastt = ( nblock - <NUM_LIT:1> ) * block + <NUM_LIT:1> <EOL> if weights is None : <EOL> weights = np . identity ( n_features , dtype = np . float64 ) <EOL> BI = block * np . identity ( n_features , dtype = np . float64 ) <EOL> bias = np . zeros ( ( n_features , <NUM_LIT:1> ) , dtype = np . float64 ) <EOL> onesrow = np . ones ( ( <NUM_LIT:1> , block ) , dtype = np . float64 ) <EOL> startweights = weights . copy ( ) <EOL> oldweights = startweights . copy ( ) <EOL> step = <NUM_LIT:0> <EOL> count_small_angle = <NUM_LIT:0> <EOL> wts_blowup = False <EOL> blockno = <NUM_LIT:0> <EOL> signcount = <NUM_LIT:0> <EOL> if extended is True : <EOL> signs = np . identity ( n_features ) <EOL> signs . flat [ slice ( <NUM_LIT:0> , n_features * n_subgauss , n_features ) ] <EOL> kurt_size = min ( kurt_size , n_samples ) <EOL> old_kurt = np . zeros ( n_features , dtype = np . float64 ) <EOL> oldsigns = np . zeros ( ( n_features , n_features ) ) <EOL> olddelta , oldchange = <NUM_LIT:1.> , <NUM_LIT:0.> <EOL> while step < max_iter : <EOL> permute = list ( range ( n_samples ) ) <EOL> rng . shuffle ( permute ) <EOL> for t in range ( <NUM_LIT:0> , lastt , block ) : <EOL> u = np . dot ( data [ permute [ t : t + block ] , : ] , weights ) <EOL> u += np . dot ( bias , onesrow ) . T <EOL> if extended is True : <EOL> y = np . tanh ( u ) <EOL> weights += l_rate * np . dot ( weights , <EOL> BI - np . dot ( np . dot ( u . T , y ) , signs ) - <EOL> np . dot ( u . T , u ) ) <EOL> bias += l_rate * np . reshape ( np . sum ( y , axis = <NUM_LIT:0> , <EOL> dtype = np . float64 ) * - <NUM_LIT> , <EOL> ( n_features , <NUM_LIT:1> ) ) <EOL> else : <EOL> y = <NUM_LIT:1.0> / ( <NUM_LIT:1.0> + np . exp ( - u ) ) <EOL> weights += l_rate * np . dot ( weights , <EOL> BI + np . dot ( u . T , ( <NUM_LIT:1.0> - <NUM_LIT> * y ) ) ) <EOL> bias += l_rate * np . reshape ( np . sum ( ( <NUM_LIT:1.0> - <NUM_LIT> * y ) , axis = <NUM_LIT:0> , <EOL> dtype = np . float64 ) , ( n_features , <NUM_LIT:1> ) ) <EOL> max_weight_val = np . max ( np . abs ( weights ) ) <EOL> if max_weight_val > max_weight : <EOL> wts_blowup = True <EOL> blockno += <NUM_LIT:1> <EOL> if wts_blowup : <EOL> break <EOL> if extended is True : <EOL> n = np . fix ( blockno / ext_blocks ) <EOL> if np . abs ( n ) * ext_blocks == blockno : <EOL> if kurt_size < n_samples : <EOL> rp = np . floor ( rng . uniform ( <NUM_LIT:0> , <NUM_LIT:1> , kurt_size ) * <EOL> ( n_samples - <NUM_LIT:1> ) ) <EOL> tpartact = np . dot ( data [ rp . astype ( int ) , : ] , weights ) . T <EOL> else : <EOL> tpartact = np . dot ( data , weights ) . T <EOL> kurt = kurtosis ( tpartact , axis = <NUM_LIT:1> , fisher = True ) <EOL> if extmomentum != <NUM_LIT:0> : <EOL> kurt = ( extmomentum * old_kurt + <EOL> ( <NUM_LIT:1.0> - extmomentum ) * kurt ) <EOL> old_kurt = kurt <EOL> signs . flat [ : : n_features + <NUM_LIT:1> ] = ( ( kurt + signsbias ) / <EOL> np . abs ( kurt + signsbias ) ) <EOL> ndiff = ( ( signs . flat [ : : n_features + <NUM_LIT:1> ] - <EOL> oldsigns . flat [ : : n_features + <NUM_LIT:1> ] ) != <NUM_LIT:0> ) . sum ( ) <EOL> if ndiff == <NUM_LIT:0> : <EOL> signcount += <NUM_LIT:1> <EOL> else : <EOL> signcount = <NUM_LIT:0> <EOL> oldsigns = signs <EOL> if signcount >= signcount_threshold : <EOL> ext_blocks = np . fix ( ext_blocks * signcount_step ) <EOL> signcount = <NUM_LIT:0> <EOL> if not wts_blowup : <EOL> oldwtchange = weights - oldweights <EOL> step += <NUM_LIT:1> <EOL> angledelta = <NUM_LIT:0.0> <EOL> delta = oldwtchange . reshape ( <NUM_LIT:1> , n_features_square ) <EOL> change = np . sum ( delta * delta , dtype = np . float64 ) <EOL> if step > <NUM_LIT:1> : <EOL> angledelta = math . acos ( np . sum ( delta * olddelta ) / <EOL> math . sqrt ( change * oldchange ) ) <EOL> angledelta *= degconst <EOL> oldweights = weights . copy ( ) <EOL> if angledelta > anneal_deg : <EOL> l_rate *= anneal_step <EOL> olddelta = delta <EOL> oldchange = change <EOL> count_small_angle = <NUM_LIT:0> <EOL> else : <EOL> if step == <NUM_LIT:1> : <EOL> olddelta = delta <EOL> oldchange = change <EOL> count_small_angle += <NUM_LIT:1> <EOL> if count_small_angle > n_small_angle : <EOL> max_iter = step <EOL> if step > <NUM_LIT:2> and change < w_change : <EOL> step = max_iter <EOL> elif change > blowup : <EOL> l_rate *= blowup_fac <EOL> else : <EOL> step = <NUM_LIT:0> <EOL> wts_blowup = <NUM_LIT:0> <EOL> blockno = <NUM_LIT:1> <EOL> l_rate *= restart_fac <EOL> weights = startweights . copy ( ) <EOL> oldweights = startweights . copy ( ) <EOL> olddelta = np . zeros ( ( <NUM_LIT:1> , n_features_square ) , dtype = np . float64 ) <EOL> bias = np . zeros ( ( n_features , <NUM_LIT:1> ) , dtype = np . float64 ) <EOL> if extended : <EOL> signs = np . identity ( n_features ) <EOL> signs . flat [ slice ( <NUM_LIT:0> , n_features * n_subgauss , n_features ) ] <EOL> oldsigns = np . zeros ( ( n_features , n_features ) ) <EOL> if l_rate > min_l_rate : <EOL> if verbose : <EOL> logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' % l_rate ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return weights . T </s>
