35. Tensorflow: Tensorflow four cross-entropy function calculation formulas: tf.nn.cross_entropy

Tensorflow cross entropy function: cross_entropy

Note: none of the logits in the input of the tensorflow cross -entropy calculation function is the output of softmax or sigmoid, but the input of the softmax or sigmoid function, because it does the sigmoid or softmax operation inside the function

 

tf.nn.sigmoid_cross_entropy_with_logits(_sentinel=None,labels=None, logits=None, name=None)

argument:    

_sentinel: Essentially a parameter that is not used, do not need to fill in

logits: a data type (type) is float32 or float64;

shape: [batch_size, num_classes], single sample is [num_classes]

labels: A tensor (tensor) with the same type (float) and shape as logits,

name: The name of the operation, optional

output:

loss,shape:[batch_size,num_classes]

Note:

It first calculates the input logits through the sigmoid function, and then calculates their cross entropy, but it optimizes the calculation method of the cross entropy so that the result will not overflow

It works well when each category is independent but not mutually exclusive: for example a picture can contain both a dog and an elephant

output is not a number, but the loss of each sample in a batch, so it is generally used with tf.reduce_mea(loss)

Calculation formula:


Python program:

importtensorflowas tf
import numpy asnp

def sigmoid(x):
    return 1.0/(1+np.exp(-x))

# 5个样本三分类问题,且一个样本可以同时拥有多类
y = np.array([[1,0,0],[0,1,0],[0,0,1],[1,1,0],[0,1,0]] 

logits = np.array([[12,3,2],[3,10,1],[1,2,5],[4,6.5,1.2],[3,6,1]])
y_pred = sigmoid(logits)
E1 = -y*np.log(y_pred)-(1-y)*np.log(1-y_pred)
print(E1) 按计算公式计算的结果
sess =tf.Session()
y = np.array(y).astype(np.float64) # labels是float64的数据类型
E2 = sess.run(tf.nn.sigmoid_cross_entropy_with_logits(labels=y,logits=logits))
print(E2)

输出的E1,E2结果相同

tf.nn.softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, dim=-1, name=None)
argument:

_sentinel:本质上是不用的参数,不用填

logits:一个数据类型(type)是float32或float64;

shape:[batch_size,num_classes]

labels:和logits具有相同type和shape的张量(tensor),,是一个有效的概率,sum(labels)=1, one_hot=True(向量中只有一个值为1.0,其他值为0.0)

name:操作的名字,可填可不填 

output:

loss,shape:[batch_size]

Note:

它对于输入的logits先通过softmax函数计算,再计算它们的交叉熵,但是它对交叉熵的计算方式进行了优化,使得结果不至于溢出

它适用于每个类别相互独立且排斥的情况,一幅图只能属于一类,而不能同时包含一条狗和一只大象

output不是一个数,而是一个batch中每个样本的loss,所以一般配合tf.reduce_mean(loss)使用

计算公式:


Python程序:

importtensorflowas tf
import numpy asnp

 

def softmax(x):
    sum_raw = np.sum(np.exp(x),axis=-1)
    x1 = np.ones(np.shape(x))
    for inrange(np.shape(x)[0]):
        x1[i] = np.exp(x[i])/sum_raw[i]
    return x1

 

y = np.array([[1,0,0],[0,1,0],[0,0,1],[1,0,0],[0,1,0]])每一行只有一个1

logits =np.array([[12,3,2],[3,10,1],[1,2,5],[4,6.5,1.2],[3,6,1]])

y_pred =softmax(logits)
E1 = -np.sum(y*np.log(y_pred),-1)
print(E1)
sess = tf.Session()
y = np.array(y).astype(np.float64)
E2 = sess.run(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=logits))
print(E2)

输出的E1,E2结果相同

tf.nn.sparse_softmax_cross_entropy_with_logits(_sentinel=None,labels=None,logits=None, name=None)

argument:

_sentinel:本质上是不用的参数,不用填

logits:一个数据类型(type)是float32或float64;

shape:[batch_size,num_classes]

labels: shape为[batch_size],labels[i]是{0,1,2,……,num_classes-1}的一个索引, type为int32或int64

name:操作的名字,可填可不填

output:

loss,shape:[batch_size]

Note:

它对于输入的logits先通过softmax函数计算,再计算它们的交叉熵,但是它对交叉熵的计算方式进行了优化,使得结果不至于溢出
它适用于每个类别相互独立且排斥的情况,一幅图只能属于一类,而不能同时包含一条狗和一只大象 
output不是一个数,而是一个batch中每个样本的loss,所以一般配合tf.reduce_mean(loss)使用
计算公式:

和tf.nn.softmax_cross_entropy_with_logits()一样,只是要将labels转换成tf.nn.softmax_cross_entropy_with_logits()中labels的形式


tf.nn.weighted_cross_entropy_with_logits(labels,logits, pos_weight, name=None) 

计算具有权重的sigmoid交叉熵sigmoid_cross_entropy_with_logits()

argument:

_sentinel:本质上是不用的参数,不用填

logits:一个数据类型(type)是float32或float64;

shape:[batch_size,num_classes],单样本是[num_classes]

labels:和logits具有相同的type(float)和shape的张量(tensor),

pos_weight:正样本的一个系数

name:操作的名字,可填可不填

output:

loss,shape:[batch_size,num_classes]

计算公式:



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325625239&siteId=291194637