多标签文本分类

Text Classification for multiple label

我在用卷积神经网络做文本分类。我为我的项目使用了健康文件(ICD-9-CM 代码),我使用了与 dennybritz 相同的模型,但我的数据有 36 个标签。我使用 one_hot 编码来编码我的标签。

这是我的问题,当我 运行 每个文档都有一个标签的数据时,我的代码准确度从 0.8 到 1 是完美的。如果我 运行 有多个标签的数据, 精度明显降低。

例如:一个文档有单个标签"782.0"[0 0 1 0 ... 0],
一个文档有多个标签 "782.0 V13.09 593.5": [1 0 1 0 ... 1].

谁能告诉我为什么会发生这种情况以及如何改进它?

标签编码似乎是正确的。如果你有多个正确的标签,[1 0 1 0 ... 1] 看起来完全没问题。 Denny的post中使用的损失函数是tf.nn.softmax_cross_entropy_with_logits,这是一个multi-class问题的损失函数。

Computes softmax cross entropy between logits and labels.

Measures the probability error in discrete classification tasks in which the classes are mutually exclusive (each entry is in exactly one class).

在多标签问题中,你应该使用tf.nn.sigmoid_cross_entropy_with_logits:

Computes sigmoid cross entropy given logits.

Measures the probability error in discrete classification tasks in which each class is independent and not mutually exclusive. For instance, one could perform multilabel classification where a picture can contain both an elephant and a dog at the same time.

损失函数的输入是 logits(WX)和目标(标签)。

修复准确度测量

为了正确测量多标签问题的准确性,需要更改以下代码。

# Calculate Accuracy
with tf.name_scope("accuracy"):
    correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1))
    self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")

当您可以有多个正确的标签时,上述 correct_predictions 的逻辑是不正确的。例如,说 num_classes=4,标签 0 和 2 是正确的。因此,您的 input_y=[1, 0, 1, 0]. correct_predictions 需要打破索引 0 和索引 2 之间的平局。我不确定 tf.argmax 是如何打破平局的,但是如果它通过选择较小的索引来打破平局,则标签 2 的预测总是被认为是错误的,这肯定会损害您的准确性测量。

实际上在多标签问题中,precision and recall 是比准确性更好的指标。您也可以考虑使用 precision@k (tf.nn.in_top_k) 来报告 classifier 性能。