我使用神经网络将一组对象分类为n类。每个对象可以同时属于多个类(多类,多标签)。
例如:
Input is ['a','b','c'] and labels are [1,0,0,1,1,1] # where 1 means it is in class and 0 means it's not in class it's multi labels classification
我搜索并发现人们建议你在多标签问题中使用sigmoid而不是softmax。
一些参考文献是:
Sentiment and genres classification
reddit group suggestion on multi label classification
Excellent blog on explaining the theory of multi label classification
another good tutorial on multi label
现在看完这些东西之后,我知道我应该选择sigmoid但是我很少有混淆:
所以对于单一分类,我在做什么:
在模型(带有lstm的隐藏层)之后,我正在构建完全连接的层:
#weights
Wo = tf.get_variable('Wo',
shape=[hdim*2, num_labels],
dtype=tf.float32,
initializer=tf.random_uniform_initializer(-0.01, 0.01))
#bias
bo = tf.get_variable('bo',
shape=[num_labels,],
dtype=tf.float32,
initializer=tf.random_uniform_initializer(-0.01, 0.01))
#logits
logits = tf.matmul(tf.concat([fsf.c, fsb.c], axis=-1), Wo) + bo
#probability
probs = tf.nn.softmax(logits)
#taking max probability
preds = tf.argmax(probs, axis=-1)
# Cross Entropy
ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels)
#loss
loss = tf.reduce_mean(ce)
# Accuracy
accuracy = tf.reduce_mean(
tf.cast(
tf.equal(tf.cast(preds, tf.int32), labels),
tf.float32
)
#training )
trainop = tf.train.AdamOptimizer().minimize(loss)
但在多标签中我应该如何接受输入:
所以我试过了:
#weights
Wo = tf.get_variable('Wo',
shape=[hdim*2, num_labels],
dtype=tf.float32,
initializer=tf.random_uniform_initializer(-0.01, 0.01))
#bias
bo = tf.get_variable('bo',
shape=[num_labels,],
dtype=tf.float32,
initializer=tf.random_uniform_initializer(-0.01, 0.01))
#logits
logits = tf.matmul(tf.concat([fsf.c, fsb.c], axis=-1), Wo) + bo
#probability
probs = tf.nn.sigmoid(logits)
#taking max probability
preds = tf.round(probs)
# Cross Entropy
ce = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=labels)
#loss
loss = tf.reduce_mean(ce)
# Accuracy
accuracy = tf.reduce_mean(
tf.cast(
tf.equal(tf.cast(preds, tf.int32), labels),
tf.float32
)
#training )
trainop = tf.train.AdamOptimizer().minimize(loss)
这是正确的做法,我做得对吗还是需要在某处进行纠正?
如果我传递给tf.nn.sigmoid,我是否需要传递给tf.nn.sigmoid_cross_entropy_with_logits?
谢谢你。