human
I am working on the file "Guided_Evolutionary_Strategies_Demo.ipynb".
The first task is:
Guided ES
Guided ES is our proposed method. It uses a diagonal plus low-rank covariance matrix for drawing perturbations, where the low-rank subspace is spanned by the available gradient information.
Can you write Python code for it?
 Can you provide a the Python code you requested?
assistant
#Here is the Python code you requested.

def guided_es(loss_fn, grads_and_vars, sigma=0.1, alpha=0.5, beta=1.0):
  
  def vardist(grad, variable):
    """Builds the sampling distribution for the given variable."""
    
    n = tf.cast(variable.shape[0], tf.float32)
    k = 1
    
    a = sigma * tf.sqrt(alpha / n)
    c = sigma * tf.sqrt((1-alpha) / k)
    b = tf.sqrt(a ** 2 + c ** 2) - a
    
    scale_diag = a * tf.ones(tf.cast(n, tf.int32))
    perturb_diag = b * tf.ones(1,)
    perturb_factor, _ = tf.qr(grad)
    
    return mvn_lowrank(scale_diag=scale_diag,
                       scale_perturb_factor=perturb_factor,
                       scale_perturb_diag=perturb_diag)
  
  dists = {v.op.name: vardist(g, v) for g, v in grads_and_vars}
  
  # antithetic getter
  updates_and_vars = evostrat_update(loss_fn, dists, grads_and_vars, beta, sigma)
  
  return updates_and_vars