Exemplo n.º 1
0
def blending():
    # saved = True if args.saved != 0 else False
    saved = args.saved
    test_file1 = ROOT_DIR + "/data/test.txt"
    test_file2 = ROOT_DIR + "/data/test_predict_aspect_ensemble.txt"
    test_texts, test_aspects = load_ab_test(test_file1, test_file2)
    # print(test_aspects)

    word2index = pickle.load(open(ROOT_DIR + "/data/vocabulary.pkl", 'rb'))

    f_dict = ROOT_DIR + "/dataset/polarity.json"
    polarity_list, polarity_dict = parse_json(f_dict)
    f_dict2 = ROOT_DIR + "/dataset/attribute.json"
    attr_list, attr_dict = parse_json(f_dict2)

    paths = args.test_dir.split('#')
    models_files = []
    for path in paths:
        models_files.append([
            os.path.join(path, f) for f in os.listdir(path)
            if os.path.isfile(os.path.join(path, f))
        ])
    test_data = Data3((test_texts, None, test_aspects),
                      word2index,
                      polarity_dict,
                      args,
                      target_dict=attr_dict)
    if args.use_elmo != 0:
        test_elmo = load_elmo(test_texts)
        test_data.add_feature(test_elmo)

    x_test = []
    for dir, checkpoints_per_model in zip(paths, models_files):
        print(dir, checkpoints_per_model)
        if saved == 1:
            oof_test = load_oof_test(dir)
        else:
            clfs = checkpoints_per_model
            oof_test = get_oof_test(clfs, test_data)
        x_test.append(oof_test)
    x_test = np.stack(x_test, axis=1)
    print(x_test)
    print(x_test.shape)
    test_predict = np.mean(x_test, axis=1)
    fw = codecs.open(ROOT_DIR + "/data/test_predict_polarity_ensemble.txt",
                     'w',
                     encoding='utf-8')
    for j, prob in enumerate(test_predict):
        polarity = np.argmax(prob) - 1
        fw.write(test_aspects[j] + ',' + str(polarity) + '\n')
    time_stamp = time.asctime().replace(':', '_').split()
    fw.close()
    shutil.copy2(
        ROOT_DIR + "/data/test_predict_polarity_ensemble.txt", ROOT_DIR +
        "/data/backup/test_predict_polarity_ensemble_%s.txt" % time_stamp)
Exemplo n.º 2
0
def test():
    # model = Classifier()
    test_file1 = ROOT_DIR + "/attribute_level/data/attribute_test.txt"
    test_file2 = ROOT_DIR + "/attribute_level/test_predict.txt"
    test_texts, test_aspects = load_ab_test(test_file1, test_file2)
    f_w2v = ROOT_DIR + "/embedding/embedding_all_merge_300.txt"
    W, word2index = load_w2v(f_w2v)

    f_dict1 = ROOT_DIR + "/dataset/polarity.json"
    f_dict2 = ROOT_DIR + "/dataset/attribute.json"
    polarity_list, polarity_dict = parse_json(f_dict1)
    attr_list, attr_dict = parse_json(f_dict2)

    assert len(test_texts) == len(test_aspects)

    files = ["checkpoint_HEAT_0.7189.pt", "checkpoint_HEAT_0.7062.pt"]

    predicts = []
    for check_point in files:
        predict = []
        classifier = torch.load(check_point)
        for text, aspect in zip(test_texts, test_aspects):
            if aspect != '':
                if aspect is None:
                    print("error")
                test_data = Data3(([text], [None], [aspect]),
                                  word2index,
                                  polarity_dict,
                                  args,
                                  target_dict=attr_dict)
                test_predict = train_single.predict(classifier, test_data,
                                                    args)
                assert len(test_predict) == 1
                polarity = str(test_predict[0].item() - 1)
            else:
                print(aspect)
                print(text)
                polarity = '0'
            # fw.write(aspect+','+polarity+'\n')
            predict.append(aspect + ',' + polarity)
        predicts.append(predict)
    print(len(predicts))
    print(len(predicts[0]))
    fw = codecs.open("test_predict_polarity_ensemble.txt",
                     'w',
                     encoding='utf-8')

    for j in range(len(predicts[0])):
        votes = [predicts[i][j] for i in range(len(predicts))]
        voted = Counter(votes).most_common(1)
        fw.write(voted + '\n')
