Beispiel #1
0
def test(model, config, loss_fun, flag, target_idx, print_attention=False):
    model.eval()
    corpus = config.corpus
    all_loss = 0.0
    sample_len = 0
    y_true_stances = np.array([])
    y_pred_stances = np.array([])

    for id_, (idx_tweets, idx_target, stances,
              target_idx) in enumerate(corpus.iter_epoch(target_idx, flag)):
        np_idx_tweets = fixed_length(idx_tweets, Config.fixed_len)
        np_idx_targets = fixed_length(idx_target, len(idx_target[0]))
        sample_len += len(stances)

        y_true_stances = np.append(y_true_stances, stance2idx(stances))

        var_s1 = Variable(torch.from_numpy(stance2idx(stances)))
        output1, attention_matrix = model(np_idx_tweets, np_idx_targets,
                                          target_idx)

        y_pred_stances = np.append(
            y_pred_stances,
            torch.max(output1, dim=1, keepdim=False)[1].data.numpy())
        loss = loss_fun(output1, var_s1)
        all_loss += loss.data.sum()
        if print_attention:
            print_attention_fun(config, idx_tweets, stances, y_pred_stances,
                                attention_matrix.data.numpy())
    f1_target1 = metric(y_true=y_true_stances,
                        y_pred=y_pred_stances,
                        name="target1")
    print(flag + " mean loss: ", all_loss / sample_len)
    print("f1: %f" % f1_target1)
    return f1_target1, y_true_stances, y_pred_stances
def train(model, config, loss_fun, optim, target_idx):
    model.train()
    corpus = config.corpus
    all_loss = 0.0
    sample_len = 0
    for id_, (idx_tweets, idx_target, stances, sentiments, target_idx) in enumerate(corpus.iter_epoch(target_idx, "Train", batch_size=16)):
    #for id_, (idx_tweets, idx_target, stances, sentiments, target_idx) in enumerate(corpus.iter_all_train_target(batch_size=30, is_random=False)):

        np_idx_tweets = fixed_length(idx_tweets, Config.fixed_len)
        np_idx_targets = fixed_length(idx_target, len(idx_target[0]))
        sample_len += len(idx_tweets)

        var_s1 = Variable(torch.from_numpy(stance2idx(stances)))
        var_s2 = Variable(torch.from_numpy(stance2idx(sentiments)))
        output1, output2 = model(np_idx_tweets, np_idx_targets, target_idx)

        #loss = loss_fun(output1, var_s1) + loss_fun(output2, var_s2)
        #loss = loss_fun(output1, var_s1)
        #loss = loss_fun(output2, var_s2)
        #train - independed

        loss = loss_fun(output1, var_s1)
        all_loss += loss.data.sum()
        optim.zero_grad()
        loss.backward()
        optim.step()
    #judge()
    print("-----------train mean loss: ", all_loss/sample_len)
    return model.state_dict()
Beispiel #3
0
def test(model, config, loss_fun, flag, target1, target2, pair_idx):
    model.eval()
    corpus = config.corpus
    all_loss = 0.0
    sample_len = 0
    y_true_t1 = np.array([])
    y_pred_t1 = np.array([])

    y_true_t2 = np.array([])
    y_pred_t2 = np.array([])

    for id_, (idx_tweets, s1, s2, pair_idx) in enumerate(
            corpus.iter_epoch(target1, target2, flag, pair_idx,
                              batch_size=64)):
        np_idx_tweets = fixed_length(idx_tweets, Config.fixed_len)
        sample_len += len(s1)

        y_true_t1 = np.append(y_true_t1, stance2idx(s1))
        y_true_t2 = np.append(y_true_t2, stance2idx(s2))

        var_s1 = Variable(torch.from_numpy(stance2idx(s1)))
        var_s2 = Variable(torch.from_numpy(stance2idx(s2)))
        output1, output2 = model(np_idx_tweets, pair_idx)

        y_pred_t1 = np.append(
            y_pred_t1,
            torch.max(output1, dim=1, keepdim=False)[1].data.numpy())
        y_pred_t2 = np.append(
            y_pred_t2,
            torch.max(output2, dim=1, keepdim=False)[1].data.numpy())
        loss = loss_fun(output1, var_s1) + loss_fun(output2, var_s2)
        all_loss += loss.data.sum()
    f1_target1 = metric(y_true=y_true_t1, y_pred=y_pred_t1, name="target1")
    f1_target2 = metric(y_true=y_true_t2, y_pred=y_pred_t2, name="target2")
    print(flag + " mean loss: ", all_loss / sample_len)
    average_f1 = (f1_target1 + f1_target2) / 2
    print("%s average f1 score %f" % (flag, average_f1))
    return f1_target1
Beispiel #4
0
def train(model, config, epoch, loss_fun, optim, target1, target2, pair_idx):
    model.train()
    corpus = config.corpus
    all_loss = 0.0
    sample_len = 0
    for id_, (idx_tweets, s1, s2,
              pair_idx) in enumerate(corpus.iter_train_epoch()):
        #for id_, (idx_tweets, s1, s2, pair_idx) in enumerate(corpus.iter_epoch(target1, target2, flag="Train", pair_idx=pair_idx)):
        np_idx_tweets = fixed_length(idx_tweets, Config.fixed_len)
        sample_len += len(s1)

        var_s1 = Variable(torch.from_numpy(stance2idx(s1)))
        var_s2 = Variable(torch.from_numpy(stance2idx(s2)))
        output1, output2 = model(np_idx_tweets, pair_idx)

        #loss = loss_fun(output1, var_s1) + loss_fun(output2, var_s2)
        #train - independed
        loss = loss_fun(output1, var_s1)
        all_loss += loss.data.sum()
        optim.zero_grad()
        loss.backward()
        optim.step()
    print("-----------train mean loss: ", all_loss / sample_len)