Python Keras 中的 model.evaluate() 返回什么值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51299836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:48:54  来源:igfitidea点击:

What values are returned from model.evaluate() in Keras?

pythonkeras

提问by GGG

I've got multiple outputs from my model from multiple Dense layers. My model has 'accuracy'as the only metric in compilation. I'd like to know the loss and accuracy for each output. This is some part of my code.

我的模型从多个 Dense 层获得了多个输出。我的模型'accuracy'是编译中的唯一指标。我想知道每个输出的损失和准确性。这是我的代码的一部分。

scores = model.evaluate(X_test, [y_test_one, y_test_two], verbose=1)

When I printed out the scores, this is the result.

当我打印出分数时,这就是结果。

[0.7185557290413819, 0.3189622712272771, 0.39959345855771927, 0.8470299135229717, 0.8016634374641469]

What are these numbers represent?

这些数字代表什么?

I'm new to Keras and this might be a trivial question. However, I have read the docs from Keras but I'm still not sure.

我是 Keras 的新手,这可能是一个微不足道的问题。但是,我已经阅读了 Keras 的文档,但我仍然不确定。

回答by today

Quoted from evaluate()method documentation:

引用自evaluate()方法文档

Returns

Scalar test loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs and/or metrics). The attribute model.metrics_nameswill give you the display labels for the scalar outputs.

退货

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

Therefore, you can use metrics_namesproperty of your model to find out what each of those values corresponds to. For example:

因此,您可以使用metrics_names模型的属性来找出每个值对应的内容。例如:

from keras import layers
from keras import models
import numpy as np

input_data = layers.Input(shape=(100,)) 
out_1 = layers.Dense(1)(input_data)
out_2 = layers.Dense(1)(input_data)

model = models.Model(input_data, [out_1, out_2])
model.compile(loss='mse', optimizer='adam', metrics=['mae'])

print(model.metrics_names)

outputs the following:

输出以下内容:

['loss', 'dense_1_loss', 'dense_2_loss', 'dense_1_mean_absolute_error', 'dense_2_mean_absolute_error']

which indicates what each of those numbers you see in the output of evaluatemethod corresponds to.

这表示您在evaluate方法输出中看到的每个数字对应的内容。

Further, if you have many layers then those dense_1and dense_2names might be a bit ambiguous. To resolve this ambiguity, you can assign names to your layers using nameargument of layers (not necessarily on all of them but only on the input and output layers):

此外,如果您有很多层,那么这些层dense_1dense_2名称可能有点含糊。为了解决这种歧义,您可以使用层的name参数(不一定在所有层上,而只能在输入和输出层上)为您的层分配名称:

# ...
out_1 = layers.Dense(1, name='output_1')(input_data)
out_2 = layers.Dense(1, name='output_2')(input_data)
# ...

print(model.metrics_names)

which outputs a more clear description:

输出更清晰的描述:

['loss', 'output_1_loss', 'output_2_loss', 'output_1_mean_absolute_error', 'output_2_mean_absolute_error']