code: def learn(self, batch_size):
      if self.prioritized:
          if not self.memory.is_full():
              return -np.inf
          indices, exps, weights = self.memory.sample(
              batch_size, self.beta
          )
      else:
          exps = self.memory.sample(batch_size)
      obss, actions, rewards, obs_nexts, dones = self._process(exps)
  
      dy.renew_cg()
      target_network = (
          self.target_network if self.use_double_dqn else self.network
      )
      if self.dueling:
          target_values, v = target_network(obs_nexts, batched=True)
          target_values = target_values.npvalue() + v.npvalue()
      else:
          target_values = target_network(obs_nexts, batched=True)
          target_values = target_values.npvalue()
      target_values = np.max(target_values, axis=0)
      target_values = rewards + self.reward_decay * (
          target_values * (1 - dones)
      )
  
      dy.renew_cg()
      if self.dueling:
          all_values_expr, v = self.network(obss, batched=True)
      else:
          all_values_expr = self.network(obss, batched=True)
      picked_values = dy.pick_batch(all_values_expr, actions)
      diff = (
          picked_values + v if self.dueling else picked_values
      ) - dy.inputTensor(target_values, batched=True)
      if self.prioritized:
          self.memory.update(
              indices, np.transpose(np.abs(diff.npvalue()))
          )
      losses = dy.pow(diff, dy.constant(1, 2))
      if self.prioritized:
          losses = dy.cmult(
              losses, dy.inputTensor(weights, batched=True)
          )
      loss = dy.sum_batches(losses)
      loss_value = loss.npvalue()
      loss.backward()
      self.trainer.update()
  
      self.epsilon = max(
          self.epsilon - self.epsilon_decrease, self.epsilon_lower
      )
      if self.prioritized:
          self.beta = min(self.beta + self.beta_increase, 1.0)
  
      self.learn_step += 1
      if (
          self.use_double_dqn
          and self.learn_step % self.n_replace_target == 0
      ):
          self.target_network.update(self.network)
      return loss_value
  
file_path: dynet/examples/reinforcement-learning/dqn.py
imports: import dynet
  import numpy
  from memory import Memory, PrioritizedMemory
label_desc: Use image node_id 4 for calling the DeepQNetwork obj's underlying member method code with example usage: obj.learn(batch_size) and returns: loss_value, unknown