import time


def evaluate(mnist, elbo, nll, x, args, sess):
    """
    Evaluate the model with ELBO and NLL over the test set
    """
    n_batches = mnist.batches_in_test
    sum_elbo = 0
    sum_nll = 0

    t_total = time.time()
    t0 = time.time()
    for i in range(n_batches):

        test_batch = mnist.next_test_batch()
        
        (batch_elbo, batch_nll) = sess.run((elbo, nll), {x: test_batch})
        sum_elbo += batch_elbo
        sum_nll += batch_nll

        if (i+1) % args.print_interval == 0:
            print(("Batch {0:d}/{1:d}; \tELBO: {2:.2f};"
                "\tNLL: {3:.2f};\ttime: {4:.2f}s").format(
                    i+1, n_batches, batch_elbo, batch_nll, time.time()-t0))
            t0 = time.time()

    test_elbo = sum_elbo / n_batches
    test_nll = sum_nll / n_batches
    print('====> Avg. ELBO: {0:.2f}, Avg. NLL: {1:.2f}, Time: {2:.2f}s'.
        format(test_elbo, test_nll, time.time()-t_total))

    return test_elbo, test_nll