0

我有一个问题,有一个图像和一个给定的问题。答案是形式[False, True]。在训练期间,我试图找出预测是对还是错。

def build_loss(logits, labels):
    # Cross-entropy loss
    loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=labels)

    # Classification accuracy
    correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    return tf.reduce_mean(loss), accuracy

logits = C(self.img, self.q, scope='Classifier')
self.all_preds = tf.nn.softmax(logits)
self.loss, self.accuracy = build_loss(logits, self.a)

假设原始答案是[0, 0]并且预测的答案是[0, 1]tf.argmaxlogits 和标签都将返回值 1。有没有办法避免这种丑陋的比较?我尝试替换tf.argmax为,tf.reduce_max但准确性始终为 0。

或者有没有其他方法可以改变模型,这样,除了在输出层有 2 个神经元,我可以在 1 个神经元中完成它。目前,我正在使用它tf.nn.softmax来查找预测并tf.nn.sigmoid_cross_entropy_with_logits作为我的损失函数。

4

1 回答 1

0

是的,您最后只能使用一个神经元并在其中使用 sigmoid 激活。

model.add(Dense(1, activation='sigmoid'))
于 2020-05-31T20:08:25.670 回答