Exemplo n.º 3
0
def stacking():
    # saved = True if args.saved != 0 else False
    saved = args.saved
    f_train = ROOT_DIR + "/data/train.txt"
    test_file1 = ROOT_DIR + "/data/test.txt"
    test_file2 = ROOT_DIR + "/data/test_predict_aspect_ensemble.txt"
    test_texts, test_aspects = load_ab_test(test_file1, test_file2)
    # print(test_aspects)

    fo = load_abp_raw(f_train)
    word2index = pickle.load(open(ROOT_DIR + "/data/vocabulary.pkl", 'rb'))

    f_dict = ROOT_DIR + "/dataset/polarity.json"
    polarity_list, polarity_dict = parse_json(f_dict)
    f_dict2 = ROOT_DIR + "/dataset/attribute.json"
    attr_list, attr_dict = parse_json(f_dict2)

    paths = args.test_dir.split('#')
    models_files = []
    for path in paths:
        path = BASE_DIR + '/' + path
        models_files.append([
            os.path.join(path, f) for f in os.listdir(path)
            if os.path.isfile(os.path.join(path, f))
        ])
    test_data = Data3((test_texts, None, test_aspects),
                      word2index,
                      polarity_dict,
                      args,
                      target_dict=attr_dict)
    if args.use_elmo != 0:
        test_elmo = load_elmo(test_texts)
        test_data.add_feature(test_elmo)

    x_train = []
    y_train = []
    x_test = []
    for dir, checkpoints_per_model in zip(paths, models_files):
        print(dir, checkpoints_per_model)
        dir = BASE_DIR + '/' + dir
        if saved == 1:
            oof_train, oof_train_y, oof_test = load_oof_dir(dir)
        else:
            print(checkpoints_per_model)
            NFOLDS = len(checkpoints_per_model)
            print(NFOLDS)
            # assert NFOLDS == args.folds
            clfs = [None for i in range(NFOLDS)]
            for cp in checkpoints_per_model:
                fold = int(cp.replace('_', '.').split('.')[-2])
                clfs[fold - 1] = cp
            if saved == 2:
                oof_train, oof_train_y, oof_test = load_oof(
                    clfs,
                    fo,
                    test_data,
                    word2index,
                    polarity_dict=polarity_dict,
                    attr_dict=attr_dict)
            elif saved == 3:
                oof_train, oof_train_y, oof_test = load_oof3(
                    clfs,
                    fo,
                    test_data,
                    word2index,
                    polarity_dict=polarity_dict,
                    attr_dict=attr_dict)
            elif saved == 0:
                oof_train, oof_train_y, oof_test = get_oof(
                    clfs,
                    fo,
                    test_data,
                    word2index,
                    polarity_dict=polarity_dict,
                    attr_dict=attr_dict)
            else:
                print("saved error, [0:3]")
                exit(-1)
        x_train.append(oof_train)
        oof_train_y = oof_train_y.reshape(oof_train_y.shape[0], )
        if y_train == []:
            y_train = oof_train_y
        else:
            assert (y_train == oof_train_y).all()
        x_test.append(oof_test)
    x_train = np.concatenate(x_train, axis=1)
    x_test = np.concatenate(x_test, axis=1)

    y_train = np.asarray(y_train).reshape((len(y_train), ))

    meta_clf = LogisticRegression()
    meta_clf.fit(x_train, y_train)
    test_predict = meta_clf.predict_proba(x_test)
    fw = codecs.open(ROOT_DIR + "/data/test_predict_polarity_ensemble.txt",
                     'w',
                     encoding='utf-8')
    for j, prob in enumerate(test_predict):
        polarity = np.argmax(prob) - 1
        fw.write(test_aspects[j] + ',' + str(polarity) + '\n')
    time_stamp = time.asctime().replace(':', '_').split()
    fw.close()
    shutil.copy2(
        ROOT_DIR + "/data/test_predict_polarity_ensemble.txt", ROOT_DIR +
        "/data/backup/test_predict_polarity_ensemble_%s.txt" % time_stamp)