Esempio n. 1
0
def test():
    model = AttributeClassifier()
    check_point = "checkpoint_AttA3_0.8810.pt"
    model.load_model(check_point)

    test_file = "data/attribute_test.txt"
    test_texts = load_test_data(test_file)
    f_w2v = "../embedding/embedding_all_merge_300.txt"
    W, word2index = load_w2v(f_w2v)

    f_dict = "../dataset/attribute.json"
    attr_list, attr_dict = parse_json(f_dict)

    test_data = Data((test_texts, None), word2index)

    test_predict = train.predict(model.classifier, test_data, args)
    print(test_predict)

    fw = codecs.open("test_predict.txt", 'w', encoding='utf-8')
    for p in test_predict:
        attributes = []
        for i, l in enumerate(p):
            if l != 0:
                attributes.append(attr_list[i])
        fw.write('|'.join(attributes) + '\n')
Esempio n. 2
0
 def add_embedding(self):
     if self.config.pre_trained == 'yes':
         self.word2id, w2v = load_w2v(self.config.embedding_file, self.config.embedding_dim, False)
     else:
         self.word2id = load_word2id(self.config.word2id_file)
         self.vocab_size = len(self.word2id) + 1
         w2v = tf.random_uniform([self.vocab_size, self.config.embedding_dim], -1.0, 1.0)
     if self.config.embedding_type == 'static':
         self.embedding = tf.constant(w2v, dtype=tf.float32, name='word_embedding')
     else:
         self.embedding = tf.Variable(w2v, dtype=tf.float32, name='word_embedding')
     inputs = tf.nn.embedding_lookup(self.embedding, self.x)
     return inputs
Esempio n. 3
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')
Esempio n. 4
0
def ensemble():
    f_train = "../data/train.txt"
    # f_test = "data/test_attr2.txt"
    if args.w2v == "merge":
        f_w2v = "../embedding/embedding_all_merge_300.txt"
    elif args.w2v == "fasttext2":
        f_w2v = "../embedding/embedding_all_fasttext2_300.txt"
    elif args.w2v == "tencent":
        f_w2v = "../embedding/embedding_all_tencent_200.txt"
    else:
        print("error, no embedding")
        exit(-1)
    f_dict = "../dataset/attribute.json"
    print(f_train)
    print(f_w2v)
    if not os.path.exists("%s" % args.check_dir):
        os.mkdir("%s" % args.check_dir)
    raw_texts, raw_labels = load_attr_data(filename=f_train)
    W, word2index2 = load_w2v(f_w2v)
    word2index = pickle.load(open("../data/vocabulary.pkl", 'rb'))
    assert word2index == word2index2
    attr_list, attr_dict = parse_json(f_dict)
    kf = 0
    for train_index, test_index in kfold_split(len(raw_texts), args.folds):
        kf += 1
        print("FOLD:", kf)
        print("TRAIN:", str(len(train_index)), '\n', "TEST:",
              str(len(test_index)))
        # train_index, test_index = train_index.tolist(), test_index.tolist()
        test_texts, test_labels = [raw_texts[i] for i in test_index
                                   ], [raw_labels[i] for i in test_index]
        train_texts, train_labels = [raw_texts[i] for i in train_index
                                     ], [raw_labels[i] for i in train_index]
        print(len(train_texts))
        print(len(test_labels))
        model = AttributeClassifier()
        print(attr_list)
        print(attr_dict)
        # exit(-1)
        # print(train_texts)
        model.train_from_data((train_texts, train_labels),
                              (test_texts, test_labels), W, word2index,
                              attr_dict, args, kf)
    pass
