如何使用tensorflow对一类数据进行分类



我有一个用TensorFlow编写的深度缠绕代码。此代码适用于多个类。我想把代码改为一个类,这样我就可以教授零类,并将任何与零类数据不同的数据识别为非零。我使用sigmoid函数和最后一层的一个神经元。我的模型训练很容易,但在测试时,它只识别任何其他类型数据的同一类。我把代码放在下面。如何将其更改为识别非类?

h_drop = tf.nn.dropout(h_pool_flat, keep_prob=self.keep_prob)
# Softmax
with tf.name_scope('softmax'):
softmax_w = tf.Variable(tf.truncated_normal([num_filters_total, self.num_classes], stddev=0.1), name='softmax_w')
softmax_b = tf.Variable(tf.constant(0.1, shape=[self.num_classes]), name='softmax_b')
# Add L2 regularization to output layer
self.l2_loss += tf.nn.l2_loss(softmax_w)
self.l2_loss += tf.nn.l2_loss(softmax_b)
self.logits = tf.matmul(h_drop, softmax_w) + softmax_b
predictions = tf.nn.sigmoid(self.logits)
print(predictions)
**self.predictions = tf.argmax(predictions, 1, name='predictions')**
# Loss
with tf.name_scope('loss'):
losses = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=self.input_y, logits=self.logits)
# Add L2 losses
self.cost = tf.reduce_mean(losses) + self.l2_reg_lambda * self.l2_loss
# Accuracy
with tf.name_scope('accuracy'):
correct_predictions = tf.equal(self.predictions, self.input_y)
print(self.input_y)
print(self.predictions)
self.correct_num = tf.reduce_sum(tf.cast(correct_predictions, tf.float32))
self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32), name='accuracy')

这条线需要改,但我不知道怎么改。self.predictions=tf.argmax(predictions,1,name='predictions'(你能指导我吗?

为了可视化你的概念错误:如果你被训练来识别猫的图像,并且每次你在训练期间正确地喊"猫",你都会得到一块饼干,如果你突然看到一只狗的图像,你会怎么说
-没错,说"猫",因为你仍然可以得到饼干。

更具体地说,如果在培训期间没有这两种情况的例子,你的人际网络就无法了解"对"或"错"的含义。如果没有一个负面的例子,你的训练就不会在传统意义上起作用,因为对网络来说,说你所展示的是它所知道的单一类别总是"有益的"。

单类分类的研究领域是存在的(例如,请参阅本文和本文(,但到目前为止,我想说,使用一些负面例子来获得不错的性能会更有意义,尤其是如果你手头有大量现成的训练数据(即MNIST中的非零图像(。

相关内容

最新更新