python - 分类器如何预测一个类别?(对于张量流/keras)
问题描述
我想自己实现模型,因此必须知道它是如何对数据进行分类的。我为 12 类分类器建立了一个模型,它预测得很好。但是最后一个 conv 层只输出 12 个浮点值,我不知道它是如何突然预测正确的类的。
有人可以为我解释吗?就像它取决于某个阈值还是选择最大值或其他什么?谢谢!
解决方案
您需要在模型中添加一个展平层,然后是一个密集层。密集层应该有 12 个节点并使用如下所示的 softmax 激活'您的模型现在将为每个图像输出 12 个概率值的列表。
flatten=tf.keras.layers.Flatten()(last_conv_layer)
output = Dense(12, activation='softmax')(flatten)
#after you train you can evaluate your model on your test set using model.evaluate()
#to make Predictions use model.predict()
predictions=model.predict(.....
#you can get the index of the predicted class for the images you predict with
for p in predictions:
predicted_index=argmax(p)
print (predicted_index)
model.evaluate 和 model.predict 的文档在这里。添加两层后不要忘记重新编译模型。
推荐阅读
- python - 如何将 google colab 中的集群文件保存到 Google Drive
- reactjs - chartjs:条形小于实际列,工具提示不显示
- python - pyspark comapre 仅来自时间戳的时间
- laravel - Laravel 响应的数据类型在不同的服务器上有所不同
- postgresql - 将 decode 和 Instr 转换为 postgres
- mlflow - 将实验从 MLFlow 服务器复制到另一个 MLFlow 服务器
- javascript - JavaScript Uncaught TypeError:无法读取未定义的属性“包含”
- angular - 必须在不使用 Moment npm (Angular) 的情况下更改 datepicker 中的默认日期格式
- javascript - 在 netlify 上托管的 gatsby 网站中启用 301 重定向规则
- reactjs - 无法获取错误 400 错误请求的数据