code: def evaluate(mod, data_iter, epoch, log_interval):
      
      start = time.time()
      total_L = 0.0
      nbatch = 0
      density = 0
      mod.set_states(value=0)
      for batch in data_iter:
          mod.forward(batch, is_train=False)
          outputs = mod.get_outputs(merge_multi_context=False)
          states = outputs[:-1]
          total_L += outputs[-1][0]
          mod.set_states(states=states)
          nbatch += 1
          # don't include padding data in the test perplexity
          density += batch.data[1].mean()
          if (nbatch + 1) % log_interval == 0:
              logging.info("Eval batch %d loss : %.7f" % (nbatch, (total_L / density).asscalar()))
      data_iter.reset()
      loss = (total_L / density).asscalar()
      ppl = math.exp(loss) if loss < 100 else 1e37
      end = time.time()
      logging.info('Iter[%d]\t\t CE loss %.7f, ppl %.7f. Eval duration = %.2f seconds ' % \
                   (epoch, loss, ppl, end - start))
      return loss
docs: Run evaluation on cpu.