In Mnih et al. (2013), every agent step samples a random minibatch of stored transitions from the replay memory and takes a gradient step on . The target comes from the same network, with the same weights , and is treated as a constant. Write that loss.
Implement dqn_loss(q_net, phi, actions, rewards, next_phi, done, gamma).
Inputs
q_net: any nn.Module that maps a batch of states of shape (B, ...) to Q-values of shape (B, K).phi, next_phi: float tensors of states, shape (B, ...).actions: a torch.long tensor of shape (B,), the actions taken.rewards: a float tensor (B,) of (clipped) rewards.done: a float tensor (B,) of and ; means is terminal.gamma: the discount factor.Target
computed with q_net itself and with no gradient flowing through it.
Output: the scalar loss tensor , still attached to the graph of so that loss.backward() gives the gradient used for the update.