【问题标题】:How to evaluate a multi input/output model in keras?如何评估 keras 中的多输入/输出模型?
【发布时间】:2019-02-25 11:16:37
【问题描述】:

我已经按照 keras 在本指南中的描述构建了以下具有多输入和多输出的模型。

## define the model
EMBEDDING_SIZE = 128
HIDDEN_LAYER_SIZE = 64
BATCH_SIZE = 32
NUM_EPOCHS = 10

# first input model
main_input = Input(shape=(50,), dtype='int32', name='main_input')
embedding = Embedding(MAX_NB_WORDS, EMBEDDING_SIZE,
                    input_length=MAX_SEQUENCE_LENGTH)(main_input)
lstm_out = LSTM(HIDDEN_LAYER_SIZE)(embedding)
auxiliary_output = Dense(4, activation='sigmoid', name='aux_output')(lstm_out)
# second input model
auxiliary_input = Input(shape=(5,), name='aux_input')
x = concatenate([lstm_out, auxiliary_input])

x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)

main_output = Dense(4, activation='sigmoid', name='main_output')(x)

model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])

model.compile(optimizer='rmsprop',
              loss={'main_output': 'categorical_crossentropy', 'aux_output': 'categorical_crossentropy'},
              loss_weights={'main_output': 1., 'aux_output': 0.2})

model.fit([x1train, x2train], [ytrain, ytrain],
                    epochs=NUM_EPOCHS, batch_size=BATCH_SIZE,
                    validation_data=([x1test, x2test], [ytest, ytest]))

在下一步中,我还想评估我的模型。我建议为它运行这段代码:

score, acc = model.evaluate(x={'main_input': x1test, 'aux_input': x2test},
                            y={'main_output': ytest, 'aux_output': ytest},
                            batch_size=BATCH_SIZE)

但使用该代码时,我收到错误“ValueError:要解压的值太多(预期为 2)”

所以我想我可能会得到两个输出的分数和准确性,并尝试了这段代码:

score1, score2, acc1, acc2 = model.evaluate(x={'main_input': x1test, 'aux_input': x2test},
                            y={'main_output': ytest, 'aux_output': ytest},
                            batch_size=BATCH_SIZE)

但现在我收到错误“ValueError: no enough values to unpack (expected 4, got 3)”

那我做错了什么?老实说,我只是对 main_output 的准确性感兴趣。

【问题讨论】:

    标签: python python-3.x tensorflow keras


    【解决方案1】:

    evaluate 的 keras 文档中可以找到here

    返回

    标量测试损失(如果模型有单个输出且没有指标)或标量列表(如果模型有多个输出和/或指标)。属性 model.metrics_names 将为您提供标量输出的显示标签。

    根据您的模型,如果您执行print(model.metrics_names),您将获得['loss', 'main_output_loss', 'aux_output_loss']

    model.evaluate 生成此格式的标量,它指示您在evaluate 方法的输出中看到的每个数字对应于什么。

    因此你的代码,

    score1, score2, acc1, acc2 = model.evaluate(x={'main_input': x1test, 'aux_input': x2test},
                                y={'main_output': ytest, 'aux_output': ytest},
                                batch_size=BATCH_SIZE)
    

    将导致错误,因为标量中只有 3 个索引并且代码期望找到 4

    还有,

    score, acc = model.evaluate(x={'main_input': x1test, 'aux_input': x2test},
                                y={'main_output': ytest, 'aux_output': ytest},
                                batch_size=BATCH_SIZE)
    

    将导致错误,因为evaluate 返回的值更多。

    如果你想直接在你的模型中解压evaluate的结果,你可以这样做。

    loss, main_loss, aux_loss = model.evaluate(x={'main_input': x1test, 'aux_input': x2test},
                                y={'main_output': ytest, 'aux_output': ytest},
                                batch_size=BATCH_SIZE)
    

    在您的代码中,我还可以看到 acc1acc2,这让我假设您希望评估模型的准确性。

    话虽如此,我可以看到您没有使用任何metrics 来编译模型。如果你想评估模型的acc,然后像这样编译你的模型。

    model.compile(optimizer='rmsprop',
                  loss={'main_output': 'categorical_crossentropy', 'aux_output': 'categorical_crossentropy'},
                  loss_weights={'main_output': 1., 'aux_output': 0.2}, metrics=['acc'])
    

    然后在model.evaluate()你会得到一个对应的标量

    ['loss',
     'main_output_loss',
     'aux_output_loss',
     'main_output_acc',
     'aux_output_acc']
    

    因此,你可以这样解压,

    loss, main_loss, aux_loss, main_acc, aux_acc = model.evaluate(x={'main_input': x1test, 'aux_input': x2test},
                                    y={'main_output': ytest, 'aux_output': ytest},
                                    batch_size=BATCH_SIZE)
    

    【讨论】:

      【解决方案2】:

      你没有在模型编译中定义你想要的准确性,所以当你使用评估函数时会返回损失。您在这里有 3 个损失来评估回报:

      • (1) (2) 和 (3) 的加权平均损失,在您的情况下:1. * (2) + 0.2 * (3)
      • (2) main_output 丢失 - categorical_crossentropy
      • (3) aux_output 丢失 - categorical_crossentropy

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-10
        • 2017-11-21
        • 1970-01-01
        • 2020-04-14
        • 1970-01-01
        • 2021-12-26
        • 2021-06-24
        • 1970-01-01
        相关资源
        最近更新 更多