简体   繁体   中英

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. 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)

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."

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(...)

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...

Use 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 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)}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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