Esempio n. 5
0
def ensemble():
    f_train = ROOT_DIR + "/data/train.txt"
    if args.w2v == "merge":
        f_w2v = ROOT_DIR + "/embedding/embedding_all_merge_300.txt"
    elif args.w2v == "fasttext2":
        f_w2v = ROOT_DIR + "/embedding/embedding_all_fasttext2_300.txt"
    elif args.w2v == "tencent":
        f_w2v = ROOT_DIR + "/embedding/embedding_all_tencent_200.txt"
    else:
        print("error, no embedding")
        exit(-1)
    f_dict1 = ROOT_DIR + "/dataset/polarity.json"
    f_dict2 = ROOT_DIR + "/dataset/attribute.json"
    print(f_train)
    print(f_w2v)
    if not os.path.exists("%s" % args.check_dir):
        os.mkdir("%s" % args.check_dir)
    W, word2index2 = load_w2v(f_w2v)
    word2index = pickle.load(open(ROOT_DIR + "/data/vocabulary.pkl", 'rb'))
    assert word2index == word2index2
    polarity_list, polarity_dict = parse_json(f_dict1)
    attr_list, attr_dict = parse_json(f_dict2)
    kf = 0
    fo = load_abp_raw(f_train)
    for train_index, test_index in kfold_split(len(fo), args.folds):
        kf += 1
        print("FOLD:", kf)
        # print("TRAIN:", train_index, '\n', "TEST:", test_index, str(len(test_index)))
        train_texts, train_labels, train_aspects, test_texts, test_labels, test_aspects = splits(
            fo, train_index, test_index)
        print(len(train_texts))
        print(len(test_texts))
        # print(list(attr_dict.keys()))
        model = Classifier()
        print(attr_list)
        print(attr_dict)
        # exit(-1)
        # print(train_texts)
        model.train_from_data((train_texts, train_labels, train_aspects),
                              (test_texts, test_labels, test_aspects), W,
                              word2index, polarity_dict, attr_dict, args, kf)
Esempio n. 6
0
def main():
    f_train = ROOT_DIR + "/data/train.txt"
    f_test = "data/test_p.txt"
    if args.w2v == "merge":
        f_w2v = ROOT_DIR + "/embedding/embedding_all_merge_300.txt"
    elif args.w2v == "fasttext":
        f_w2v = ROOT_DIR + "/embedding/embedding_all_fasttext_300.txt"
    elif args.w2v == "fasttext2":
        f_w2v = ROOT_DIR + "/embedding/embedding_all_fasttext2_300.txt"
    elif args.w2v == "tencent":
        f_w2v = ROOT_DIR + "/embedding/embedding_all_tencent_200.txt"
    else:
        print("error, no embedding")
        exit(-1)
    f_dict1 = ROOT_DIR + "/dataset/polarity.json"
    f_dict2 = ROOT_DIR + "/dataset/attribute.json"
    print(f_w2v)
    # train_texts, train_labels = load_attr_data(filename=f_train)
    # # test_text, test_labels = load_attr_data(filename=f_test)
    # train_texts, train_labels, test_texts, test_labels = split_dev(train_texts, train_labels)
    train_texts, train_labels, train_aspects, test_texts, test_labels, test_aspects = load_abp_data(
        f_train, folds=5)
    if not os.path.exists("%s" % args.check_dir):
        os.mkdir("%s" % args.check_dir)
    print(len(train_texts))
    print(len(test_texts))
    W, word2index2 = load_w2v(f_w2v)
    word2index = pickle.load(open(ROOT_DIR + "/data/vocabulary.pkl", 'rb'))
    assert word2index == word2index2
    polarity_list, polarity_dict = parse_json(f_dict1)
    attr_list, attr_dict = parse_json(f_dict2)
    # print(list(attr_dict.keys()))
    model = Classifier()
    print(polarity_list)
    print(polarity_dict)
    # exit(-1)
    # print(train_texts)
    model.train_from_data((train_texts, train_labels, train_aspects),
                          (test_texts, test_labels, test_aspects), W,
                          word2index, polarity_dict, attr_dict, args)
Esempio n. 7
0
def main():
    f_train = "../data/train.txt"
    # f_test = "data/test_attr2.txt"
    if args.w2v == "merge":
        f_w2v = "../embedding/embedding_all_merge_300.txt"
    elif args.w2v == "fasttext":
        f_w2v = "../embedding/embedding_all_fasttext_300.txt"
    elif args.w2v == "fasttext2":
        f_w2v = "../embedding/embedding_all_fasttext2_300.txt"
    elif args.w2v == "tencent":
        f_w2v = "../embedding/embedding_all_tencent_200.txt"
    else:
        print("error, no embedding")
        exit(-1)
    f_dict = "../dataset/attribute.json"
    print(f_w2v)
    train_texts, train_labels = load_attr_data(filename=f_train)
    train_texts, train_labels, test_texts, test_labels = split_dev(
        train_texts, train_labels)
    print(len(train_texts))
    print(len(test_labels))
    # train_texts2, train_labels2, test_texts, test_labels = split_dev(train_texts, train_labels)
    if not os.path.exists("%s" % args.check_dir):
        os.mkdir("%s" % args.check_dir)
    # test_texts, test_labels = load_attr_data(filename=f_test)
    W, word2index2 = load_w2v(f_w2v)
    word2index = pickle.load(open("../data/vocabulary.pkl", 'rb'))
    assert word2index == word2index2
    attr_list, attr_dict = parse_json(f_dict)
    print(list(attr_dict.keys()))
    model = AttributeClassifier()
    print(attr_list)
    print(attr_dict)
    # exit(-1)
    # print(train_texts)
    model.train_from_data((train_texts, train_labels),
                          (test_texts, test_labels), W, word2index, attr_dict,
                          args)
