TensorsFlow/Keras 如何为 seq2seq 问题找到不可预测的训练示例?

问题描述

我可以对所有训练样例进行 1 x 1 迭代(这非常缓慢),并找到未能成功预测的训练样例。

我可以“非常快速地”批量评估相同的示例,但是,我只能看到 verbose=1 的损失和准确性(而不是失败的预测)

有没有办法让批处理评估为未预测的项目发出信息?

这是一个 seq2seq 问题。

def decode_sequence(input_seq): # 将输入编码为状态向量。 states_value = encoder_model.predict(input_seq)

# Generate empty target sequence of length 1.
target_seq = np.zeros((1,1,num_decoder_tokens))
# Populate the first character of target sequence with the start character.
target_seq[0,target_token_index["\t"]] = 1.0

# Sampling loop for a batch of sequences
# (to simplify,here we assume a batch of size 1).
stop_condition = False
decoded_sentence = ""
while not stop_condition:
    output_tokens,h,c = decoder_model.predict([target_seq] + states_value)

    # Sample a token
    sampled_token_index = np.argmax(output_tokens[0,-1,:])
    sampled_char = reverse_target_char_index[sampled_token_index]
    decoded_sentence += sampled_char

    # Exit condition: either hit max length
    # or find stop character.
    if sampled_char == "\n" or len(decoded_sentence) > max_decoder_seq_length:
        stop_condition = True

    # Update the target sequence (of length 1).
    target_seq = np.zeros((1,num_decoder_tokens))
    target_seq[0,sampled_token_index] = 1.0

    # Update states
    states_value = [h,c]
return decoded_sentence

提前致谢

解决方法

您可以使用 tf.keras.Model.predict 进行批量预测。然后你只需要用tf.math.equal

比较预测值和真实值

注意:这个答案是在作者提到这是seq2seq相关问题之前发布的