def test(ctx, data_iter, best, mode='validation', num_iter=(- 1)):
    data_iter.reset()
    batches = len(data_iter)
    data_iter.set_context(ctx[0])
    preds = []
    labels = [mx.nd.array(data_iter.labels, ctx=ctx[0]).reshape(((- 1), 1))]
    for _ in tqdm(range(batches), desc='Testing in {} mode'.format(mode)):
        (l_tree, l_sent, r_tree, r_sent, label) = data_iter.next()
        z = net(mx.nd, l_sent, r_sent, l_tree, r_tree)
        preds.append(z)
    preds = to_score(mx.nd.concat(*preds, dim=0))
    metric.update(preds, labels)
    (names, values) = metric.get()
    metric.reset()
    for (name, acc) in zip(names, values):
        logging.info((mode + (' acc: %s=%f' % (name, acc))))
        if (name == 'pearsonr'):
            test_r = acc
    if ((mode == 'validation') and (num_iter >= 0)):
        if (test_r >= best):
            best = test_r
            logging.info('New optimum found: {}. Checkpointing.'.format(best))
            net.collect_params().save('childsum_tree_lstm_{}.params'.format(num_iter))
            test(ctx, test_iter, (- 1), 'test')
        return best
def test(ctx, data_iter, best, mode='validation', num_iter=(- 1)):
    data_iter.reset()
    batches = len(data_iter)
    data_iter.set_context(ctx[0])
    preds = []
    labels = [mx.nd.array(data_iter.labels, ctx=ctx[0]).reshape(((- 1), 1))]
    for _ in tqdm(range(batches), desc='Testing in {} mode'.format(mode)):
        (l_tree, l_sent, r_tree, r_sent, label) = data_iter.next()
        z = net(mx.nd, l_sent, r_sent, l_tree, r_tree)
        preds.append(z)
    preds = to_score(mx.nd.concat(*preds, dim=0))
    metric.update(preds, labels)
    (names, values) = metric.get()
    metric.reset()
    for (name, acc) in zip(names, values):
        logging.info((mode + (' acc: %s=%f' % (name, acc))))
        if (name == 'pearsonr'):
            test_r = acc
    if ((mode == 'validation') and (num_iter >= 0)):
        if (test_r >= best):
            best = test_r
            logging.info('New optimum found: {}. Checkpointing.'.format(best))
            net.collect_params().save('childsum_tree_lstm_{}.params'.format(num_iter))
            test(ctx, test_iter, (- 1), 'test')
        return best