<s> from __future__ import absolute_import <EOL> import scipy <EOL> import scipy . linalg <EOL> from . base import Minimizer <EOL> class Xnes ( Minimizer ) : <EOL> def __init__ ( self , wrt , f , args = None ) : <EOL> super ( Xnes , self ) . __init__ ( wrt , args = args ) <EOL> self . _f = f <EOL> dim = self . wrt . shape [ <NUM_LIT:0> ] <EOL> log_dim = scipy . log ( dim ) <EOL> self . step_rate = <NUM_LIT> * ( <NUM_LIT:3> + log_dim ) / dim / scipy . sqrt ( dim ) <EOL> self . batch_size = <NUM_LIT:4> + int ( scipy . floor ( <NUM_LIT:3> * log_dim ) ) <EOL> def set_from_info ( self , info ) : <EOL> raise NotImplemented ( '<STR_LIT>' ) <EOL> def extended_info ( self , ** kwargs ) : <EOL> raise NotImplemented ( '<STR_LIT>' ) <EOL> def f ( self , x , * args , ** kwargs ) : <EOL> return - self . _f ( x , * args , ** kwargs ) <EOL> def __iter__ ( self ) : <EOL> dim = self . wrt . shape [ <NUM_LIT:0> ] <EOL> I = scipy . eye ( dim ) <EOL> A = scipy . eye ( dim ) <EOL> center = self . wrt . copy ( ) <EOL> n_evals = <NUM_LIT:0> <EOL> best_wrt = None <EOL> best_x = float ( '<STR_LIT>' ) <EOL> for i , ( args , kwargs ) in enumerate ( self . args ) : <EOL> samples = scipy . random . standard_normal ( ( self . batch_size , dim ) ) <EOL> samples = scipy . dot ( samples , A ) + center <EOL> fitnesses = [ self . f ( samples [ j ] , * args , ** kwargs ) <EOL> for j in range ( samples . shape [ <NUM_LIT:0> ] ) ] <EOL> fitnesses = scipy . array ( fitnesses ) . flatten ( ) <EOL> if fitnesses . max ( ) > best_x : <EOL> best_loss = fitnesses . max ( ) <EOL> self . wrt [ : ] = samples [ fitnesses . argmax ( ) ] <EOL> utilities = self . compute_utilities ( fitnesses ) <EOL> center += scipy . dot ( scipy . dot ( utilities , samples ) , A ) <EOL> cov_gradient = sum ( [ u * ( scipy . outer ( s , s ) - I ) <EOL> for ( s , u ) in zip ( samples , utilities ) ] ) <EOL> update = scipy . linalg . expm2 ( A * cov_gradient * self . step_rate * <NUM_LIT:0.5> ) <EOL> A [ : ] = scipy . dot ( A , update ) <EOL> yield dict ( loss = - best_x , n_iter = i ) <EOL> def compute_utilities ( self , fitnesses ) : <EOL> n_fitnesses = fitnesses . shape [ <NUM_LIT:0> ] <EOL> ranks = scipy . zeros_like ( fitnesses ) <EOL> l = sorted ( enumerate ( fitnesses ) , key = lambda x : x [ <NUM_LIT:1> ] ) <EOL> for i , ( j , _ ) in enumerate ( l ) : <EOL> ranks [ j ] = i <EOL> utilities = - scipy . log ( ( n_fitnesses - ranks ) . astype ( '<STR_LIT>' ) ) <EOL> utilities += scipy . log ( n_fitnesses / <NUM_LIT> + <NUM_LIT:1.0> ) <EOL> utilities = scipy . clip ( utilities , <NUM_LIT:0> , float ( '<STR_LIT>' ) ) <EOL> utilities /= utilities . sum ( ) <EOL> utilities -= <NUM_LIT:1.> / n_fitnesses <EOL> return utilities </s>
