(f, th_mean, batch_size, n_iter, elite_frac, initial_std=1.0)
Generic implementation of the cross-entropy method for maximizing a black-box function

    f: a function mapping from vector -> scalar
    th_mean: initial mean over input distribution
    batch_size: number of samples of theta to evaluate per batch
    n_iter: number of batches
    elite_frac: each batch, select this fraction of the top-performing samples
    initial_std: initial standard deviation over parameter vectors
Generic implementation of the cross-entropy method for maximizing a black-box function
Generic implementation of the cross - entropy method for maximizing a black - box function
def cem(f, th_mean, batch_size, n_iter, elite_frac, initial_std=1.0):
    """
    Generic implementation of the cross-entropy method for maximizing a black-box function

    f: a function mapping from vector -> scalar
    th_mean: initial mean over input distribution
    batch_size: number of samples of theta to evaluate per batch
    n_iter: number of batches
    elite_frac: each batch, select this fraction of the top-performing samples
    initial_std: initial standard deviation over parameter vectors
    """
    n_elite = int(np.round(batch_size*elite_frac))
    th_std = np.ones_like(th_mean) * initial_std

    for _ in range(n_iter):
        ths = np.array([th_mean + dth for dth in  th_std[None,:]*np.random.randn(batch_size, th_mean.size)])
        ys = np.array([f(th) for th in ths])
        elite_inds = ys.argsort()[::-1][:n_elite]
        elite_ths = ths[elite_inds]
        th_mean = elite_ths.mean(axis=0)
        th_std = elite_ths.std(axis=0)
        yield {'ys' : ys, 'theta_mean' : th_mean, 'y_mean' : ys.mean()}
def cem ( f , th_mean , batch_size , n_iter , elite_frac , initial_std = 1.0 ) : n_elite = int ( np . round ( batch_size * elite_frac ) ) th_std = np . ones_like ( th_mean ) * initial_std for _ in range ( n_iter ) : ths = np . array ( [ th_mean + dth for dth in th_std [ None , : ] * np . random . randn ( batch_size , th_mean . size ) ] ) ys = np . array ( [ f ( th ) for th in ths ] ) elite_inds = ys . argsort ( ) [ : : - 1 ] [ : n_elite ] elite_ths = ths [ elite_inds ] th_mean = elite_ths . mean ( axis = 0 ) th_std = elite_ths . std ( axis = 0 ) yield { 'ys' : ys , 'theta_mean' : th_mean , 'y_mean' : ys . mean ( ) }