zoukankan      html  css  js  c++  java
  • tensorflow 笔记10:tf.nn.sparse_softmax_cross_entropy_with_logits 函数

    函数: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]]

    以下是计算方法:

  • 相关阅读:
    react native android9 axios network error
    .NET Core3.1升级.NET5 oracle连接报错
    asp.net mvc api swagger 配置
    ASP.NET Core3.1 中使用MongoDB基本操作
    基于.NET Core3.1的SQLiteHelper增删改帮助类
    linux离线安装gcc 和g++
    简单验证两次密码输入是否相同
    循环结构-回文数
    《暴走大事件》为80、90后正名
    循环结构-判断一个数是否为完全数(C语言)
  • 原文地址:https://www.cnblogs.com/lovychen/p/9439221.html
Copyright © 2011-2022 走看看