试图了解model.predict的输出

时间:2019-08-17 06:18:43

标签: python tensorflow machine-learning keras deep-learning

我正在尝试使用model.predict方法预测猫/狗图像。由于它是2类分类器,因此得到2个值的数组。根据我的理解,这些值代表每个类别中的概率(如果我错了,请纠正我)。如果是这样,则必须将概率总和为1。但是两个类的概率都相同

模型历史记录

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten (Flatten)            multiple                  0         
_________________________________________________________________
dense (Dense)                multiple                  30848     
_________________________________________________________________
dropout (Dropout)            multiple                  0         
_________________________________________________________________
batch_normalization (BatchNo multiple                  512       
_________________________________________________________________
dense_1 (Dense)              multiple                  12900     
_________________________________________________________________
dropout_1 (Dropout)          multiple                  0         
_________________________________________________________________
batch_normalization_1 (Batch multiple                  400       
_________________________________________________________________
dense_2 (Dense)              multiple                  10100     
_________________________________________________________________
dropout_2 (Dropout)          multiple                  0         
_________________________________________________________________
batch_normalization_2 (Batch multiple                  400       
_________________________________________________________________
dense_3 (Dense)              multiple                  10100     
_________________________________________________________________
dropout_3 (Dropout)          multiple                  0         
_________________________________________________________________
batch_normalization_3 (Batch multiple                  400       
_________________________________________________________________
dense_4 (Dense)              multiple                  10100     
_________________________________________________________________
dropout_4 (Dropout)          multiple                  0         
_________________________________________________________________
batch_normalization_4 (Batch multiple                  400       
_________________________________________________________________
dense_5 (Dense)              multiple                  202       
=================================================================
Total params: 76,362
Trainable params: 75,306
Non-trainable params: 1,056

预测代码

class_prob=model.predict(new_array_2.T,batch_size=1)
print(class_prob)
classifications=model.predict_classes(new_array_2.T,batch_size=1)
print(classifications)
print(CATEGORIES[classifications[0]])

输出

[[0.39456758 0.39456758]]
[0]
Dog

1 个答案:

答案 0 :(得分:1)

model.predict只是返回给定输入的模型的计算输出,因此您提到的所有细节都取决于模型的输出,例如,最后一层的激活。

模型输出总和为1的概率仅由输出层上的softmax激活产生,在我看来,您的最后一层具有不同的激活,可能为sigmoid,不会产生合计的概率。您应该更喜欢使用softmax进行多类分类。