Keras的model.predict可以返回字典吗? [英] Can Keras' model.predict return a dictionary?

查看:903
本文介绍了Keras的model.predict可以返回字典吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档 https://keras.io/models/model/#predict 表示model.predict返回预测的Numpy数组. 在Keras API中,有没有一种方法可以区分这些数组中的哪一个?在TF实现中如何?

The documentation https://keras.io/models/model/#predict says that model.predict returns Numpy array(s) of predictions. In the Keras API, is there is a way to distinguishing which of these arrays are which? How about in the TF implementation?

在同一页文档的顶部,他们说模型可以使用列表指定多个输入和输出".似乎没有任何障碍,相反,如果通过一个字典:

At the top of the same page of documentation, they say that "models can specify multiple inputs and outputs using lists". It seems that nothing breaks if instead, one passes dictionaries:

my_model = tf.keras.models.Model(inputs=my_inputs_dict, outputs=my_outputs_dict)

调用model.fit时,同一文档说:如果模型中的输入层已命名,则还可以传递将输入名称映射到Numpy数组的字典."

When calling model.fit the same documentation says "If input layers in the model are named, you can also pass a dictionary mapping input names to Numpy arrays."

如果将my_output_dict中的键或my_output_dict中的字典值(层)的名称附加到my_model.predict(...)

It would be nice if either the keys from my_output_dict or the names of the dictionary values (layers) in my_output_dict were attached to the outputs of my_model.predict(...)

如果我使用以下命令将模型保存为TensorFlow的saved_model格式protobuf tf.keras.model.save tf.serving API以这种方式工作-带有命名的输入和输出...

If I save the model to TensorFlow's saved_model format protobuf using tf.keras.model.save the tf.serving API works this way-- with named inputs and outputs...

推荐答案

使用my_model.output_names

给予

Use my_model.output_names

Given

my_model = tf.keras.models.Model(inputs=my_inputs_dict, outputs=my_outputs_dict)

my_model.output_names自己创建dict,这是按预测顺序输出层的name属性的列表

create the dict yourself from my_model.output_names, which is a list of name attributes of your output layers in the order of prediction

prediction_list = my_model.predict(my_test_input_dict)
prediction_dict = {name: pred for name, pred in zip(my_model.output_names, prediction_list)}

这篇关于Keras的model.predict可以返回字典吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