Beispiel #1
0
def evaluate_conv(args, unique_entities):
    model_conv = SpKBGATConvOnly(entity_embeddings, relation_embeddings, args.entity_out_dim, args.entity_out_dim,
                                 args.drop_GAT, args.drop_conv, args.alpha, args.alpha_conv,
                                 args.nheads_GAT, args.out_channels)
    model_conv = nn.DataParallel(model_conv)

    if CUDA:
        model_conv.load_state_dict(torch.load(
            '{0}conv/trained_{1}.pth'.format(args.output_folder, args.epochs_conv - 1)))
        model_conv.cuda()
    else:
        model_conv.load_state_dict(torch.load(
            '{0}conv/trained_{1}.pth'.format(args.output_folder, args.epochs_conv - 1), map_location=torch.device('cpu')))
    model_conv.eval()
    with torch.no_grad():
        if isinstance(model_conv, nn.DataParallel):
            ### original code is get_validation_pred
            Corpus_.get_validation_pred_relation(args, model_conv.module, unique_entities)
        else:
            Corpus_.get_validation_pred_relation(args, model_conv, unique_entities)

        if isinstance(model_conv, nn.DataParallel):
            Corpus_.get_validation_pred(args, model_conv.module, unique_entities)
        else:
            Corpus_.get_validation_pred(args, model_conv, unique_entities)
Beispiel #2
0
def evaluate_conv(args, unique_entities, load_model):
    CUDA = torch.cuda.is_available()
    model_conv = SpKBGATConvOnly(entity_embeddings, relation_embeddings,
                                 args.entity_out_dim, args.entity_out_dim,
                                 args.drop_GAT, args.drop_conv, args.alpha,
                                 args.alpha_conv, args.nheads_GAT,
                                 args.out_channels)
    model_conv = nn.DataParallel(model_conv)
    if CUDA:
        model_conv.load_state_dict(torch.load(load_model))
        model_conv.cuda()
    else:
        model_conv.load_state_dict(torch.load(load_model),
                                   map_location=torch.device('cpu'))

    model_conv.eval()
    with torch.no_grad():
        if isinstance(model_conv, nn.DataParallel):
            Corpus_.get_validation_pred_relation(args, model_conv.module,
                                                 unique_entities)
        else:
            Corpus_.get_validation_pred_relation(args, model_conv,
                                                 unique_entities)

        if isinstance(model_conv, nn.DataParallel):
            Corpus_.get_validation_pred(args, model_conv.module,
                                        unique_entities)
        else:
            Corpus_.get_validation_pred(args, model_conv, unique_entities)
Beispiel #3
0
def evaluate_conv(args, unique_entities_train, unique_entities_test):

    global initial_entity_emb_params
    global entity_embeddings

    model_gat = SpKBGATModified(entity_embeddings, relation_embeddings,
                                args.entity_out_dim, args.entity_out_dim,
                                args.drop_GAT, args.alpha, args.nheads_GAT,
                                initial_entity_emb_params)
    model_gat.load_state_dict(
        torch.load('{0}trained_{1}.pth'.format(args.output_folder, 0)))

    model_entity_embedding = EntityEmbedding(                                  initial_entity_emb_params['entity_embed_dim_in'], \
                                  initial_entity_emb_params['hidden_dim_entity'], \
                                  initial_entity_emb_params['num_encoder_layers_entity'], \
                                  initial_entity_emb_params['is_bidirectional'], \
                                  initial_entity_emb_params['drop_out_rate'], \
                                  initial_entity_emb_params['hidden_dim_entity'], \
                                  initial_entity_emb_params['entity_embed_dim_out'], \
                                  initial_entity_emb_params['entity_conv_filter_size'],
                                  initial_entity_emb_params['word_vocab'], \
                                  initial_entity_emb_params['word_embed_dim'], \
                                  initial_entity_emb_params['char_embed_dim'], \
                                  initial_entity_emb_params['word_embed_matrix'], \
                                  initial_entity_emb_params['char_feature_size'], \
                                  initial_entity_emb_params['conv_filter_size'], \
                                  initial_entity_emb_params['max_word_len_entity'], \
                                  initial_entity_emb_params['char_vocab'])
    model_entity_embedding.load_state_dict(
        torch.load('{0}{1}/trained_{2}.pth'.format(args.output_folder,
                                                   'entity_embeddings', 0)))

    model_conv = SpKBGATConvOnly(entity_embeddings, relation_embeddings,
                                 args.entity_out_dim, args.entity_out_dim,
                                 args.drop_GAT, args.drop_conv, args.alpha,
                                 args.alpha_conv, args.nheads_GAT,
                                 args.out_channels)
    model_conv.load_state_dict(
        torch.load('{0}conv/trained_{1}.pth'.format(args.output_folder, 0)))

    if CUDA:
        model_conv.cuda()
        model_gat.cuda()
        model_entity_embedding.cuda()
    model_conv.eval()
    model_gat.eval()
    model_entity_embedding.eval()
    with torch.no_grad():
        Corpus_.get_validation_cnfmat(args,
                                      model_gat,
                                      model_entity_embedding,
                                      model_conv,
                                      unique_entities_train,
                                      unique_entities_test,
                                      reuse=False,
                                      gat_only=False)
