assignment3_coding/starter_code 2/code/policy_network.py
def sample_path(self, env, num_episodes = None):
    """
    Sample paths (trajectories) from the environment.

    Args:
        num_episodes: the number of episodes to be sampled
            if none, sample one batch (size indicated by config file)
        env: open AI Gym envinronment

    Returns:
        paths: a list of paths. Each path in paths is a dictionary with
            path["observation"] a numpy array of ordered observations in the path
            path["actions"] a numpy array of the corresponding actions in the path
            path["reward"] a numpy array of the corresponding rewards in the path
        total_rewards: the sum of all rewards encountered during this "path"

    You do not have to implement anything in this function, but you will need to
    understand what it returns, and it is worthwhile to look over the code
    just so you understand how we are taking actions in the environment
    and generating batches to train on.
    """
    episode = 0
    episode_rewards = []
    paths = []
    t = 0

    while (num_episodes or t < self.config.batch_size):
      state = env.reset()
      states, actions, rewards = [], [], []
      episode_reward = 0

      for step in range(self.config.max_ep_len):
        states.append(state)
        action = self.sess.run(self.sampled_action, feed_dict={self.observation_placeholder : states[-1][None]})
        state, reward, done, info = env.step(action)
        actions.append(action)
        rewards.append(reward)
        episode_reward += reward
        t += 1
        if (done or step == self.config.max_ep_len-1):
          episode_rewards.append(episode_reward)
          break
        if (not num_episodes) and t == self.config.batch_size:
          break

      path = {"observation" : np.array(states),
                      "reward" : np.array(rewards),
                      "action" : np.array(actions)}
      paths.append(path)
      episode += 1
      if num_episodes and episode >= num_episodes:
        break

    return paths, episode_rewards

    Sample paths (trajectories) from the environment.

    Args:
        num_episodes: the number of episodes to be sampled
            if none, sample one batch (size indicated by config file)
        env: open AI Gym envinronment

    Returns:
        paths: a list of paths. Each path in paths is a dictionary with
            path["observation"] a numpy array of ordered observations in the path
            path["actions"] a numpy array of the corresponding actions in the path
            path["reward"] a numpy array of the corresponding rewards in the path
        total_rewards: the sum of all rewards encountered during this "path"

    You do not have to implement anything in this function, but you will need to
    understand what it returns, and it is worthwhile to look over the code
    just so you understand how we are taking actions in the environment
    and generating batches to train on.
    
Sample paths (trajectories) from the environment.
Sample paths ( trajectories ) from the environment .
def sample_path(self, env, num_episodes = None):
    episode = 0
    episode_rewards = []
    paths = []
    t = 0
    while (num_episodes or t < self.config.batch_size):
      state = env.reset()
      states, actions, rewards = [], [], []
      episode_reward = 0
      for step in range(self.config.max_ep_len):
        states.append(state)
        action = self.sess.run(self.sampled_action, feed_dict={self.observation_placeholder : states[-1][None]})
        state, reward, done, info = env.step(action)
        actions.append(action)
        rewards.append(reward)
        episode_reward += reward
        t += 1
        if (done or step == self.config.max_ep_len-1):
          episode_rewards.append(episode_reward)
          break
        if (not num_episodes) and t == self.config.batch_size:
          break
      path = {"observation" : np.array(states),
                      "reward" : np.array(rewards),
                      "action" : np.array(actions)}
      paths.append(path)
      episode += 1
      if num_episodes and episode >= num_episodes:
        break
    return paths, episode_rewards
def sample_path ( self , env , num_episodes = None ) : episode = 0 episode_rewards = [ ] paths = [ ] t = 0 while ( num_episodes or t < self . config . batch_size ) : state = env . reset ( ) states , actions , rewards = [ ] , [ ] , [ ] episode_reward = 0 for step in range ( self . config . max_ep_len ) : states . append ( state ) action = self . sess . run ( self . sampled_action , feed_dict = { self . observation_placeholder : states [ - 1 ] [ None ] } ) state , reward , done , info = env . step ( action ) actions . append ( action ) rewards . append ( reward ) episode_reward += reward t += 1 if ( done or step == self . config . max_ep_len - 1 ) : episode_rewards . append ( episode_reward ) break if ( not num_episodes ) and t == self . config . batch_size : break path = { "observation" : np . array ( states ) , "reward" : np . array ( rewards ) , "action" : np . array ( actions ) } paths . append ( path ) episode += 1 if num_episodes and episode >= num_episodes : break return paths , episode_rewards
Sample paths (trajectories) from the environment.
Sample paths ( trajectories ) from the environment .
"""
    Sample paths (trajectories) from the environment.

    Args:
        num_episodes: the number of episodes to be sampled
            if none, sample one batch (size indicated by config file)
        env: open AI Gym envinronment

    Returns:
        paths: a list of paths. Each path in paths is a dictionary with
            path["observation"] a numpy array of ordered observations in the path
            path["actions"] a numpy array of the corresponding actions in the path
            path["reward"] a numpy array of the corresponding rewards in the path
        total_rewards: the sum of all rewards encountered during this "path"

    You do not have to implement anything in this function, but you will need to
    understand what it returns, and it is worthwhile to look over the code
    just so you understand how we are taking actions in the environment
    and generating batches to train on.
    """
a list of paths. Each path in paths is a dictionary with
path["observation"] a numpy array of ordered observations in the path
path["actions"] a numpy array of the corresponding actions in the path
path["reward"] a numpy array of the corresponding rewards in the path
total_rewards: the sum of all rewards encountered during this "path"
a list of paths . Each path in paths is a dictionary with path [ " observation " ] a numpy array of ordered observations in the path path [ " actions " ] a numpy array of the corresponding actions in the path path [ " reward " ] a numpy array of the corresponding rewards in the path total_rewards : the sum of all rewards encountered during this " path "
paths
self
env
open AI Gym envinronment
open AI Gym envinronment
num_episodes
the number of episodes to be sampled
if none, sample one batch (size indicated by config file)
the number of episodes to be sampled if none sample one batch ( size indicated by config file )