def test_zero_one():
    def generate_beam(min_val=10,
                      max_val=50,
                      useless_val=88,
                      min_len=5,
                      max_len=10,
                      beam_size=2):
        seqs = []
        lengths = []
        for _ in range(beam_size):
            seq_len = random.randint(min_len, max_len)
            seq = [
                bytes(str(random.randint(min_val, max_val)), encoding='utf8')
                for _ in range(seq_len)
            ]
            seq += [bytes(str(useless_val), encoding='utf8')
                    ] * (max_len - seq_len)

            seqs.append(seq)
            lengths.append(seq_len)

        seqs = list(zip(*seqs))

        return seqs, lengths

    batch_size = 3

    ref = [
        [b'41', b'12', b'13', b'14', b'15', b'<stop>'],
        [b'21', b'22', b'<stop>', b'p', b'p', b'p'],
        [b'<start>', b'31', b'62', b'33', b'<stop>', b'p'],
    ]
    ref_lengths = [6, 3, 5]

    pred = [
        [[b'11', b'12', b'13', b'14', b'15', b'16'],
         [b'11', b'12', b'13', b'14', b'15', b'16']],
        [[b'<start>', b'21', b'22', b'p', b'p', b'p'],
         [b'<start>', b'21', b'22', b'p', b'p', b'p']],
        [[b'31', b'32', b'33', b'18', b'p', b'p'],
         [b'31', b'32', b'33', b'18', b'p', b'p']],
    ]
    pred_lengths = [[6, 6], [5, 5], [4, 4]]

    ref = tf.constant(ref, dtype=tf.string)
    pred = tf.constant(pred, dtype=tf.string)
    pred = tf.transpose(pred, [0, 2, 1])
    ref_lengths = tf.constant(ref_lengths)
    pred_lengths = tf.constant(pred_lengths)

    best_pred = pred_lengths[:, :, 0]

    blues = create_bleu_metric_ops(ref, pred, ref_lengths, pred_lengths)

    with tf.Session() as sess:
        print()
        print(sess.run(blues))
Exemplo n.º 2
0
def get_avg_bleu_smmary(tgt_tokens, pred_tokens, tgt_len, pred_len):
    if pred_tokens.shape.ndims > 2:
        best_tokens = pred_tokens[:, :, 0]
        best_len = pred_len[:, 0]
    else:
        best_tokens = pred_tokens
        best_len = pred_len

    bleu_score = metrics.create_bleu_metric_ops(tgt_tokens, best_tokens, tgt_len, best_len)
    avg_bleu = tf.reduce_mean(bleu_score)

    return avg_bleu