mirror of
https://github.com/gryf/coach.git
synced 2025-12-18 03:30:19 +01:00
N-step returns for rainbow (#67)
* n_step returns for rainbow * Rename CartPole_PPO -> CartPole_ClippedPPO
This commit is contained in:
@@ -116,7 +116,6 @@ class Agent(AgentInterface):
|
||||
self.output_filter.set_device(device)
|
||||
self.pre_network_filter.set_device(device)
|
||||
|
||||
|
||||
# initialize all internal variables
|
||||
self._phase = RunPhase.HEATUP
|
||||
self.total_shaped_reward_in_current_episode = 0
|
||||
@@ -143,7 +142,7 @@ class Agent(AgentInterface):
|
||||
self.accumulated_shaped_rewards_across_evaluation_episodes = 0
|
||||
self.num_successes_across_evaluation_episodes = 0
|
||||
self.num_evaluation_episodes_completed = 0
|
||||
self.current_episode_buffer = Episode(discount=self.ap.algorithm.discount)
|
||||
self.current_episode_buffer = Episode(discount=self.ap.algorithm.discount, n_step=self.ap.algorithm.n_step)
|
||||
# TODO: add agents observation rendering for debugging purposes (not the same as the environment rendering)
|
||||
|
||||
# environment parameters
|
||||
@@ -452,10 +451,10 @@ class Agent(AgentInterface):
|
||||
:return: None
|
||||
"""
|
||||
self.current_episode_buffer.is_complete = True
|
||||
self.current_episode_buffer.update_returns()
|
||||
self.current_episode_buffer.update_transitions_rewards_and_bootstrap_data()
|
||||
|
||||
for transition in self.current_episode_buffer.transitions:
|
||||
self.discounted_return.add_sample(transition.total_return)
|
||||
self.discounted_return.add_sample(transition.n_step_discounted_rewards)
|
||||
|
||||
if self.phase != RunPhase.TEST or self.ap.task_parameters.evaluate_only:
|
||||
self.current_episode += 1
|
||||
@@ -497,7 +496,7 @@ class Agent(AgentInterface):
|
||||
self.curr_state = {}
|
||||
self.current_episode_steps_counter = 0
|
||||
self.episode_running_info = {}
|
||||
self.current_episode_buffer = Episode(discount=self.ap.algorithm.discount)
|
||||
self.current_episode_buffer = Episode(discount=self.ap.algorithm.discount, n_step=self.ap.algorithm.n_step)
|
||||
if self.exploration_policy:
|
||||
self.exploration_policy.reset()
|
||||
self.input_filter.reset()
|
||||
|
||||
@@ -17,14 +17,11 @@
|
||||
from typing import Union
|
||||
|
||||
import numpy as np
|
||||
|
||||
from rl_coach.agents.dqn_agent import DQNNetworkParameters, DQNAlgorithmParameters, DQNAgentParameters
|
||||
from rl_coach.agents.value_optimization_agent import ValueOptimizationAgent
|
||||
from rl_coach.architectures.head_parameters import CategoricalQHeadParameters
|
||||
from rl_coach.base_parameters import AgentParameters
|
||||
from rl_coach.core_types import StateType
|
||||
from rl_coach.exploration_policies.e_greedy import EGreedyParameters
|
||||
from rl_coach.memories.non_episodic.experience_replay import ExperienceReplayParameters
|
||||
from rl_coach.memories.non_episodic.prioritized_experience_replay import PrioritizedExperienceReplay
|
||||
from rl_coach.schedules import LinearSchedule
|
||||
|
||||
@@ -85,28 +82,47 @@ class CategoricalDQNAgent(ValueOptimizationAgent):
|
||||
|
||||
# for the action we actually took, the error is calculated by the atoms distribution
|
||||
# for all other actions, the error is 0
|
||||
distributed_q_st_plus_1, TD_targets = self.networks['main'].parallel_prediction([
|
||||
distributional_q_st_plus_1, TD_targets = self.networks['main'].parallel_prediction([
|
||||
(self.networks['main'].target_network, batch.next_states(network_keys)),
|
||||
(self.networks['main'].online_network, batch.states(network_keys))
|
||||
])
|
||||
|
||||
# only update the action that we have actually done in this transition
|
||||
target_actions = np.argmax(self.distribution_prediction_to_q_values(distributed_q_st_plus_1), axis=1)
|
||||
# select the optimal actions for the next state
|
||||
target_actions = np.argmax(self.distribution_prediction_to_q_values(distributional_q_st_plus_1), axis=1)
|
||||
m = np.zeros((self.ap.network_wrappers['main'].batch_size, self.z_values.size))
|
||||
|
||||
batches = np.arange(self.ap.network_wrappers['main'].batch_size)
|
||||
|
||||
# an alternative to the for loop. 3.7x perf improvement vs. the same code done with for looping.
|
||||
# only 10% speedup overall - leaving commented out as the code is not as clear.
|
||||
|
||||
# tzj_ = np.fmax(np.fmin(batch.rewards() + (1.0 - batch.game_overs()) * self.ap.algorithm.discount *
|
||||
# np.transpose(np.repeat(self.z_values[np.newaxis, :], batch.size, axis=0), (1, 0)),
|
||||
# self.z_values[-1]),
|
||||
# self.z_values[0])
|
||||
#
|
||||
# bj_ = (tzj_ - self.z_values[0]) / (self.z_values[1] - self.z_values[0])
|
||||
# u_ = (np.ceil(bj_)).astype(int)
|
||||
# l_ = (np.floor(bj_)).astype(int)
|
||||
# m_ = np.zeros((self.ap.network_wrappers['main'].batch_size, self.z_values.size))
|
||||
# np.add.at(m_, [batches, l_],
|
||||
# np.transpose(distributional_q_st_plus_1[batches, target_actions], (1, 0)) * (u_ - bj_))
|
||||
# np.add.at(m_, [batches, u_],
|
||||
# np.transpose(distributional_q_st_plus_1[batches, target_actions], (1, 0)) * (bj_ - l_))
|
||||
|
||||
for j in range(self.z_values.size):
|
||||
tzj = np.fmax(np.fmin(batch.rewards() +
|
||||
(1.0 - batch.game_overs()) * self.ap.algorithm.discount * self.z_values[j],
|
||||
self.z_values[self.z_values.size - 1]),
|
||||
self.z_values[-1]),
|
||||
self.z_values[0])
|
||||
bj = (tzj - self.z_values[0])/(self.z_values[1] - self.z_values[0])
|
||||
u = (np.ceil(bj)).astype(int)
|
||||
l = (np.floor(bj)).astype(int)
|
||||
m[batches, l] = m[batches, l] + (distributed_q_st_plus_1[batches, target_actions, j] * (u - bj))
|
||||
m[batches, u] = m[batches, u] + (distributed_q_st_plus_1[batches, target_actions, j] * (bj - l))
|
||||
m[batches, l] += (distributional_q_st_plus_1[batches, target_actions, j] * (u - bj))
|
||||
m[batches, u] += (distributional_q_st_plus_1[batches, target_actions, j] * (bj - l))
|
||||
|
||||
# total_loss = cross entropy between actual result above and predicted result for the given action
|
||||
# only update the action that we have actually done in this transition
|
||||
TD_targets[batches, batch.actions()] = m
|
||||
|
||||
# update errors in prioritized replay buffer
|
||||
@@ -120,7 +136,7 @@ class CategoricalDQNAgent(ValueOptimizationAgent):
|
||||
# TODO: fix this spaghetti code
|
||||
if isinstance(self.memory, PrioritizedExperienceReplay):
|
||||
errors = losses[0][np.arange(batch.size), batch.actions()]
|
||||
self.memory.update_priorities(batch.info('idx'), errors)
|
||||
self.call_memory('update_priorities', (batch.info('idx'), errors))
|
||||
|
||||
return total_loss, losses, unclipped_grads
|
||||
|
||||
|
||||
@@ -116,8 +116,10 @@ class ClippedPPOAgent(ActorCriticAgent):
|
||||
# calculate advantages
|
||||
advantages = []
|
||||
value_targets = []
|
||||
total_returns = batch.n_step_discounted_rewards()
|
||||
|
||||
if self.policy_gradient_rescaler == PolicyGradientRescaler.A_VALUE:
|
||||
advantages = batch.total_returns() - current_state_values
|
||||
advantages = total_returns - current_state_values
|
||||
elif self.policy_gradient_rescaler == PolicyGradientRescaler.GAE:
|
||||
# get bootstraps
|
||||
episode_start_idx = 0
|
||||
@@ -181,11 +183,13 @@ class ClippedPPOAgent(ActorCriticAgent):
|
||||
result = self.networks['main'].target_network.predict({k: v[start:end] for k, v in batch.states(network_keys).items()})
|
||||
old_policy_distribution = result[1:]
|
||||
|
||||
total_returns = batch.n_step_discounted_rewards(expand_dims=True)
|
||||
|
||||
# calculate gradients and apply on both the local policy network and on the global policy network
|
||||
if self.ap.algorithm.estimate_state_value_using_gae:
|
||||
value_targets = np.expand_dims(gae_based_value_targets, -1)
|
||||
else:
|
||||
value_targets = batch.total_returns(expand_dims=True)[start:end]
|
||||
value_targets = total_returns[start:end]
|
||||
|
||||
inputs = copy.copy({k: v[start:end] for k, v in batch.states(network_keys).items()})
|
||||
inputs['output_1_0'] = actions
|
||||
|
||||
@@ -58,11 +58,13 @@ class MixedMonteCarloAgent(ValueOptimizationAgent):
|
||||
(self.networks['main'].online_network, batch.states(network_keys))
|
||||
])
|
||||
|
||||
total_returns = batch.n_step_discounted_rewards()
|
||||
|
||||
for i in range(self.ap.network_wrappers['main'].batch_size):
|
||||
one_step_target = batch.rewards()[i] + \
|
||||
(1.0 - batch.game_overs()[i]) * self.ap.algorithm.discount * \
|
||||
q_st_plus_1[i][selected_actions[i]]
|
||||
monte_carlo_target = batch.total_returns()[i]
|
||||
monte_carlo_target = total_returns()[i]
|
||||
TD_targets[i, batch.actions()[i]] = (1 - self.mixing_rate) * one_step_target + \
|
||||
self.mixing_rate * monte_carlo_target
|
||||
|
||||
|
||||
@@ -98,10 +98,10 @@ class NECAgent(ValueOptimizationAgent):
|
||||
network_keys = self.ap.network_wrappers['main'].input_embedders_parameters.keys()
|
||||
|
||||
TD_targets = self.networks['main'].online_network.predict(batch.states(network_keys))
|
||||
|
||||
bootstrapped_return_from_old_policy = batch.n_step_discounted_rewards()
|
||||
# only update the action that we have actually done in this transition
|
||||
for i in range(self.ap.network_wrappers['main'].batch_size):
|
||||
TD_targets[i, batch.actions()[i]] = batch.total_returns()[i]
|
||||
TD_targets[i, batch.actions()[i]] = bootstrapped_return_from_old_policy[i]
|
||||
|
||||
# set the gradients to fetch for the DND update
|
||||
fetches = []
|
||||
@@ -165,10 +165,10 @@ class NECAgent(ValueOptimizationAgent):
|
||||
episode = self.call_memory('get_last_complete_episode')
|
||||
if episode is not None and self.phase != RunPhase.TEST:
|
||||
assert len(self.current_episode_state_embeddings) == episode.length()
|
||||
returns = episode.get_transitions_attribute('total_return')
|
||||
discounted_rewards = episode.get_transitions_attribute('n_step_discounted_rewards')
|
||||
actions = episode.get_transitions_attribute('action')
|
||||
self.networks['main'].online_network.output_heads[0].DND.add(self.current_episode_state_embeddings,
|
||||
actions, returns)
|
||||
actions, discounted_rewards)
|
||||
|
||||
def save_checkpoint(self, checkpoint_id):
|
||||
with open(os.path.join(self.ap.task_parameters.checkpoint_save_dir, str(checkpoint_id) + '.dnd'), 'wb') as f:
|
||||
|
||||
@@ -70,6 +70,7 @@ class PALAgent(ValueOptimizationAgent):
|
||||
|
||||
# calculate TD error
|
||||
TD_targets = np.copy(q_st_online)
|
||||
total_returns = batch.n_step_discounted_rewards()
|
||||
for i in range(self.ap.network_wrappers['main'].batch_size):
|
||||
TD_targets[i, batch.actions()[i]] = batch.rewards()[i] + \
|
||||
(1.0 - batch.game_overs()[i]) * self.ap.algorithm.discount * \
|
||||
@@ -83,7 +84,7 @@ class PALAgent(ValueOptimizationAgent):
|
||||
TD_targets[i, batch.actions()[i]] -= self.alpha * advantage_learning_update
|
||||
|
||||
# mixing monte carlo updates
|
||||
monte_carlo_target = batch.total_returns()[i]
|
||||
monte_carlo_target = total_returns[i]
|
||||
TD_targets[i, batch.actions()[i]] = (1 - self.monte_carlo_mixing_rate) * TD_targets[i, batch.actions()[i]] \
|
||||
+ self.monte_carlo_mixing_rate * monte_carlo_target
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ class PolicyGradientsAgent(PolicyOptimizationAgent):
|
||||
# batch contains a list of episodes to learn from
|
||||
network_keys = self.ap.network_wrappers['main'].input_embedders_parameters.keys()
|
||||
|
||||
total_returns = batch.total_returns()
|
||||
total_returns = batch.n_step_discounted_rewards()
|
||||
for i in reversed(range(batch.size)):
|
||||
if self.policy_gradient_rescaler == PolicyGradientRescaler.TOTAL_RETURN:
|
||||
total_returns[i] = total_returns[0]
|
||||
|
||||
@@ -73,11 +73,11 @@ class PolicyOptimizationAgent(Agent):
|
||||
episode_discounted_returns = []
|
||||
for i in range(episode.length()):
|
||||
transition = episode.get_transition(i)
|
||||
episode_discounted_returns.append(transition.total_return)
|
||||
episode_discounted_returns.append(transition.n_step_discounted_rewards)
|
||||
self.num_episodes_where_step_has_been_seen[i] += 1
|
||||
self.mean_return_over_multiple_episodes[i] -= self.mean_return_over_multiple_episodes[i] / \
|
||||
self.num_episodes_where_step_has_been_seen[i]
|
||||
self.mean_return_over_multiple_episodes[i] += transition.total_return / \
|
||||
self.mean_return_over_multiple_episodes[i] += transition.n_step_discounted_rewards / \
|
||||
self.num_episodes_where_step_has_been_seen[i]
|
||||
self.mean_discounted_return = np.mean(episode_discounted_returns)
|
||||
self.std_discounted_return = np.std(episode_discounted_returns)
|
||||
@@ -97,7 +97,7 @@ class PolicyOptimizationAgent(Agent):
|
||||
network.set_is_training(True)
|
||||
|
||||
# we need to update the returns of the episode until now
|
||||
episode.update_returns()
|
||||
episode.update_transitions_rewards_and_bootstrap_data()
|
||||
|
||||
# get t_max transitions or less if the we got to a terminal state
|
||||
# will be used for both actor-critic and vanilla PG.
|
||||
|
||||
@@ -112,11 +112,11 @@ class PPOAgent(ActorCriticAgent):
|
||||
# current_states_with_timestep = self.concat_state_and_timestep(batch)
|
||||
|
||||
current_state_values = self.networks['critic'].online_network.predict(batch.states(network_keys)).squeeze()
|
||||
|
||||
total_returns = batch.n_step_discounted_rewards()
|
||||
# calculate advantages
|
||||
advantages = []
|
||||
if self.policy_gradient_rescaler == PolicyGradientRescaler.A_VALUE:
|
||||
advantages = batch.total_returns() - current_state_values
|
||||
advantages = total_returns - current_state_values
|
||||
elif self.policy_gradient_rescaler == PolicyGradientRescaler.GAE:
|
||||
# get bootstraps
|
||||
episode_start_idx = 0
|
||||
@@ -155,6 +155,7 @@ class PPOAgent(ActorCriticAgent):
|
||||
# current_states_with_timestep = self.concat_state_and_timestep(dataset)
|
||||
|
||||
mix_fraction = self.ap.algorithm.value_targets_mix_fraction
|
||||
total_returns = batch.n_step_discounted_rewards(True)
|
||||
for j in range(epochs):
|
||||
curr_batch_size = batch.size
|
||||
if self.networks['critic'].online_network.optimizer_type != 'LBFGS':
|
||||
@@ -165,7 +166,7 @@ class PPOAgent(ActorCriticAgent):
|
||||
k: v[i * curr_batch_size:(i + 1) * curr_batch_size]
|
||||
for k, v in batch.states(network_keys).items()
|
||||
}
|
||||
total_return_batch = batch.total_returns(True)[i * curr_batch_size:(i + 1) * curr_batch_size]
|
||||
total_return_batch = total_returns[i * curr_batch_size:(i + 1) * curr_batch_size]
|
||||
old_policy_values = force_list(self.networks['critic'].target_network.predict(
|
||||
current_states_batch).squeeze())
|
||||
if self.networks['critic'].online_network.optimizer_type != 'LBFGS':
|
||||
|
||||
@@ -39,23 +39,17 @@ class RainbowDQNNetworkParameters(DQNNetworkParameters):
|
||||
class RainbowDQNAlgorithmParameters(CategoricalDQNAlgorithmParameters):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.n_step = 3
|
||||
|
||||
|
||||
class RainbowDQNExplorationParameters(ParameterNoiseParameters):
|
||||
def __init__(self, agent_params):
|
||||
super().__init__(agent_params)
|
||||
|
||||
|
||||
class RainbowDQNMemoryParameters(PrioritizedExperienceReplayParameters):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
# needed for n-step updates to work. i.e. waiting for a full episode to be closed before storing each transition
|
||||
self.store_transitions_only_when_episodes_are_terminated = True
|
||||
|
||||
|
||||
class RainbowDQNAgentParameters(CategoricalDQNAgentParameters):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.algorithm = RainbowDQNAlgorithmParameters()
|
||||
self.exploration = RainbowDQNExplorationParameters(self)
|
||||
self.exploration = ParameterNoiseParameters(self)
|
||||
self.memory = PrioritizedExperienceReplayParameters()
|
||||
self.network_wrappers = {"main": RainbowDQNNetworkParameters()}
|
||||
|
||||
@@ -65,15 +59,13 @@ class RainbowDQNAgentParameters(CategoricalDQNAgentParameters):
|
||||
|
||||
|
||||
# Rainbow Deep Q Network - https://arxiv.org/abs/1710.02298
|
||||
# Agent implementation is WIP. Currently is composed of:
|
||||
# Agent implementation is composed of:
|
||||
# 1. NoisyNets
|
||||
# 2. C51
|
||||
# 3. Prioritized ER
|
||||
# 4. DDQN
|
||||
# 5. Dueling DQN
|
||||
#
|
||||
# still missing:
|
||||
# 1. N-Step
|
||||
# 6. N-step returns
|
||||
|
||||
class RainbowDQNAgent(CategoricalDQNAgent):
|
||||
def __init__(self, agent_parameters, parent: Union['LevelManager', 'CompositeAgent']=None):
|
||||
@@ -87,7 +79,7 @@ class RainbowDQNAgent(CategoricalDQNAgent):
|
||||
|
||||
# for the action we actually took, the error is calculated by the atoms distribution
|
||||
# for all other actions, the error is 0
|
||||
distributed_q_st_plus_1, TD_targets = self.networks['main'].parallel_prediction([
|
||||
distributional_q_st_plus_n, TD_targets = self.networks['main'].parallel_prediction([
|
||||
(self.networks['main'].target_network, batch.next_states(network_keys)),
|
||||
(self.networks['main'].online_network, batch.states(network_keys))
|
||||
])
|
||||
@@ -98,15 +90,16 @@ class RainbowDQNAgent(CategoricalDQNAgent):
|
||||
|
||||
batches = np.arange(self.ap.network_wrappers['main'].batch_size)
|
||||
for j in range(self.z_values.size):
|
||||
tzj = np.fmax(np.fmin(batch.rewards() +
|
||||
(1.0 - batch.game_overs()) * self.ap.algorithm.discount * self.z_values[j],
|
||||
self.z_values[self.z_values.size - 1]),
|
||||
self.z_values[0])
|
||||
# we use batch.info('should_bootstrap_next_state') instead of (1 - batch.game_overs()) since with n-step,
|
||||
# we will not bootstrap for the last n-step transitions in the episode
|
||||
tzj = np.fmax(np.fmin(batch.n_step_discounted_rewards() + batch.info('should_bootstrap_next_state') *
|
||||
(self.ap.algorithm.discount ** self.ap.algorithm.n_step) * self.z_values[j],
|
||||
self.z_values[-1]), self.z_values[0])
|
||||
bj = (tzj - self.z_values[0])/(self.z_values[1] - self.z_values[0])
|
||||
u = (np.ceil(bj)).astype(int)
|
||||
l = (np.floor(bj)).astype(int)
|
||||
m[batches, l] = m[batches, l] + (distributed_q_st_plus_1[batches, target_actions, j] * (u - bj))
|
||||
m[batches, u] = m[batches, u] + (distributed_q_st_plus_1[batches, target_actions, j] * (bj - l))
|
||||
m[batches, l] += (distributional_q_st_plus_n[batches, target_actions, j] * (u - bj))
|
||||
m[batches, u] += (distributional_q_st_plus_n[batches, target_actions, j] * (bj - l))
|
||||
|
||||
# total_loss = cross entropy between actual result above and predicted result for the given action
|
||||
TD_targets[batches, batch.actions()] = m
|
||||
@@ -122,7 +115,7 @@ class RainbowDQNAgent(CategoricalDQNAgent):
|
||||
# TODO: fix this spaghetti code
|
||||
if isinstance(self.memory, PrioritizedExperienceReplay):
|
||||
errors = losses[0][np.arange(batch.size), batch.actions()]
|
||||
self.memory.update_priorities(batch.info('idx'), errors)
|
||||
self.call_memory('update_priorities', (batch.info('idx'), errors))
|
||||
|
||||
return total_loss, losses, unclipped_grads
|
||||
|
||||
|
||||
Reference in New Issue
Block a user