我正在尝试修改Deep MNIST for Experts这个教程来检测一个类,让我们说检测一个图像是否包含一个小猫.
这是我的代码的预测部分:
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.initialize_all_variables())
for i in range(20000):
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x:batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print("test accuracy %g"%accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
Run Code Online (Sandbox Code Playgroud)
问题是,对于一个类,softmax总是以1的置信度返回该类,即使对于空白图像也是如此.我尝试修改softmax和交叉熵,但我无法解决它.
我需要知道这个问题的推荐方法.我希望预测是图像成为小猫的概率.
我知道这可以通过使用随机图像训练的第二个标签来解决,但我需要知道是否有更好的解决方案.
非常感谢你.