def test(dataset, args, all_hash, modelD, tflogger, subsample=1):
    l_ranks, r_ranks = [], []
    test_loader = DataLoader(dataset, num_workers=1, collate_fn=collate_fn)

    cst_inds = np.arange(args.num_ent, dtype=np.int64)[:, None]

    if args.show_tqdm:
        data_itr = tqdm(enumerate(test_loader))
    else:
        data_itr = enumerate(test_loader)

    for idx, triplet in data_itr:
        if idx % subsample != 0:
            continue

        lhs, rel, rhs = triplet.view(-1)

        l_batch = np.concatenate(
            [cst_inds,
             np.array([[rel, rhs]]).repeat(args.num_ent, axis=0)],
            axis=1)
        r_batch = np.concatenate(
            [np.array([[lhs, rel]]).repeat(args.num_ent, axis=0), cst_inds],
            axis=1)

        l_fns = np.array([int(x.tobytes() in all_hash) for x in l_batch],
                         dtype=np.float32)
        r_fns = np.array([int(x.tobytes() in all_hash) for x in r_batch],
                         dtype=np.float32)

        l_batch = ltensor(l_batch).contiguous()
        r_batch = ltensor(r_batch).contiguous()

        if args.use_cuda:
            l_batch = l_batch.cuda()
            r_batch = r_batch.cuda()

        l_batch = Variable(l_batch)
        r_batch = Variable(r_batch)

        d_ins = torch.cat([l_batch, r_batch], dim=0)
        d_outs = modelD(d_ins)
        l_enrgs = d_outs[:len(l_batch)]
        r_enrgs = d_outs[len(l_batch):]

        l_rank = compute_rank(v2np(l_enrgs), lhs, mask_observed=l_fns)
        r_rank = compute_rank(v2np(r_enrgs), rhs, mask_observed=r_fns)

        l_ranks.append(l_rank)
        r_ranks.append(r_rank)

    l_ranks = np.array(l_ranks)
    r_ranks = np.array(r_ranks)

    return l_ranks, r_ranks
Esempio n. 2
0
    def test(dataset):
        l_ranks, r_ranks = [], []
        test_loader = DataLoader(dataset, num_workers=1, collate_fn=collate_fn)
        cst_inds = np.arange(args.num_ent, dtype=np.int64)[:, None]

        for idx, triplet in tqdm(enumerate(test_loader)):
            lhs, rel, rhs = triplet.view(-1)

            l_batch = np.concatenate([
                cst_inds,
                np.array([[rel, rhs]]).repeat(args.num_ent, axis=0)
            ],
                                     axis=1)
            r_batch = np.concatenate([
                np.array([[lhs, rel]]).repeat(args.num_ent, axis=0), cst_inds
            ],
                                     axis=1)

            l_batch = ltensor(l_batch).contiguous()
            r_batch = ltensor(r_batch).contiguous()

            if args.use_cuda:
                l_batch = l_batch.cuda()
                r_batch = r_batch.cuda()

            l_batch = Variable(l_batch)
            r_batch = Variable(r_batch)

            l_enrgs = modelD(l_batch)
            r_enrgs = modelD(r_batch)

            l_rank = compute_rank(v2np(l_enrgs), lhs)
            r_rank = compute_rank(v2np(r_enrgs), rhs)

            l_ranks.append(l_rank)
            r_ranks.append(r_rank)

        l_ranks = np.array(l_ranks)
        r_ranks = np.array(r_ranks)

        return l_ranks, r_ranks