Beispiel #4
0
def evaluate_conv(args, unique_entities):
    model_conv = SpKBGATConvOnly(entity_embeddings, relation_embeddings, args.entity_out_dim, args.entity_out_dim,
                                 args.drop_GAT, args.drop_conv, args.alpha, args.alpha_conv,
                                 args.nheads_GAT, args.out_channels)
    model_conv.load_state_dict(torch.load(
        '{0}conv/trained_{1}.pth'.format(args.output_folder, args.epochs_conv - 1)))

    model_conv.cuda()
    with torch.no_grad():
        Corpus_.get_validation_pred(args, model_conv, unique_entities)
Beispiel #5
0
def evaluate_conv(args, unique_entities):
    model_conv = SpKBGATConvOnly(entity_embeddings, relation_embeddings,
                                 args.entity_out_dim, args.entity_out_dim,
                                 args.drop_GAT, args.drop_conv, args.alpha,
                                 args.alpha_conv, args.nheads_GAT,
                                 args.out_channels)
    model_conv.load_state_dict(
        torch.load('{0}conv/trained_{1}.pth'.format(args.output_folder,
                                                    args.epochs_conv - 1)))

    model_conv.cuda()
    model_conv.eval()
    with torch.no_grad():
        preds = Corpus_.get_validation_pred(args, model_conv, unique_entities)

    data_path = "/home/harsha1/Desktop/pLogicNet/data/wn18rr/"
    entity2id = read_entity_from_id(data_path + 'entity2id.txt')
    relation2id = read_relation_from_id(data_path + 'relation2id.txt')

    id2entity = {v: k for k, v in entity2id.items()}
    id2relation = {v: k for k, v in relation2id.items()}

    is_unweigted = False
    directed = True
    work_path = args.work_path
    cand_triples, train_adjacency_mat, unique_entities_test = load_data1(
        os.path.join(work_path, 'hidden.txt'), entity2id, relation2id,
        is_unweigted, directed)

    scores = infer_step(model_conv, cand_triples, args)
    with open(work_path + '/annotation.txt', 'w') as fo:
        for (h, r, t), s in zip(cand_triples, scores):
            fo.write('{}\t{}\t{}\t{}\n'.format(id2entity[h], id2relation[r],
                                               id2entity[t], s))

    with open(work_path + '/pred_kge.txt', 'w') as fo:
        print(len(preds))
        i = 0
        for value in preds:
            if len(value) == 3:
                continue
            else:
                i = i + 1
                h, r, t, f, rk, l = value
                fo.write('{}\t{}\t{}\t{}\t{}\n'.format(id2entity[h],
                                                       id2relation[r],
                                                       id2entity[t], f, rk))
                #print(i)
                for e, val in l:
                    fo.write('{}:{:.12f} '.format(id2entity[e], val))
                fo.write('\n')
Beispiel #6
0
def evaluate_conv(args, unique_entities):
    model_conv = SpKBGATConvOnly(
        initial_entity_emb=entity_embeddings,
        initial_relation_emb=relation_embeddings,
        entity_out_dim=args.entity_out_dim,
        relation_out_dim=args.entity_out_dim,
        drop_GAT=args.drop_GAT,
        drop_conv=args.drop_conv,
        alpha=args.alpha,
        alpha_conv=args.alpha_conv,
        nheads_GAT=args.nheads_GAT,
        conv_out_channels=args.out_channels,
    )
    model_conv.load_state_dict(
        torch.load("{0}conv/trained_{1}.pth".format(args.output_folder,
                                                    args.epochs_conv - 1)),
        strict=False,
    )

    if CUDA:
        model_conv.cuda()
    model_conv.eval()
    with torch.no_grad():
        Corpus_.get_validation_pred(args, model_conv, unique_entities)
