如何评估 keras 中的多输入/输出模型?

Son*_*ius 4 python python-3.x keras tensorflow

我已经按照 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]))
Run Code Online (Sandbox Code Playgroud)

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

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

但是使用该代码,我收到错误“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)
Run Code Online (Sandbox Code Playgroud)

但现在我收到错误“ValueError:没有足够的值来解压(预期为 4,得到 3)”

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

Sre*_* TP 9

evaluate可以在此处找到keras 文档

退货

标量测试损失(如果模型有单个输出且没有指标)或标量列表(如果模型有多个输出和/或指标)。属性 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)
Run Code Online (Sandbox Code Playgroud)

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

还,

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

将导致错误,因为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)
Run Code Online (Sandbox Code Playgroud)

另外,在你的代码中,我可以看到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'])
Run Code Online (Sandbox Code Playgroud)

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

['loss',
 'main_output_loss',
 'aux_output_loss',
 'main_output_acc',
 'aux_output_acc']
Run Code Online (Sandbox Code Playgroud)

因此,你可以像这样解压它,

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)
Run Code Online (Sandbox Code Playgroud)