Keras:.predict返回百分比而不是类 [英] Keras: .predict returns percentages instead of classes

查看:95
本文介绍了Keras:.predict返回百分比而不是类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立3个类的模型:[0,1,2] 训练后,.predict函数将返回百分比列表. 我正在查看keras文档,但无法弄清楚我做错了什么. .predict_classes不再起作用,以前的分类器也没有此问题.我已经尝试过不同的激活功能(relu,Sigmoid等) 如果我理解正确,Dense(3...)中的数字定义了类的数量.

I am building a model with 3 classes: [0,1,2] After training, the .predict function returns a list of percentages instead. I was checking the keras documentation but could not figure out, what I did wrong. .predict_classes is not working anymore, and I did not have this problem with previous classifiers. I already tried different activation functions (relu, sigmoid etc.) If I understand correctly, the number inDense(3...) defines the amount of classes.

outputs1=Dense(3,activation='softmax')(att_out) 
model1=Model(inputs1,outputs1)
model1.summary()
model1.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=['accuracy'])
model1.fit(x=text_pad,y=train_y,batch_size=batch_size,epochs=epochs,verbose=1,shuffle=True) 

y_pred = model1.predict(test_text_matrix)

输出示例:

[[0.34014237 0.33570153 0.32415614]
 [0.34014237 0.33570153 0.32415614]
 [0.34014237 0.33570153 0.32415614]
 [0.34014237 0.33570153 0.32415614]
 [0.34014237 0.33570153 0.32415614]]

我想要的输出:

[1,2,0,0,0,1,2,0]

谢谢您的任何想法.

推荐答案

您没有做错任何事情,predict始终返回模型的输出,对于分类器而言,这始终是每个类的概率.

You did not do anything wrong, predict has always returned the output of the model, for a classifier this has always been probabilities per class.

predict_classes仅适用于Sequential模型,不适用于功能模型.

predict_classes is only available for Sequential models, not for Functional ones.

但是有一个简单的解决方案,您只需要在最后一个维度上使用argmax,您将获得类索引:

But there is an easy solution, you just need to take the argmax on the last dimension and you will get class indices:

y_probs = model1.predict(test_text_matrix)
y_pred  = np.argmax(y_probs, axis=-1)

这篇关于Keras:.predict返回百分比而不是类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