【问题标题】:Question about Binary classification Keras关于二分类Keras的问题
【发布时间】:2019-12-26 11:05:25
【问题描述】:

我正在研究使用 keras 的分类系统。 Keras 通过检查哪个是最大的数字来对标签进行分类。 例如,如果输出是 [0.1 0.8 0.1],它会将标签 1 归类为正确答案,因为它是 0.8,这是最高的数字。

但是在二进制分类中我得到了这个结果。

[0.642]
[0.996]
[0.976]
[0.302]
[0.963]
[0.115]
   .
   .
   .

我认为 Keras 将它们分类为: 如果结果[i][0] > 0.5 返回 1

但这只是我的猜测,所以我想知道 Keras 是如何对二进制结果进行分类的。

这是我使用的代码。

model = Sequential()

# Step 1 - Convolution
model.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))

# Step 2 - Pooling
model.add(MaxPooling2D(pool_size = (2, 2)))

# Adding a second convolutional layer
model.add(Conv2D(32, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))

# Step 3 - Flattening
model.add(Flatten())

# Step 4 - Full connection
model.add(Dense(units = 128, activation = 'relu'))
model.add(Dense(units = 1, activation = 'sigmoid'))

import numpy as np

print("-- Predict --")
output = model.predict_generator(test_set, steps=5)
np.set_printoptions(formatter={'float': lambda x: "{0:0.3f}".format(x)})
print(test_set.class_indices)
print(output)

【问题讨论】:

  • 你的模型和优化目标是什么?

标签: python keras


【解决方案1】:

在您的代码中,我们可以看到model.add(Dense(units = 1, activation = 'sigmoid'))。 所以我们使用sigmoid 作为激活函数。 如果您看到this 链接,您将看到函数范围为 [0,1],因此将阈值视为 0.5,分配了类。

如果您一直在进行多类分类,softmax 将是合适的激活函数。

【讨论】:

  • 感谢您的回复 :) 这对我帮助很大
【解决方案2】:

我建议你学习各种激活函数。在给定的示例中,您使用的是 sigmoid 激活函数,它输出 0 到 1 之间的连续值范围。如果使用 softmax 函数,它将生成一个向量,该向量表示潜在结果列表的概率分布。就像您在问题中提到的那样,它会将数字(即 logits)转换为总和为 1 [0.1 0.8 0.1] 的概率。

【讨论】:

  • 谢谢你的回复 :) 我要研究激活函数
猜你喜欢
  • 1970-01-01
  • 2021-11-01
  • 2019-01-13
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 2021-08-03
相关资源
最近更新 更多