简体   繁体   English

打印或保存 tf.keras 模型的输出,与输入配对

[英]Print or save output of tf.keras model, paired with inputs

def locations_model(...):
    input_shape = image_shape + (3,)
    base_model = tf.keras.applications.MobileNetV2(...)                                                                base_model.trainable = False 
    inputs = tf.keras.Input(...)  
... ...     
    outputs = tfl.Dense(5, activation = "softmax")(x)
    model = tf.keras.Model(inputs, outputs)
 
    return model

The code above is just to show inputs and outputs in a tf.keras model that classifies input images into 5 categories.上面的代码只是显示 tf.keras 模型中的输入和输出,该模型将输入图像分为 5 个类别。 How can I save the output category ("y_pred") for every input image?如何保存每个输入图像的输出类别(“y_pred”)?

The simple statement ypreds = model(inputs) or ypreds = model.predict(inputs) produces a set of 5-element arrays that add to 1, ie, probabilities.简单语句ypreds = model(inputs)ypreds = model.predict(inputs)生成一组 5 元素数组,它们相加为 1,即概率。
The question therefore is how to output the predicted categories, which in this case are integers: 0-4, instead of the probabilities.因此,问题是如何输出预测的类别,在这种情况下是整数:0-4,而不是概率。 Update: this was answer by Apostolova for the question "Get class labels from Keras functional model" by Lodzz, as test_probas = model.predict(test_data) test_classes = probas.argmax(axis = -1)更新:这是 Apostolova 对 Lodzz 的“从 Keras 功能模型获取类标签”问题的回答,如 test_probas = model.predict(test_data) test_classes = probas.argmax(axis = -1)

Thanks for the confirmation @EduardoriosChicago .感谢@EduardoriosChicago的确认。 I am mentioning your answer here for the benefit of the community.为了社区的利益,我在这里提到您的答案。

The code is代码是

probas = model(x_in); x_classes = probas.argmax( axis = - 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM