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: Initialize variables and start training.
  
  Args:
      trainer (Trainer): Experiment trainer, which provides services
          such as snapshotting and sampler control.
  
  Returns:
      float: The average return in last epoch cycle.
docstr_tok: ["Initialize","variables","and","start","training",".","Args",":","trainer","(","Trainer",")",":","Experiment","trainer",",","which","provides","services","such","as","snapshotting","and","sampler","control",".","Returns",":","float",":","The","average","return","in","last","epoch","cycle","."]
code: def train(self, trainer):
      """Initialize variables and start training.
  
      Args:
          trainer (Trainer): Experiment trainer, which provides services
              such as snapshotting and sampler control.
  
      Returns:
          float: The average return in last epoch cycle.
  
      """
      # epoch-wise
      self._cur_std = self._init_std
      self._cur_mean = self.policy.get_param_values()
      # epoch-cycle-wise
      self._cur_params = self._cur_mean
      self._all_returns = []
      self._all_params = [self._cur_mean.copy()]
      # constant
      self._n_best = int(self._n_samples * self._best_frac)
      assert (
          self._n_best >= 1
      ), "n_samples is too low. Make sure that n_samples * best_frac >= 1"
      self._n_params = len(self._cur_mean)
  
      # start actual training
      last_return = None
  
      for _ in trainer.step_epochs():
          for _ in range(self._n_samples):
              trainer.step_episode = trainer.obtain_episodes(
                  trainer.step_itr
              )
              last_return = self._train_once(
                  trainer.step_itr, trainer.step_episode
              )
              trainer.step_itr += 1
  
      return last_return
  
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 3 for calling the CEM obj's underlying member method code with example usage: obj.train(trainer) and returns: last_return