in qlearn/atari/bayes_backprop_agent.py [0:0]
def learn(self, states, actions, rewards, next_states, terminals):
self.online_net.train()
self.target_net.eval()
self.online_net.reset_noise()
# self.target_net.reset_noise()
states = Variable(self.FloatTensor(states / 255.0))
actions = Variable(self.LongTensor(actions))
next_states = Variable(self.FloatTensor(next_states / 255.0))
rewards = Variable(self.FloatTensor(rewards)).view(-1, 1)
terminals = Variable(self.FloatTensor(terminals)).view(-1, 1)
# Compute Q(s_t, a) - the model computes Q(s_t), then we select the
# columns of actions taken
state_action_values = self.online_net(states).gather(1, actions.view(-1, 1))
if self.double_q:
next_actions = self.online_net(next_states).max(1)[1]
next_state_values = self.target_net(next_states).gather(1, next_actions.view(-1, 1))
else:
next_state_values = self.target_net(next_states).max(1)[0].view(-1, 1)
target_state_action_values = rewards + (1 - terminals) * self.discount * next_state_values
td_errors = F.smooth_l1_loss(state_action_values, target_state_action_values.detach())
# loss = td_errors
kl_reg = self.online_net.get_reg()
loss = td_errors + self.kl_coeff * kl_reg
# Optimize the model
self.optimiser.zero_grad()
loss.backward()
clip_grad_norm_(self.online_net.parameters(), 10)
# for param in self.online_net.parameters():
# param.grad.data.clamp_(-1, 1)
self.optimiser.step()
return td_errors, kl_reg, loss