Esempio n. 8
0
def dev():
    model = AttributeClassifier()
    check_point = "checkpoints5/checkpoint_AttA3_0.8666.pt"
    model.load_model(check_point)

    f_train = "data/attribute_data.txt"
    # f_test = "data/test_attr2.txt"
    f_w2v = "../embedding/embedding_all_merge_300.txt"
    f_dict = "../dataset/attribute.json"
    print(f_w2v)
    raw_texts, raw_labels = load_attr_data(filename=f_train)
    W, word2index = load_w2v(f_w2v)
    attr_list, attr_dict = parse_json(f_dict)
    kf = 0

    _, test_index = kfold_split(len(raw_texts), args.folds)[2]
    test_texts, test_labels = [raw_texts[i] for i in test_index
                               ], [raw_labels[i] for i in test_index]
    test_data = Data((test_texts, test_labels), word2index, attr_dict, args)

    test_predict = train.predict(model.classifier, test_data, args)
    pred_acc_t = score(test_predict, test_data.labels)
    print(pred_acc_t)
Esempio n. 9
0
def main(_):
    word_id_mapping_o, w2v_o = load_w2v(FLAGS.embedding_file,
                                        FLAGS.embedding_dim, True)
    # word_id_mapping_o, w2v_o = load_word_embedding(FLAGS.word_id_file, FLAGS.embedding_file, FLAGS.embedding_dim, True)
    word_embedding_o = tf.constant(w2v_o, dtype=tf.float32)
    # word_id_mapping_r, w2v_r = load_w2v(FLAGS.embedding_file_r, FLAGS.embedding_dim, True)
    # word_id_mapping_r, w2v_r = load_word_embedding(FLAGS.word_id_file, FLAGS.embedding_file_r, FLAGS.embedding_dim, True)
    word_id_mapping_r = word_id_mapping_o
    word_embedding_r = tf.constant(w2v_o, dtype=tf.float32)

    with tf.name_scope('inputs'):
        keep_prob1 = tf.placeholder(tf.float32)
        keep_prob2 = tf.placeholder(tf.float32)
        x_o = tf.placeholder(tf.int32,
                             [None, FLAGS.max_doc_len, FLAGS.max_sentence_len])
        x_r = tf.placeholder(tf.int32,
                             [None, FLAGS.max_doc_len, FLAGS.max_sentence_len])
        sen_len_o = tf.placeholder(tf.int32, [None, FLAGS.max_doc_len])
        sen_len_r = tf.placeholder(tf.int32, [None, FLAGS.max_doc_len])
        doc_len_o = tf.placeholder(tf.int32, None)
        doc_len_r = tf.placeholder(tf.int32, None)
        y = tf.placeholder(tf.float32, [None, FLAGS.n_class])

    with tf.device('/gpu:0'):
        inputs_o = tf.nn.embedding_lookup(word_embedding_o, x_o)
        inputs_o = tf.reshape(
            inputs_o, [-1, FLAGS.max_sentence_len, FLAGS.embedding_dim])
        if FLAGS.method == 'ATT':
            h_o = hn_att(inputs_o, sen_len_o, doc_len_o, keep_prob1,
                         keep_prob2, 'o')
        else:
            h_o = hn(inputs_o, sen_len_o, doc_len_o, keep_prob1, keep_prob2,
                     'o')
        prob_o = softmax_layer(h_o, 2 * FLAGS.n_hidden, FLAGS.random_base,
                               keep_prob2, FLAGS.l2_reg, FLAGS.n_class, 'o')
    with tf.device('/gpu:1'):
        inputs_r = tf.nn.embedding_lookup(word_embedding_r, x_r)
        inputs_r = tf.reshape(
            inputs_r, [-1, FLAGS.max_sentence_len, FLAGS.embedding_dim])
        if FLAGS.method == 'ATT':
            h_r = hn_att(inputs_r, sen_len_r, doc_len_r, keep_prob1,
                         keep_prob2, 'r')
        else:
            h_r = hn(inputs_r, sen_len_r, doc_len_r, keep_prob1, keep_prob2,
                     'r')
        prob_r = softmax_layer(h_r, 2 * FLAGS.n_hidden, FLAGS.random_base,
                               keep_prob2, FLAGS.l2_reg, FLAGS.n_class, 'r')

    with tf.name_scope('loss'):
        r_y = tf.reverse(y, [True])
        reg_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
        # loss = - tf.reduce_mean(y * tf.log(prob_o)) - tf.reduce_mean(r_y * tf.log(prob_r)) + sum(reg_loss)
        # prob = FLAGS.alpha * prob_o + (1.0 - FLAGS.alpha) * tf.reverse(prob_r, [False, True])
        loss_o = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=prob_o, labels=y))
        loss_r = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=prob_r, labels=y))
        loss = loss_o + loss_r + tf.add_n(reg_loss)
        prob = FLAGS.alpha * prob_o + (1.0 - FLAGS.alpha) * tf.reverse(
            prob_r, [True])
        all_vars = [var for var in tf.global_variables()]

    with tf.name_scope('train'):
        global_step = tf.Variable(0, name='global_step', trainable=False)
        optimizer = tf.train.AdamOptimizer(learning_rate=FLAGS.learning_rate)
        grads, global_norm = tf.clip_by_global_norm(
            tf.gradients(loss, all_vars), 5.0)
        train_op = optimizer.apply_gradients(zip(grads, all_vars),
                                             name='train_op',
                                             global_step=global_step)

    with tf.name_scope('predict'):
        cor_pred = tf.equal(tf.argmax(prob, 1), tf.argmax(y, 1))
        acc_prob = tf.reduce_mean(tf.cast(cor_pred, tf.float32))
        acc_num = tf.reduce_sum(tf.cast(cor_pred, tf.int32))

    true_y = tf.argmax(y, 1)
    pred_y = tf.argmax(prob, 1)

    title = '-d1-{}d2-{}b-{}r-{}l2-{}sen-{}dim-{}h-{}c-{}'.format(
        FLAGS.keep_prob1, FLAGS.keep_prob2, FLAGS.batch_size,
        FLAGS.learning_rate, FLAGS.l2_reg, FLAGS.max_sentence_len,
        FLAGS.embedding_dim, FLAGS.n_hidden, FLAGS.n_class)

    conf = tf.ConfigProto(allow_soft_placement=True)
    with tf.Session(config=conf) as sess:
        import time
        timestamp = str(int(time.time()))
        _dir = 'summary/' + str(timestamp) + '_' + title
        test_loss = tf.placeholder(tf.float32)
        test_acc = tf.placeholder(tf.float32)
        train_summary_op, test_summary_op, validate_summary_op, train_summary_writer, test_summary_writer, \
        validate_summary_writer = summary_func(loss, acc_prob, test_loss, test_acc, _dir, title, sess)

        save_dir = 'temp_model/' + str(timestamp) + '_' + title + '/'
        saver = saver_func(save_dir)

        init = tf.global_variables_initializer()
        sess.run(init)

        # saver.restore(sess, '/-')

        tr_x, tr_y, tr_sen_len, tr_doc_len = load_inputs_document(
            FLAGS.train_file, word_id_mapping_o, FLAGS.max_sentence_len,
            FLAGS.max_doc_len)
        te_x, te_y, te_sen_len, te_doc_len = load_inputs_document(
            FLAGS.test_file, word_id_mapping_o, FLAGS.max_sentence_len,
            FLAGS.max_doc_len)
        tr_x_r, tr_y_r, tr_sen_len_r, tr_doc_len_r = load_inputs_document(
            FLAGS.train_file_r, word_id_mapping_r, FLAGS.max_sentence_len,
            FLAGS.max_doc_len)
        te_x_r, te_y_r, te_sen_len_r, te_doc_len_r = load_inputs_document(
            FLAGS.test_file_r, word_id_mapping_r, FLAGS.max_sentence_len,
            FLAGS.max_doc_len)

        # v_x, v_y, v_sen_len, v_doc_len = load_inputs_document(
        #     FLAGS.validate_file,
        #     word_id_mapping,
        #     FLAGS.max_sentence_len,
        #     FLAGS.max_doc_len
        # )

        # v_x, v_y, v_sen_len, v_doc_len = load_inputs_document(
        #     FLAGS.validate_file,
        #     word_id_mapping,
        #     FLAGS.max_sentence_len,
        #     FLAGS.max_doc_len
        # )

        def get_batch_data(xo,
                           slo,
                           dlo,
                           xr,
                           slr,
                           dlr,
                           yy,
                           batch_size,
                           kp1,
                           kp2,
                           is_shuffle=True):
            for index in batch_index(len(yy), batch_size, 1, is_shuffle):
                feed_dict = {
                    x_o: xo[index],
                    x_r: xr[index],
                    y: yy[index],
                    sen_len_o: slo[index],
                    sen_len_r: slr[index],
                    doc_len_o: dlo[index],
                    doc_len_r: dlr[index],
                    keep_prob1: kp1,
                    keep_prob2: kp2,
                }
                yield feed_dict, len(index)

        max_acc, max_prob, step = 0., None, None
        max_ty, max_py = None, None
        for i in xrange(FLAGS.n_iter):
            for train, _ in get_batch_data(tr_x, tr_sen_len, tr_doc_len,
                                           tr_x_r, tr_sen_len_r, tr_doc_len_r,
                                           tr_y, FLAGS.batch_size,
                                           FLAGS.keep_prob1, FLAGS.keep_prob2):
                _, step, summary = sess.run(
                    [train_op, global_step, train_summary_op], feed_dict=train)
                train_summary_writer.add_summary(summary, step)
                # embed_update = tf.assign(word_embedding, tf.concat(0, [tf.zeros([1, FLAGS.embedding_dim]), word_embedding[1:]]))
                # sess.run(embed_update)

            acc, cost, cnt = 0., 0., 0
            p, ty, py = [], [], []
            for test, num in get_batch_data(te_x, te_sen_len, te_doc_len,
                                            te_x_r, te_sen_len_r, te_doc_len_r,
                                            te_y, 2000, 1.0, 1.0, False):
                _loss, _acc, _p, _ty, _py = sess.run(
                    [loss, acc_num, prob, true_y, pred_y], feed_dict=test)
                p += list(_p)
                ty += list(_ty)
                py += list(_py)
                acc += _acc
                cost += _loss * num
                cnt += num
            print 'all samples={}, correct prediction={}'.format(cnt, acc)
            acc = acc / cnt
            cost = cost / cnt
            print 'Iter {}: mini-batch loss={:.6f}, test acc={:.6f}'.format(
                i, cost, acc)
            summary = sess.run(test_summary_op,
                               feed_dict={
                                   test_loss: cost,
                                   test_acc: acc
                               })
            test_summary_writer.add_summary(summary, step)
            if acc > max_acc:
                max_acc = acc
                max_prob = p
                max_ty = ty
                max_py = py
                # saver.save(sess, save_dir, global_step=step)

        print 'P:', precision_score(max_ty, max_py, average=None)
        print 'R:', recall_score(max_ty, max_py, average=None)
        print 'F:', f1_score(max_ty, max_py, average=None)

        fp = open(FLAGS.prob_file, 'w')
        for item in max_prob:
            fp.write(' '.join([str(it) for it in item]) + '\n')
        print 'Optimization Finished! Max acc={}'.format(max_acc)

        print 'Learning_rate={}, iter_num={}, batch_size={}, hidden_num={}, l2={}'.format(
            FLAGS.learning_rate, FLAGS.n_iter, FLAGS.batch_size,
            FLAGS.n_hidden, FLAGS.l2_reg)
Esempio n. 10
0
def main(_):
    word_id_mapping, w2v = load_w2v(FLAGS.embedding_file_path,
                                    FLAGS.embedding_dim, True)
    word_embedding = tf.constant(w2v, dtype=tf.float32)

    with tf.name_scope('inputs'):
        keep_prob1 = tf.placeholder(tf.float32)
        keep_prob2 = tf.placeholder(tf.float32)
        x_l = tf.placeholder(tf.int32, [None, FLAGS.max_sentence_len])
        x_r = tf.placeholder(tf.int32, [None, FLAGS.max_sentence_len])
        len_l = tf.placeholder(tf.int32, [None])
        len_r = tf.placeholder(tf.int32, [None])
        y = tf.placeholder(tf.float32, [None, FLAGS.n_class])

    inputs_l = tf.nn.embedding_lookup(word_embedding, x_l)
    inputs_r = tf.nn.embedding_lookup(word_embedding, x_r)
    prob = ian(inputs_l, len_l, inputs_r, len_r, keep_prob1, keep_prob2)

    reg_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    loss = -tf.reduce_mean(y * tf.log(prob)) + sum(reg_loss)

    acc_num, acc_prob = acc_func(y, prob)
    global_step = tf.Variable(0, name='tr_global_step', trainable=False)
    optimizer = tf.train.MomentumOptimizer(learning_rate=FLAGS.learning_rate,
                                           momentum=0.5).minimize(
                                               loss, global_step=global_step)
    optimizer = tf.train.AdamOptimizer(
        learning_rate=FLAGS.learning_rate).minimize(loss,
                                                    global_step=global_step)
    # optimizer = train_func(loss, FLAGS.learning_rate, global_step)
    true_y = tf.argmax(y, 1)
    pred_y = tf.argmax(prob, 1)

    title = '-d1-{}d2-{}b-{}r-{}l2-{}sen-{}dim-{}h-{}c-{}'.format(
        FLAGS.keep_prob1, FLAGS.keep_prob2, FLAGS.batch_size,
        FLAGS.learning_rate, FLAGS.l2_reg, FLAGS.max_sentence_len,
        FLAGS.embedding_dim, FLAGS.n_hidden, FLAGS.n_class)

    conf = tf.ConfigProto(allow_soft_placement=True)
    with tf.Session(config=conf) as sess:
        import time
        timestamp = str(int(time.time()))
        _dir = 'summary/' + str(timestamp) + '_' + title
        test_loss = tf.placeholder(tf.float32)
        test_acc = tf.placeholder(tf.float32)
        train_summary_op, test_summary_op, validate_summary_op, train_summary_writer, test_summary_writer, \
        validate_summary_writer = summary_func(loss, acc_prob, test_loss, test_acc, _dir, title, sess)

        save_dir = 'temp_model/' + str(timestamp) + '_' + title + '/'
        saver = saver_func(save_dir)

        init = tf.initialize_all_variables()
        sess.run(init)

        # saver.restore(sess, '/-')

        tr_x1, tr_x2, tr_len1, tr_len2, tr_y = load_sentence(
            FLAGS.train_file_path, word_id_mapping, FLAGS.max_sentence_len)

        te_x1, te_x2, te_len1, te_len2, te_y = load_sentence(
            FLAGS.test_file_path, word_id_mapping, FLAGS.max_sentence_len)

        def get_batch_data(x1,
                           x2,
                           len1,
                           len2,
                           yy,
                           batch_size,
                           kp1,
                           kp2,
                           is_shuffle=True):
            for index in batch_index(len(yy), batch_size, 1, is_shuffle):
                feed_dict = {
                    x_l: x1[index],
                    x_r: x2[index],
                    y: yy[index],
                    len_l: len1[index],
                    len_r: len2[index],
                    keep_prob1: kp1,
                    keep_prob2: kp2,
                }
                yield feed_dict, len(index)

        max_acc, max_prob, step = 0., None, None
        max_ty, max_py = None, None
        for i in xrange(FLAGS.n_iter):
            for train, _ in get_batch_data(tr_x1, tr_x2, tr_len1, tr_len2,
                                           tr_y, FLAGS.batch_size,
                                           FLAGS.keep_prob1, FLAGS.keep_prob2):
                _, step, summary = sess.run(
                    [optimizer, global_step, train_summary_op],
                    feed_dict=train)
                train_summary_writer.add_summary(summary, step)
                # embed_update = tf.assign(word_embedding, tf.concat([tf.zeros([1, FLAGS.embedding_dim]), word_embedding[1:]], 0))
                # sess.run(embed_update)

            acc, cost, cnt = 0., 0., 0
            p, ty, py = [], [], []
            for test, num in get_batch_data(te_x1, te_x2, te_len1, te_len2,
                                            te_y, 2000, 1.0, 1.0, False):
                _loss, _acc, _p, _ty, _py = sess.run(
                    [loss, acc_num, prob, true_y, pred_y], feed_dict=test)
                p += list(_p)
                ty += list(_ty)
                py += list(_py)
                acc += _acc
                cost += _loss * num
                cnt += num
            print 'all samples={}, correct prediction={}'.format(cnt, acc)
            acc = acc / cnt
            cost = cost / cnt
            print 'Iter {}: mini-batch loss={:.6f}, test acc={:.6f}'.format(
                i, cost, acc)
            summary = sess.run(test_summary_op,
                               feed_dict={
                                   test_loss: cost,
                                   test_acc: acc
                               })
            test_summary_writer.add_summary(summary, step)
            if acc > max_acc:
                max_acc = acc
                max_prob = p
                max_ty = ty
                max_py = py
                saver.save(sess, save_dir, global_step=step)

        print 'P:', precision_score(max_ty, max_py, average=None)
        print 'R:', recall_score(max_ty, max_py, average=None)
        print 'F:', f1_score(max_ty, max_py, average=None)

        fp = open(FLAGS.prob_file, 'w')
        for item in max_prob:
            fp.write(' '.join([str(it) for it in item]) + '\n')
        print 'Optimization Finished! Max acc={}'.format(max_acc)

        print 'Learning_rate={}, iter_num={}, batch_size={}, hidden_num={}, l2={}'.format(
            FLAGS.learning_rate, FLAGS.n_iter, FLAGS.batch_size,
            FLAGS.n_hidden, FLAGS.l2_reg)
Esempio n. 11
0
def main(_):
    word_id_mapping_o, w2v_o = load_w2v(FLAGS.embedding_file_o,
                                        FLAGS.embedding_dim, True)
    word_embedding_o = tf.constant(w2v_o, dtype=tf.float32)
    word_id_mapping_r, w2v_r = load_w2v(FLAGS.embedding_file_r,
                                        FLAGS.embedding_dim, True)
    word_embedding_r = tf.constant(w2v_r, dtype=tf.float32)

    with tf.name_scope('inputs'):
        keep_prob1 = tf.placeholder(tf.float32)
        keep_prob2 = tf.placeholder(tf.float32)
        x_o = tf.placeholder(tf.int32, [None, FLAGS.max_sentence_len])
        x_r = tf.placeholder(tf.int32, [None, FLAGS.max_sentence_len])
        len_o = tf.placeholder(tf.int32, [None])
        len_r = tf.placeholder(tf.int32, [None])
        y = tf.placeholder(tf.float32, [None, FLAGS.n_class])

    with tf.device('/gpu:0'):
        inputs_o = tf.nn.embedding_lookup(word_embedding_o, x_o)
        inputs_o = tf.reshape(
            inputs_o, [-1, FLAGS.max_sentence_len, FLAGS.embedding_dim])
        if FLAGS.method == 'ATT':
            prob_o = bi_rnn_att(inputs_o, len_o, keep_prob1, keep_prob2, 'o')
        else:
            prob_o = bi_rnn(inputs_o, len_o, keep_prob1, keep_prob2, 'o')
    with tf.device('/gpu:1'):
        inputs_r = tf.nn.embedding_lookup(word_embedding_r, x_r)
        inputs_r = tf.reshape(
            inputs_r, [-1, FLAGS.max_sentence_len, FLAGS.embedding_dim])
        if FLAGS.method == 'ATT':
            prob_r = bi_rnn_att(inputs_r, len_r, keep_prob1, keep_prob2, 'r')
        else:
            prob_r = bi_rnn(inputs_r, len_r, keep_prob1, keep_prob2, 'r')

    r_y = tf.reverse(y, [False, True])
    reg_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    loss = -tf.reduce_mean(y * tf.log(prob_o)) - tf.reduce_mean(
        r_y * tf.log(prob_r)) + sum(reg_loss)
    prob = FLAGS.alpha * prob_o + (1.0 - FLAGS.alpha) * tf.reverse(
        prob_r, [False, True])

    acc_num, acc_prob = acc_func(y, prob)
    global_step = tf.Variable(0, name='tr_global_step', trainable=False)
    optimizer = train_func(loss, FLAGS.learning_rate, global_step)

    title = '-d1-{}d2-{}b-{}r-{}l2-{}sen-{}dim-{}h-{}c-{}'.format(
        FLAGS.keep_prob1, FLAGS.keep_prob2, FLAGS.batch_size,
        FLAGS.learning_rate, FLAGS.l2_reg, FLAGS.max_sentence_len,
        FLAGS.embedding_dim, FLAGS.n_hidden, FLAGS.n_class)

    conf = tf.ConfigProto(allow_soft_placement=True)
    with tf.Session(config=conf) as sess:
        import time
        timestamp = str(int(time.time()))
        _dir = 'summary/' + str(timestamp) + '_' + title
        _dir = 'summary/' + title
        test_loss = tf.placeholder(tf.float32)
        test_acc = tf.placeholder(tf.float32)
        train_summary_op, test_summary_op, validate_summary_op, train_summary_writer, test_summary_writer, \
        validate_summary_writer = summary_func(loss, acc_prob, test_loss, test_acc, _dir, title, sess)

        save_dir = 'temp_model/' + str(timestamp) + '_' + title + '/'
        saver = saver_func(save_dir)

        init = tf.global_variables_initializer()
        sess.run(init)

        # saver.restore(sess, '/-')

        tr_x, tr_sen_len, tr_y = load_inputs_twitter(FLAGS.train_file,
                                                     word_id_mapping_o,
                                                     FLAGS.max_sentence_len)
        te_x, te_sen_len, te_y = load_inputs_twitter(FLAGS.test_file,
                                                     word_id_mapping_o,
                                                     FLAGS.max_sentence_len)
        tr_x_r, tr_sen_len_r, tr_y_r = load_inputs_twitter(
            FLAGS.train_file_r, word_id_mapping_r, FLAGS.max_sentence_len)
        te_x_r, te_sen_len_r, te_y_r = load_inputs_twitter(
            FLAGS.test_file_r, word_id_mapping_r, FLAGS.max_sentence_len)

        def get_batch_data(xo,
                           slo,
                           xr,
                           slr,
                           yy,
                           batch_size,
                           kp1,
                           kp2,
                           is_shuffle=True):
            for index in batch_index(len(yy), batch_size, 1, is_shuffle):
                feed_dict = {
                    x_o: xo[index],
                    x_r: xr[index],
                    y: yy[index],
                    len_o: slo[index],
                    len_r: slr[index],
                    keep_prob1: kp1,
                    keep_prob2: kp2,
                }
                yield feed_dict, len(index)

        max_acc, max_prob, step = 0., None, None
        for i in xrange(FLAGS.n_iter):
            for train, _ in get_batch_data(tr_x, tr_sen_len, tr_x_r,
                                           tr_sen_len_r, tr_y,
                                           FLAGS.batch_size, FLAGS.keep_prob1,
                                           FLAGS.keep_prob2):
                _, step, summary = sess.run(
                    [optimizer, global_step, train_summary_op],
                    feed_dict=train)
                train_summary_writer.add_summary(summary, step)

            saver.save(sess, save_dir, global_step=step)

            acc, cost, cnt = 0., 0., 0
            p = []
            for test, num in get_batch_data(te_x, te_sen_len, te_x_r,
                                            te_sen_len_r, te_y, 2000, 1.0, 1.0,
                                            False):
                _loss, _acc, _p = sess.run([loss, acc_num, prob],
                                           feed_dict=test)
                p += list(_p)
                acc += _acc
                cost += _loss * num
                cnt += num
            print 'all samples={}, correct prediction={}'.format(cnt, acc)
            acc = acc / cnt
            cost = cost / cnt
            print 'Iter {}: mini-batch loss={:.6f}, test acc={:.6f}'.format(
                i, cost, acc)
            summary = sess.run(test_summary_op,
                               feed_dict={
                                   test_loss: cost,
                                   test_acc: acc
                               })
            test_summary_writer.add_summary(summary, step)
            if acc > max_acc:
                max_acc = acc
                max_prob = p

        fp = open(FLAGS.prob_file, 'w')
        for item in max_prob:
            fp.write(' '.join([str(it) for it in item]) + '\n')
        print 'Optimization Finished! Max acc={}'.format(max_acc)

        print 'Learning_rate={}, iter_num={}, batch_size={}, hidden_num={}, l2={}'.format(
            FLAGS.learning_rate, FLAGS.n_iter, FLAGS.batch_size,
            FLAGS.n_hidden, FLAGS.l2_reg)