函數:tf.nn.sparse_softmax_cross_entropy_with_logits(_sentinel=None,labels=None,logits=None,name=None)
#如果遇到這個問題:Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 2). 一般是維度沒有計算好;
函數是將softmax和cross_entropy放在一起計算,對於分類問題而言,最后一般都是一個單層全連接神經網絡,比如softmax分類器居多,對這個函數而言,tensorflow神經網絡中是沒有softmax層,而是在這個函數中進行softmax函數的計算。
這里的logits通常是最后的全連接層的輸出結果,labels是具體哪一類的標簽,這個函數是直接使用標簽數據的,而不是采用one-hot編碼形式。
Args: _sentinel: Used to prevent positional parameters. Internal, do not use. labels: Tensor of shape [d_0, d_1, ..., d_{r-1}] (where r is rank of labels and result) and dtype int32 or int64. Each entry in labels must be an index in [0, num_classes).
Other values will raise an exception when this op is run on CPU, and return NaN for corresponding loss and gradient rows on GPU. logits: Unscaled log probabilities of shape [d_0, d_1, ..., d_{r-1}, num_classes] and dtype float32 or float64. name: A name for the operation (optional). Returns: A Tensor of the same shape as labels and of the same type as logits with the softmax cross entropy loss. Raises: ValueError: If logits are scalars (need to have rank >= 1) or if the rank of the labels is not equal to the rank of the logits minus one.
用法:
labele:表示的訓練數據的label信息
logits :表示的是模型計算出來的結果
如下例所示: batch_size = 5,num_class = 5,分為五類;
label 的shape為([batch_size,1]) #直接是分類后的結果,比如分五類,那么 就是[[3],[4]] logits 的shape為([batch_size,num_classes]) #softmax計算后的概率分布,對比上面,[[0.1,0.2,0.2,0.1,0.4],[0.1,0.1,0.1,0.1,0.6]]
以下是計算方法: