class_docstr: Cross Entropy Method.
  
  CEM works by iteratively optimizing a gaussian distribution of policy.
  
  In each epoch, CEM does the following:
  1. Sample n_samples policies from a gaussian distribution of
     mean cur_mean and std cur_std.
  2. Collect episodes for each policy.
  3. Update cur_mean and cur_std by doing Maximum Likelihood Estimation
     over the n_best top policies in terms of return.
  
  Args:
      env_spec (EnvSpec): Environment specification.
      policy (garage.np.policies.Policy): Action policy.
      sampler (garage.sampler.Sampler): Sampler.
      n_samples (int): Number of policies sampled in one epoch.
      discount (float): Environment reward discount.
      best_frac (float): The best fraction.
      init_std (float): Initial std for policy param distribution.
      extra_std (float): Decaying std added to param distribution.
      extra_decay_time (float): Epochs that it takes to decay extra std.
class_docstr_tok: ["Cross","Entropy","Method",".","CEM","works","by","iteratively","optimizing","a","gaussian","distribution","of","policy",".","In","each","epoch",",","CEM","does","the","following",":","1",".","Sample","n_samples","policies","from","a","gaussian","distribution","of","mean","cur_mean","and","std","cur_std",".","2",".","Collect","episodes","for","each","policy",".","3",".","Update","cur_mean","and","cur_std","by","doing","Maximum","Likelihood","Estimation","over","the","n_best","top","policies","in","terms","of","return",".","Args",":","env_spec","(","EnvSpec",")",":","Environment","specification",".","policy","(","garage.np.policies.Policy",")",":","Action","policy",".","sampler","(","garage.sampler.Sampler",")",":","Sampler",".","n_samples","(","int",")",":","Number","of","policies","sampled","in","one","epoch",".","discount","(","float",")",":","Environment","reward","discount",".","best_frac","(","float",")",":","The","best","fraction",".","init_std","(","float",")",":","Initial","std","for","policy","param","distribution",".","extra_std","(","float",")",":","Decaying","std","added","to","param","distribution",".","extra_decay_time","(","float",")",":","Epochs","that","it","takes","to","decay","extra","std","."]
docstr: Perform one step of policy optimization given one batch of samples.
  
  Args:
      itr (int): Iteration number.
      episodes (garage.EpisodeBatch): Episodes collected using the
          current policy.
  
  Returns:
      float: The average return of epoch cycle.
docstr_tok: ["Perform","one","step","of","policy","optimization","given","one","batch","of","samples",".","Args",":","itr","(","int",")",":","Iteration","number",".","episodes","(","garage.EpisodeBatch",")",":","Episodes","collected","using","the","current","policy",".","Returns",":","float",":","The","average","return","of","epoch","cycle","."]
code: def _train_once(self, itr, episodes):
      """Perform one step of policy optimization given one batch of samples.
  
      Args:
          itr (int): Iteration number.
          episodes (garage.EpisodeBatch): Episodes collected using the
              current policy.
  
      Returns:
          float: The average return of epoch cycle.
  
      """
      # -- Stage: Run and calculate performance of the algorithm
      undiscounted_returns = log_performance(
          itr, episodes, discount=self._discount
      )
      self._episode_reward_mean.extend(undiscounted_returns)
      tabular.record(
          "Extras/EpisodeRewardMean", np.mean(self._episode_reward_mean)
      )
      average_return = np.mean(undiscounted_returns)
  
      epoch = itr // self._n_samples
      i_sample = itr - epoch * self._n_samples
      tabular.record("Epoch", epoch)
      tabular.record("# Sample", i_sample)
      rtn = average_return
      self._all_returns.append(average_return)
  
      # -- Stage: Update policy distribution.
      if (itr + 1) % self._n_samples == 0:
          avg_rtns = np.array(self._all_returns)
          best_inds = np.argsort(-avg_rtns)[: self._n_best]
          best_params = np.array(self._all_params)[best_inds]
  
          # MLE of normal distribution
          self._cur_mean = best_params.mean(axis=0)
          self._cur_std = best_params.std(axis=0)
          self.policy.set_param_values(self._cur_mean)
  
          # Clear for next epoch
          rtn = max(self._all_returns)
          self._all_returns.clear()
          self._all_params.clear()
  
      # -- Stage: Generate a new policy for next path sampling
      self._cur_params = self._sample_params(itr)
      self._all_params.append(self._cur_params.copy())
      self.policy.set_param_values(self._cur_params)
  
      logger.log(tabular)
      return rtn
  
file_path: garage/src/garage/np/algos/cem.py
imports: import collections
  from dowel import logger, tabular
  import numpy
  from garage import log_performance
  from garage.np.algos.rl_algorithm import RLAlgorithm
label_desc: Use image node_id 4 for calling the CEM obj's underlying member method code with example usage: obj._train_once(itr, episodes) and returns: rtn