Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 513
James McKeown Avatar asked Nov 16 '22 04:11

James McKeown


1 Answers

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)}
like image 50
Ronald Luc Avatar answered Dec 06 '22 21:12

Ronald Luc