def learn()

in qlearn/toys/bootstrapped_agent.py [0:0]


    def learn(self, states, actions, rewards, next_states, terminals):
        self.online_net.train()
        self.target_net.eval()
        states = Variable(self.FloatTensor(states))
        actions = Variable(self.LongTensor(actions))
        next_states = Variable(self.FloatTensor(next_states))
        rewards = Variable(self.FloatTensor(rewards)).view(-1, 1)
        terminals = Variable(self.FloatTensor(terminals)).view(-1, 1)

        # import pdb
        # pdb.set_trace()
        # Compute Q(s_t, a) - the model computes Q(s_t), then we select the
        # columns of actions taken
        online_outputs = self.online_net(states)
        target_outputs = self.target_net(next_states)
        loss = 0
        # import pdb
        # pdb.set_trace()
        for k in range(self.nheads):
            state_action_values = online_outputs[k].gather(1, actions.view(-1, 1))

            # Compute V(s_{t+1}) for all next states.
            if self.double_q:
                next_actions = online_outputs[k].max(1)[1]
                next_state_values = target_outputs[k].gather(1, next_actions.view(-1, 1))
            else:
                next_state_values = target_outputs[k].max(1)[0].view(-1, 1)

            target_state_action_values = rewards + (1 - terminals) * self.discount * next_state_values.view(-1, 1)

            # Compute Huber loss
            loss += F.smooth_l1_loss(state_action_values, target_state_action_values.detach())
        # loss /= args.nheads
        # Optimize the model
        self.optimiser.zero_grad()
        loss.backward()
        for param in self.online_net.parameters():
            param.grad.data.clamp_(-1, 1)
        self.optimiser.step()
        return loss