对比TargetNetwork 和 Double DQN

1、Double DQN算法:

Selection using DQN:
a ⋆ = argmax ⁡ a Q ( s t + 1 , a ; w ) . a^{\star}=\operatorname*{argmax}_{a}Q(s_{t+1},a;\mathbf{w}). \\ a=aargmaxQ(st+1,a;w).
Evaluation using target network:
y t = r t + γ ⋅ Q ( s t + 1 , a ⋆ ; w − ) . y_{t}=r_{t}+\gamma\cdot Q(s_{t+1},a^{\star};\mathbf{w}^{-}). \\ yt=rt+γQ(st+1,a;w).


2、Target Network算法:

Selection using target network:
a ⋆ = argmax ⁡ a Q ( s t + 1 , a ; w − ) . a^{\star}=\operatorname*{argmax}_{a}Q(s_{t+1},a;\mathbf{w}^{-}). \\ a=aargmaxQ(st+1,a;w).
Evaluation using target network:
y t = r t + γ ⋅ Q ( s t + 1 , a ⋆ ; w − ) . y_{t}=r_{t}+\gamma\cdot Q(s_{t+1},a^{\star};\mathbf{w}^{-}). \\ yt=rt+γQ(st+1,a;w).


3、对比

在动作选择步骤不同,Double DQN使用DQN选择动作,Target Network使用target network选择,Double DQN更优,因为:
Q ( s t + 1 , a ⋆ ; w − ) ≤ max ⁡ a Q ( s t + 1 , a ; w − ) . Q(s_{t+1},a^\star;\mathbf{w}^-)\quad\leq\quad\max_aQ(s_{t+1},a;\mathbf{w}^-). Q(st+1,a;w)amaxQ(st+1,a;w).


4、算法实现:

class QNet(nn.Module):
    """QNet.
    Input: feature
    Output: num_act of values
    """

    def __init__(self, dim_obs, num_act):
        super().__init__()
        self.fc1 = nn.Linear(dim_obs, 64)
        self.fc2 = nn.Linear(64, 32)
        self.fc3 = nn.Linear(32, num_act)

    def forward(self, obs):
        x = F.relu(self.fc1(obs))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

class DoubleDQN:
    def __init__(self, dim_obs=None, num_act=None, discount=0.9):
        self.discount = discount
        self.model = QNet(dim_obs, num_act)
        self.target_model = QNet(dim_obs, num_act)
        self.target_model.load_state_dict(self.model.state_dict())

    def get_action(self, obs):
        qvals = self.model(obs)
        return qvals.argmax()

    def compute_loss(self, s_batch, a_batch, r_batch, d_batch, next_s_batch):
        # Compute current Q value based on current states and actions.
        qvals = self.model(s_batch).gather(1, a_batch.unsqueeze(1)).squeeze()
        # next state的value不参与导数计算,避免不收敛。
        next_qvals, _ = self.target_model(next_s_batch).detach().max(dim=1)
        loss = F.mse_loss(r_batch + self.discount * next_qvals * (1 - d_batch), qvals)
        return loss
    
class TargetNetworkDQN:
    def __init__(self, dim_obs=None, num_act=None, discount=0.9):
        self.discount = discount
        self.model = QNet(dim_obs, num_act)
        self.target_model = QNet(dim_obs, num_act)
        self.target_model.load_state_dict(self.model.state_dict())

    def get_action(self, obs):
        qvals = self.model(obs)
        return qvals.argmax()

    def compute_loss(self, s_batch, a_batch, r_batch, d_batch, next_s_batch):
        # Compute current Q value based on current states and actions.
        qvals = self.target_model(s_batch).gather(1, a_batch.unsqueeze(1)).squeeze()
        # next state的value不参与导数计算,避免不收敛。
        next_qvals, _ = self.target_model(next_s_batch).detach().max(dim=1)
        loss = F.mse_loss(r_batch + self.discount * next_qvals * (1 - d_batch), qvals)
        return loss    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KPer_Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值