[英]Why DQN for cartpole game has a ascending reward while loss is not descending?
我寫了一個 DQN 來使用 TensorFlow 和 tf_agents 玩 OpenAI 健身房推桿游戲。 代碼如下所示:
def compute_avg_return(environment, policy, num_episodes=10):
total_return = 0.0
for _ in range(num_episodes):
time_step = environment.reset()
episode_return = 0.0
while not time_step.is_last():
action_step = policy.action(time_step)
time_step = environment.step(action_step.action)
episode_return += time_step.reward
total_return += episode_return
avg_return = total_return / num_episodes
return avg_return.numpy()[0]
def collect_step(environment, policy, buffer):
time_step = environment.current_time_step()
action_step = policy.action(time_step)
next_time_step = environment.step(action_step.action)
traj = trajectory.from_transition(time_step, action_step, next_time_step)
buffer.add_batch(traj)
def collect_data(env, policy, buffer, steps):
for _ in range(steps):
collect_step(env, policy, buffer)
def train_model(
num_iterations=config.default_num_iterations,
collect_steps_per_iteration=config.default_collect_steps_per_iteration,
replay_buffer_max_length=config.default_replay_buffer_max_length,
batch_size=config.default_batch_size,
learning_rate=config.default_learning_rate,
log_interval=config.default_log_interval,
num_eval_episodes=config.default_num_eval_episodes,
eval_interval=config.default_eval_interval,
checkpoint_saver_directory=config.default_checkpoint_saver_directory,
model_saver_directory=config.default_model_saver_directory,
visualize=False,
static_plot=False,
):
env_name = 'CartPole-v0'
train_py_env = suite_gym.load(env_name)
eval_py_env = suite_gym.load(env_name)
train_env = tf_py_environment.TFPyEnvironment(train_py_env)
eval_env = tf_py_environment.TFPyEnvironment(eval_py_env)
fc_layer_params = (100,)
q_net = q_network.QNetwork(
train_env.observation_spec(),
train_env.action_spec(),
fc_layer_params=fc_layer_params)
optimizer = Adam(learning_rate=learning_rate)
train_step_counter = tf.Variable(0)
agent = dqn_agent.DqnAgent(
train_env.time_step_spec(),
train_env.action_spec(),
q_network=q_net,
optimizer=optimizer,
td_errors_loss_fn=common.element_wise_squared_loss,
train_step_counter=train_step_counter)
agent.initialize()
replay_buffer = tf_uniform_replay_buffer.TFUniformReplayBuffer(
data_spec=agent.collect_data_spec,
batch_size=train_env.batch_size,
max_length=replay_buffer_max_length)
dataset = replay_buffer.as_dataset(
num_parallel_calls=3,
sample_batch_size=batch_size,
num_steps=2).prefetch(3)
iterator = iter(dataset)
agent.train_step_counter.assign(0)
avg_return = compute_avg_return(eval_env, agent.policy, num_eval_episodes)
returns = []
loss = []
for _ in range(num_iterations):
for _ in range(collect_steps_per_iteration):
collect_step(train_env, agent.collect_policy, replay_buffer)
experience, unused_info = next(iterator)
train_loss = agent.train(experience).loss
step = agent.train_step_counter.numpy()
avg_return = compute_avg_return(eval_env, agent.policy, num_eval_episodes)
returns.append(avg_return)
雖然平均獎勵越來越好,達到了最高分200,最終損失並沒有明顯減少。
這是損失 plot:
這是獎勵plot:
好的一點是model是成功的,它可以很好地玩游戲。 但是,我真的很想了解為什么會發生這種情況,而極高的損失仍然會產生良好的回報。
這可能與您的 Q 值的規模有關。 我的 DQN 損失有相同的行為,我的代理很容易解決環境問題,但損失正在通過訓練增加。
如果您查看 DQN 算法的這一部分,您可能會得到一些見解:
希望這會有所幫助,請注意,這是一個純粹直觀和個人的解釋,我沒有進行任何測試來檢查我的假設。 我認為第二點可能是最重要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.