繁体   English   中英

如何使用tensorflow tf.losses.softmax_cross_entropy?

[英]how to use tensorflow tf.losses.softmax_cross_entropy?

我正在做一些语义分割问题,需要定义损失函数。

有没有人知道如何使用tensorflow“tf.losses.softmax_cross_entropy”?

在文档中说,函数的第一个输入是onehot_labels,那么我们是否需要首先将像素级类标签转换为一种热编码格式并将一个热编码输入到此函数中?

或者我们可以直接输入像张量sigmoid_cross_entropy损失函数中的tf.losses.sigmoid_cross_entropy这样的像素类标签来进行图像分割

非常感谢!

我基本上解决了我的问题,请看下面的演示代码。

# each element is a class label for vectors (eg,[2,1,3]) in logits1
indices = [[1, 0],[1, 0]]
# each 1d vector eg [2,1,3] is a prediction vector for 3 classes 0,1,2;
# i.e., class 0 is predicted to be 2 and class 1 is predicted to be 1
# softmax will map this to corresponding probabilities
logits1 = np.array([  [[2.0,1.,3.],[2.,1,3]],   [[1.0,2.,3.],[2.,1,3]]   ])
print(logits1)
depth = 3

# first go to one hot encode format
# depth is nb of classes
encode = tf.one_hot(indices, depth)
# dim of encode must be equal to dim of logits
test = tf.losses.softmax_cross_entropy(onehot_labels = encode, logits = logits1)
init_op = tf.global_variables_initializer()

with tf.Session() as sess:
    init_op.run()
    print(sess.run(encode))
    print(sess.run(test))

该功能真正做的是以下内容:


(-np.log(np.exp(1)/(np.exp(1)+np.exp(2)+np.exp(3)))-np.log(np.exp(2)/(np.exp(2)+np.exp(1)+np.exp(3))))/4 +\
(-np.log(np.exp(2)/(np.exp(1)+np.exp(2)+np.exp(3)))-np.log(np.exp(2)/(np.exp(2)+np.exp(1)+np.exp(3))))/4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM