예제 #1
0
def dev_step(sess, cnn, testList, dev_size=100):
    scoreList = []
    if dev_size > len(testList) / 500:
        dev_size = len(testList) / 500
        print("have test %d samples" % dev_size)
    for i in range(dev_size):
        batch_scores = []
        for j in range(int(500 / FLAGS.batch_size)):
            x_test_1, x_test_2, x_test_3 = insurance_qa_data_helpers.load_val_batch(
                testList, vocab, i * 500 + j * FLAGS.batch_size,
                FLAGS.batch_size)
            feed_dict = {
                cnn.input_x_1: x_test_1,
                cnn.input_x_2:
                x_test_2,  # x_test_2 equals x_test_3 for the test case
                cnn.input_x_3: x_test_3
            }
            predicted = sess.run(cnn.score12, feed_dict)
            batch_scores.extend(predicted)

        max_indexs = []

        max_score = max(batch_scores)
        for index, s in enumerate(batch_scores):
            if s == max_score:
                max_indexs.append(index)
        if len(max_indexs) == 0:
            scoreList.append(0)
            continue
        index = random.choice(max_indexs)
        if int(testList[i * 500 + index].split()[0]) == 1:
            scoreList.append(1)
        else:
            scoreList.append(0)
    return sum(scoreList) * 1.0 / len(scoreList)
예제 #2
0
def dev_step(sess, cnn, testList, dev_size=100):
    scoreList = []
    if dev_size > len(testList) / 500:
        dev_size = len(testList) / 500
        print("have test %d samples" % dev_size)
    for i in range(dev_size):
        #1ループ内は1つのqに対する500の候補処理
        batch_scores = []
        for j in range(int(500 / FLAGS.batch_size)):
            #x_test_1:test1のq、 x_test_2とx_test_3は同じで:test2のaかneg
            x_test_1, x_test_2, x_test_3 = insurance_qa_data_helpers.load_val_batch(
                testList, vocab, i * 500 + j * FLAGS.batch_size,
                FLAGS.batch_size)
            feed_dict = {
                cnn.input_x_1: x_test_1,
                cnn.input_x_2:
                x_test_2,  #x_test_2 equals x_test_3 for the test case
                cnn.input_x_3: x_test_3
                # cnn.dropout_keep_prob: 1.0
            }
            #predictedはリスト形式, score12はcos(q,pos)
            predicted = sess.run(
                [cnn.score12],
                feed_dict)  # add the [] means the returned value is a list

            batch_scores.extend(predicted[0])
        max_indexs = []
        max_score = max(batch_scores)
        for index, s in enumerate(batch_scores):
            if s == max_score:
                max_indexs.append(index)  #score12が最大の候補のインデックスを探索

        index = random.choice(max_indexs)
        print('ラベル 質問ID 質問文 正解回答文')
        print(testList[i * 500].split())
        print('ラベル 質問ID  質問文 モデルの予測回答文')
        print(testList[i * 500 + index].split())
        print('')
        if int(testList[i * 500 + index].split()[0]) == 1:
            #予測した候補のラベルが1ならscoreを加算
            scoreList.append(1)
        else:
            scoreList.append(0)
    return sum(scoreList) * 1.0 / len(scoreList)
예제 #3
0
def dev_step(sess, cnn, testList, dev_size=100):
    score_rr_list = []
    score_pre_list = []
    if dev_size > len(testList) / 500:
        dev_size = len(testList) / 500
        print("have test %d samples" % dev_size)
    for i in range(dev_size):
        #1ループ内は1つのqに対する500の候補処理
        batch_scores = []
        for j in range(int(500 / FLAGS.batch_size)):
            #x_test_1:test1のq、 x_test_2とx_test_3は同じで:test2のaかneg
            x_test_1, x_test_2, x_test_3 = insurance_qa_data_helpers.load_val_batch(
                testList, vocab, i * 500 + j * FLAGS.batch_size,
                FLAGS.batch_size)
            feed_dict = {
                cnn.input_x_1: x_test_1,
                cnn.input_x_2:
                x_test_2,  #x_test_2 equals x_test_3 for the test case
                cnn.input_x_3: x_test_3
                # cnn.dropout_keep_prob: 1.0
            }
            #predictedはリスト形式, score12はcos(q,pos)
            predicted = sess.run(
                [cnn.score12],
                feed_dict)  # add the [] means the returned value is a list
            batch_scores.extend(predicted[0])

        #MRR
        score_rr = rr_for_mrr(i, batch_scores, testList)
        score_rr_list.append(score_rr)

        #precision@k
        score_at_k = precision_at(i, batch_scores, testList, 10)
        score_pre_list.append(score_at_k)

    mrr = sum(score_rr_list) * 1.0 / len(score_rr_list)
    precision = sum(score_pre_list) * 1.0 / len(score_pre_list)
    return mrr, precision