#如果遇到这个问题: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]]
以下是计算方法: