<s> """<STR_LIT>""" <EOL> import logging <EOL> import numpy as np <EOL> logger = logging . getLogger ( __name__ ) <EOL> def sample_from_measure ( model , xmin , xmax , n_representers , BestGuesses , acquisition_fn ) : <EOL> """<STR_LIT>""" <EOL> if ( model . X . size == <NUM_LIT:0> ) : <EOL> dim = xmax . size <EOL> zb = np . add ( np . multiply ( ( xmax - xmin ) , np . random . uniform ( size = ( n_representers , dim ) ) ) , xmin ) <EOL> zb = zb . flatten ( ) . reshape ( ( dim , n_representers ) ) . transpose ( ) <EOL> mb = np . dot ( - np . log ( np . prod ( xmax - xmin ) ) , np . ones ( ( n_representers , <NUM_LIT:1> ) ) ) <EOL> return zb , mb <EOL> dim = model . X . shape [ <NUM_LIT:1> ] <EOL> d0 = np . divide ( <EOL> np . linalg . norm ( ( xmax - xmin ) , ord = <NUM_LIT:2> ) , <EOL> <NUM_LIT:2> ) <EOL> zb = np . zeros ( ( n_representers , dim ) ) <EOL> mb = np . zeros ( ( n_representers , <NUM_LIT:1> ) ) <EOL> numblock = np . floor ( n_representers / <NUM_LIT> ) <EOL> restarts = np . zeros ( ( numblock , dim ) ) <EOL> restarts [ <NUM_LIT:0> : ( np . minimum ( numblock , BestGuesses . shape [ <NUM_LIT:0> ] ) ) , ] = BestGuesses [ np . maximum ( BestGuesses . shape [ <NUM_LIT:0> ] - numblock + <NUM_LIT:1> , <NUM_LIT:1> ) - <NUM_LIT:1> : , ] <EOL> restarts [ ( np . minimum ( numblock , BestGuesses . shape [ <NUM_LIT:0> ] ) ) : numblock , ] = np . add ( xmin , <EOL> np . multiply ( ( xmax - xmin ) , <EOL> np . random . uniform ( <EOL> size = ( np . arange ( np . minimum ( <EOL> numblock , BestGuesses . shape [ <NUM_LIT:0> ] ) + <NUM_LIT:1> , numblock + <NUM_LIT:1> ) . size , dim ) <EOL> ) ) ) <EOL> xx = restarts [ <NUM_LIT:0> , np . newaxis ] <EOL> subsample = <NUM_LIT:20> <EOL> num_interrupts = <NUM_LIT:0> <EOL> i = <NUM_LIT:0> <EOL> while i < subsample * n_representers + <NUM_LIT:1> : <EOL> i += <NUM_LIT:1> <EOL> if ( ( i - <NUM_LIT:1> ) % ( subsample * <NUM_LIT:10> ) == <NUM_LIT:0> ) and ( i / ( subsample * <NUM_LIT> ) < numblock ) : <EOL> xx = restarts [ i / ( subsample * <NUM_LIT:10> ) , np . newaxis ] <EOL> xx = slice_ShrinkRank_nolog ( xx , acquisition_fn , d0 , True ) <EOL> if i % subsample == <NUM_LIT:0> : <EOL> emb = acquisition_fn ( xx ) <EOL> mb [ ( i / subsample ) - <NUM_LIT:1> , <NUM_LIT:0> ] = np . log ( emb ) <EOL> zb [ ( i / subsample ) - <NUM_LIT:1> , ] = xx <EOL> return zb , mb <EOL> def projNullSpace ( J , v ) : <EOL> """<STR_LIT>""" <EOL> if J . shape [ <NUM_LIT:1> ] > <NUM_LIT:0> : <EOL> return v - J . dot ( J . transpose ( ) ) . dot ( v ) <EOL> else : <EOL> return v <EOL> def slice_ShrinkRank_nolog ( xx , P , s0 , transpose ) : <EOL> """<STR_LIT>""" <EOL> if transpose : <EOL> xx = xx . transpose ( ) <EOL> D = xx . shape [ <NUM_LIT:0> ] <EOL> f = P ( xx . transpose ( ) ) <EOL> logf = np . log ( f ) <EOL> logy = np . log ( np . random . uniform ( ) ) + logf <EOL> theta = <NUM_LIT> <EOL> k = <NUM_LIT:0> <EOL> s = np . array ( [ s0 ] ) <EOL> c = np . zeros ( ( D , <NUM_LIT:0> ) ) <EOL> J = np . zeros ( ( D , <NUM_LIT:0> ) ) <EOL> while True : <EOL> k += <NUM_LIT:1> <EOL> c = np . append ( c , np . array ( projNullSpace ( J , xx + s [ k - <NUM_LIT:1> ] * np . random . randn ( D , <NUM_LIT:1> ) ) ) , axis = <NUM_LIT:1> ) <EOL> sx = np . divide ( <NUM_LIT:1.> , np . sum ( np . divide ( <NUM_LIT:1.> , s ) ) ) <EOL> mx = np . dot ( <EOL> sx , <EOL> np . sum ( <EOL> np . multiply ( <EOL> np . divide ( <NUM_LIT:1.> , s ) , <EOL> np . subtract ( c , xx ) <EOL> ) , <EOL> <NUM_LIT:1> ) ) <EOL> xk = xx + projNullSpace ( J , mx . reshape ( ( D , <NUM_LIT:1> ) ) + <EOL> np . multiply ( sx , np . random . normal ( size = ( D , <NUM_LIT:1> ) ) ) ) <EOL> fk , dfk = P ( xk . transpose ( ) , derivative = True ) <EOL> logfk = np . log ( fk ) <EOL> dlogfk = np . divide ( dfk , fk ) <EOL> if ( logfk > logy ) . all ( ) : <EOL> xx = xk . transpose ( ) <EOL> return xx <EOL> else : <EOL> g = projNullSpace ( J , dlogfk ) <EOL> if J . shape [ <NUM_LIT:1> ] < D - <NUM_LIT:1> and np . dot ( g . transpose ( ) , dlogfk ) > <NUM_LIT:0.5> * np . linalg . norm ( g ) * np . linalg . norm ( dlogfk ) : <EOL> J = np . append ( J , np . divide ( g , np . linalg . norm ( g ) ) , axis = <NUM_LIT:1> ) <EOL> s = np . append ( s , s [ k - <NUM_LIT:1> ] ) <EOL> else : <EOL> s = np . append ( s , np . multiply ( theta , s [ k - <NUM_LIT:1> ] ) ) <EOL> if s [ k ] < np . spacing ( <NUM_LIT:1> ) : <EOL> logger . error ( <EOL> '<STR_LIT>' ) <EOL> if transpose : <EOL> xx = xx . transpose ( ) <EOL> return xx <EOL> else : <EOL> return xx </s>