Beispiel #7
0
def train_conv(args):

    # Creating convolution model here.
    ####################################

    print("Defining model")
    if args.tanh:
        model_gat = SpKBGATModified(entity_embeddings, relation_embeddings, args.entity_out_dim, args.entity_out_dim,
                                    args.drop_GAT, args.alpha, args.nheads_GAT, 'tanh')
    else:
        model_gat = SpKBGATModified(entity_embeddings, relation_embeddings, args.entity_out_dim, args.entity_out_dim,
                                    args.drop_GAT, args.alpha, args.nheads_GAT, 'leakyrelu')
    print("Only Conv model trained")
    model_conv = SpKBGATConvOnly(entity_embeddings, relation_embeddings, args.entity_out_dim, args.entity_out_dim,
                                 args.drop_GAT, args.drop_conv, args.alpha, args.alpha_conv,
                                 args.nheads_GAT, args.out_channels)

    model_gat = nn.DataParallel(model_gat)
    model_conv = nn.DataParallel(model_conv)

    if CUDA:
        model_conv.cuda()
        model_gat.cuda()
    # load gat weights given pretrained
    if (args.load_gat is None) or (args.load_gat is not None and args.epochs_gat > 0):
        model_gat.load_state_dict(torch.load(
            '{0}gat/trained_{1}.pth'.format(args.output_folder, args.epochs_gat - 1)))
    else:
        model_gat.load_state_dict(torch.load(args.load_gat))

    if isinstance(model_conv, nn.DataParallel):
        if args.load_conv is None:
            model_conv.module.final_entity_embeddings = model_gat.module.final_entity_embeddings
            model_conv.module.final_relation_embeddings = model_gat.module.final_relation_embeddings
        else:
            model_conv.load_state_dict(torch.load(args.load_conv))
    else:
        model_conv.final_entity_embeddings = model_gat.final_entity_embeddings
        model_conv.final_relation_embeddings = model_gat.final_relation_embeddings

    Corpus_.batch_size = args.batch_size_conv
    Corpus_.invalid_valid_ratio = int(args.valid_invalid_ratio_conv)

    optimizer = torch.optim.Adam(
        model_conv.parameters(), lr=args.lr, weight_decay=args.weight_decay_conv)

    scheduler = torch.optim.lr_scheduler.StepLR(
        optimizer, step_size=25, gamma=0.5, last_epoch=-1)

    margin_loss = torch.nn.SoftMarginLoss()

    epoch_losses = []   # losses of all epochs
    print("Number of epochs {}".format(args.epochs_conv))

    for epoch in range(args.epochs_conv):
        print("\nepoch-> ", epoch)
        random.shuffle(Corpus_.train_triples)
        Corpus_.train_indices = np.array(
            list(Corpus_.train_triples)).astype(np.int32)

        model_conv.train()  # getting in training mode
        start_time = time.time()
        epoch_loss = []

        if len(Corpus_.train_indices) % args.batch_size_conv == 0:
            num_iters_per_epoch = len(
                Corpus_.train_indices) // args.batch_size_conv
        else:
            num_iters_per_epoch = (
                len(Corpus_.train_indices) // args.batch_size_conv) + 1

        for iters in range(num_iters_per_epoch):
            start_time_iter = time.time()
            train_indices, train_values = Corpus_.get_iteration_batch(iters)

            if CUDA:
                train_indices = Variable(
                    torch.LongTensor(train_indices)).cuda()
                train_values = Variable(torch.FloatTensor(train_values)).cuda()

            else:
                train_indices = Variable(torch.LongTensor(train_indices))
                train_values = Variable(torch.FloatTensor(train_values))

            preds = model_conv(
                Corpus_, Corpus_.train_adj_matrix, train_indices)

            optimizer.zero_grad()

            loss = margin_loss(preds.view(-1), train_values.view(-1))

            loss.backward()
            for param in model_conv.parameters():
                param.grad.data.clamp_(-1, 1)
            optimizer.step()

            epoch_loss.append(loss.data.item())

            end_time_iter = time.time()
            line = "Iteration-> {0}  , Iteration_time-> {1:.4f} , Iteration_loss {2:.4f}".format(
                iters, end_time_iter - start_time_iter, loss.data.item())
            print(line)
            write_to_file(args, line)

        scheduler.step()
        line = "Epoch {} , average loss {} , epoch_time {}".format(
            epoch, sum(epoch_loss) / len(epoch_loss), time.time() - start_time)
        epoch_losses.append(sum(epoch_loss) / len(epoch_loss))
        print(line)
        write_to_file(args, line)

        save_model(model_conv, args.data, epoch,
                   args.output_folder + "conv/")