简体   繁体   English

Keras 的 model.predict 可以返回字典吗?

[英]Can Keras' model.predict return a dictionary?

The documentation https://keras.io/models/model/#predict says that model.predict returns Numpy array(s) of predictions.文档https://keras.io/models/model/#predictmodel.predict返回 Numpy 预测数组。 In the Keras API, is there is a way to distinguishing which of these arrays are which?在 Keras API 中,有没有办法区分这些数组中的哪些是哪些? How about in the TF implementation?在TF实现中怎么样?

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)

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."调用model.fit ,相同的文档说“如果模型中的输入层被命名,您还可以将字典映射输入名称传递给 Numpy 数组。”

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(...)如果my_output_dict的键或my_output_dict中的字典值(层)的my_output_dict附加到my_model.predict(...)的输出, my_model.predict(...)

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...如果我使用tf.keras.model.save将模型保存到 TensorFlow 的 saved_model 格式 protobuf tf.serving API 以这种方式工作 - 具有命名的输入和输出......

Use my_model.output_names使用my_model.output_names

Given给定的

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

create the dict yourself from my_model.output_names , which is a list of name attributes of your output layers in the order of predictionmy_model.output_names自己创建dict ,它是按预测顺序排列的输出层的name属性列表

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

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

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