深度强化学习 - CartPole 问题

Posted

技术标签:

【中文标题】深度强化学习 - CartPole 问题【英文标题】:Deep Reinforcement Learning - CartPole Problem 【发布时间】:2021-08-13 23:03:57 【问题描述】:

我尝试实现最简单的深度 Q 学习算法。我认为,我已经正确地实施了它,并且知道深度 Q 学习与分歧作斗争,但回报下降得非常快,损失也在分歧。如果有人可以帮助我指出正确的超参数,或者我是否错误地实现了算法,我将不胜感激。我尝试了很多超参数组合,也改变了 QNet 的复杂性。

import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
import collections
import numpy as np
import matplotlib.pyplot as plt
import gym
from torch.nn.modules.linear import Linear
from torch.nn.modules.loss import MSELoss


class ReplayBuffer:
  def __init__(self, max_replay_size, batch_size):
    self.max_replay_size = max_replay_size
    self.batch_size      = batch_size
    self.buffer          = collections.deque()


def push(self, *transition):
    if len(self.buffer) == self.max_replay_size:
        self.buffer.popleft()
    self.buffer.append(transition)


def sample_batch(self):
    indices = np.random.choice(len(self.buffer), self.batch_size, replace = False)
    batch   = [self.buffer[index] for index in indices]
    
    state, action, reward, next_state, done = zip(*batch)
    
    state      = np.array(state)
    action     = np.array(action)
    reward     = np.array(reward)
    next_state = np.array(next_state)
    done       = np.array(done)
    
    return state, action, reward, next_state, done


def __len__(self):
    return len(self.buffer)


class QNet(nn.Module):
  def __init__(self, state_dim, action_dim):
    super(QNet, self).__init__()

    self.linear1 = Linear(in_features = state_dim, out_features = 64)
    self.linear2 = Linear(in_features = 64, out_features = action_dim)


  def forward(self, x):
    x = self.linear1(x)
    x = F.relu(x)
    x = self.linear2(x)
    return x


def train(replay_buffer, model, target_model, discount_factor, mse, optimizer):
  state, action, reward, next_state, _ = replay_buffer.sample_batch()
  state, next_state = torch.tensor(state, dtype = torch.float), torch.tensor(next_state, 
  dtype = torch.float)

  # Compute Q Value and Target Q Value
  q_values = model(state).gather(1, torch.tensor(action, dtype = torch.int64).unsqueeze(-1))

  with torch.no_grad():
    max_next_q_values = target_model(next_state).detach().max(1)[0]
    q_target_value = torch.tensor(reward, dtype = torch.float) + discount_factor * 
                     max_next_q_values

  optimizer.zero_grad()
  loss = mse(q_values, q_target_value.unsqueeze(1))
  loss.backward()
  optimizer.step()

  return loss.item()


def main():
  # Define Hyperparameters and Parameters
  EPISODES        = 10000
  MAX_REPLAY_SIZE = 10000
  BATCH_SIZE      = 32
  EPSILON         = 1.0
  MIN_EPSILON     = 0.05
  DISCOUNT_FACTOR = 0.95
  DECAY_RATE      = 0.99
  LEARNING_RATE   = 1e-3
  SYNCHRONISATION = 33
  EVALUATION      = 32

  # Initialize Environment, Model, Target-Model, Optimizer, Loss Function and Replay Buffer
  env = gym.make("CartPole-v0")

  model        = QNet(state_dim = env.observation_space.shape[0], action_dim = 
                 env.action_space.n)
  target_model = QNet(state_dim = env.observation_space.shape[0], action_dim = 
                 env.action_space.n)
  target_model.load_state_dict(model.state_dict())

  optimizer = optim.Adam(model.parameters(), lr = LEARNING_RATE)
  mse       = MSELoss()

  replay_buffer = ReplayBuffer(max_replay_size = MAX_REPLAY_SIZE, batch_size = BATCH_SIZE)

  while len(replay_buffer) != MAX_REPLAY_SIZE:
    state = env.reset()
    done  = False
    while done != True:
        action = env.action_space.sample()

        next_state, reward, done, _ = env.step(action)

        replay_buffer.push(state, action, reward, next_state, done)

        state = next_state

  # Begin with the Main Loop where the QNet is trained
  count_until_synchronisation = 0
  count_until_evaluation      = 0
  history = 'Episode': [], 'Reward': [], 'Loss': []
  for episode in range(EPISODES):
    total_reward = 0.0
    total_loss   = 0.0
    state        = env.reset()
    iterations   = 0
    done         = False
    while done != True:
        count_until_synchronisation += 1
        count_until_evaluation      += 1

        # Take an action
        if np.random.rand(1) < EPSILON:
            action = env.action_space.sample()
        else:
            with torch.no_grad():
                output = model(torch.tensor(state, dtype = torch.float)).numpy()
            action = np.argmax(output)

        # Observe new state and reward + store into replay_buffer
        next_state, reward, done, _ = env.step(action)
        total_reward += reward

        replay_buffer.push(state, action, reward, next_state, done)

        state = next_state

        if count_until_synchronisation % SYNCHRONISATION == 0:
            target_model.load_state_dict(model.state_dict())

        if count_until_evaluation % EVALUATION == 0:
            loss = train(replay_buffer = replay_buffer, model = model, target_model = 
                         target_model, discount_factor = DISCOUNT_FACTOR,
                         mse = mse, optimizer = optimizer)
            total_loss += loss

        iterations += 1

    print (f"Episode episode is concluded in iterations iterations with a total reward 
           of total_reward")

    if EPSILON > MIN_EPSILON:
        EPSILON *= DECAY_RATE

    history['Episode'].append(episode)
    history['Reward'].append(total_reward)
    history['Loss'].append(total_loss)

# Plot the Loss + Reward per Episode
fig, ax = plt.subplots(figsize = (10, 6))
ax.plot(history['Episode'], history['Reward'], label = "Reward")
ax.set_xlabel('Episodes', fontsize = 15)
ax.set_ylabel('Total Reward per Episode', fontsize = 15)
plt.legend(prop = 'size': 15)
plt.show()

fig, ax = plt.subplots(figsize = (10, 6))
ax.plot(history['Episode'], history['Loss'], label = "Loss")
ax.set_xlabel('Episodes', fontsize = 15)
ax.set_ylabel('Total Loss per Episode', fontsize = 15)
plt.legend(prop = 'size': 15)
plt.show()


if __name__ == "__main__":
  main()

【问题讨论】:

【参考方案1】:

您的代码看起来不错,我认为您的超参数并不理想。我会改变两件事,可能是三件事:

如果我没记错的话,您每 32 步更新一次目标网络。我认为这太低了。在original paper by Mnih et al. 中,他们每 10k 步进行一次硬更新。想一想:目标网络是用来计算损失的,你基本上每 32 步更改一次损失函数,这将超过每集一次。 您的回放缓冲区非常小。我会将其设置为 100k 或 1M,即使这比您打算训练的时间长。如果重播缓冲区太小,您将丢失较旧的转换,这可能导致您的网络“忘记”它已经学到的东西。不确定这对 cartpole 有多大影响,但也许值得一试... 学习率也可能更低,我正在使用 1-e4 和 RMSProp。一般来说,更改优化器也会产生不同的结果。

希望这有帮助,祝你好运:)

【讨论】:

【参考方案2】:

你的代码看起来不错,写得很好,超参数看起来很合理(除了更新频率可能太低),我认为 Q 网络很小,只有一个密集层。

更深的模型可能会做得更好(虽然可能不超过 3-4 层),但您说您已经尝试了不同的网络大小。

想到的另一件事是目标更新。您每 n 步进行一次硬更新;软更新可能会有所帮助,但我不会指望它。

您也可以尝试稍微降低学习率,但我想您已经这样做了。


我的建议是:

尝试降低目标更新频率 如果您还没有尝试过更大的(更深的,例如 2/3 密集层和 32 个节点) 查看软目标更新(polyak 平均等) 在其他简单的健身房环境中尝试您的实施,并检查其行为是否仍然相同。

遗憾的是,DQN 并不理想,对于很多问题不会收敛,但它应该能够解决 cartpole。

【讨论】:

以上是关于深度强化学习 - CartPole 问题的主要内容,如果未能解决你的问题,请参考以下文章

强化学习系列12:使用julia训练深度强化模型

PyTorch强化学习实战——强化学习环境配置与PyTorch基础

DQN 处理 CartPole 问题——使用强化学习,本质上是训练MLP,预测每一个动作的得分

Actor-critic强化学习方法应用于CartPole-v1

强化学习基于tensorflow2.x 的 PPO2(离散动作情况) 训练 CartPole-v1

Keras深度学习实战(43)——深度Q学习算法