Keras多标签多分类模型预测结果的类别对应关系确认
Absolutely, the order of the arrays returned by model.predict(test_data) matches exactly the order in which you defined your output layers in the model architecture.
Let's break this down with your specific setup:
- Your model ends with three
Dense(1)layers added in the sequence: first for Class1, then Class2, finally Class3. - So the three
(num_samples, 1)ndarrays you get back will correspond to:- First array: Predictions for Class1
- Second array: Predictions for Class2
- Third array: Predictions for Class3
This is a consistent, default behavior in Keras for multi-output models. The framework tracks each output tensor in the exact order you add them during model construction—this applies to both training (when mapping loss functions to outputs) and inference (when returning predictions).
A handy tip to avoid any confusion down the line: name your output layers explicitly when defining them, like this:
# ... after GRU2 layer ... output_class1 = Dense(1, name="pred_class1")(gru2_output) output_class2 = Dense(1, name="pred_class2")(gru2_output) output_class3 = Dense(1, name="pred_class3")(gru2_output) model = Model(inputs=input_layer, outputs=[output_class1, output_class2, output_class3])
Then you can use return_dict=True in predict() to get a dictionary where keys are your layer names and values are the corresponding prediction arrays:
predictions = model.predict(test_data, return_dict=True) # Access predictions like predictions["pred_class1"], predictions["pred_class2"], etc.
This makes your code more readable and eliminates any ambiguity about which prediction corresponds to which class.
内容的提问来源于stack exchange,提问作者shanlodh

