tf.nn.sparse_softmax_cross_entropy_with_logits

简介:
相当于合并了softmax和cross_entropy两步
sparse_softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, name=None)
    Computes sparse softmax cross entropy between `logits` and `labels`.
    
    Measures the probability error in discrete classification tasks in which the
    classes are mutually exclusive (each entry is in exactly one class).  For
    example, each CIFAR-10 image is labeled with one and only one label: an image
    can be a dog or a truck, but not both.
    
    **NOTE:**  For this operation, the probability of a given label is considered
    exclusive.  That is, soft classes are not allowed, and the `labels` vector
    must provide a single specific index for the true class for each row of
    `logits` (each minibatch entry).  For soft softmax classification with
    a probability distribution for each entry, see
    `softmax_cross_entropy_with_logits`.
    
    **WARNING:** This op expects unscaled logits, since it performs a `softmax`
    on `logits` internally for efficiency.  Do not call this op with the
    output of `softmax`, as it will produce incorrect results.
    
    A common use case is to have logits of shape `[batch_size, num_classes]` and
    labels of shape `[batch_size]`. But higher dimensions are supported.
    
    **Note that to avoid confusion, it is required to pass only named arguments to
    this function.**
    
    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.
目录
相关文章
|
5月前
|
机器学习/深度学习 PyTorch 算法框架/工具
归一化技术比较研究:Batch Norm, Layer Norm, Group Norm
本文将使用合成数据集对三种归一化技术进行比较,并在每种配置下分别训练模型。记录训练损失,并比较模型的性能。
211 2
|
5月前
|
机器学习/深度学习 资源调度 监控
PyTorch使用Tricks:Dropout,R-Dropout和Multi-Sample Dropout等 !!
PyTorch使用Tricks:Dropout,R-Dropout和Multi-Sample Dropout等 !!
64 0
|
2月前
|
算法框架/工具 数据格式
tf.keras.layers.Conv2D
【8月更文挑战第20天】tf.keras.layers.Conv2D。
31 2
|
2月前
tf.keras.layers.Dense
【8月更文挑战第20天】tf.keras.layers.Dense。
38 2
|
2月前
tf.zeros(), tf.zeros_like(), tf.ones(),tf.ones_like()
【8月更文挑战第11天】tf.zeros(), tf.zeros_like(), tf.ones(),tf.ones_like()。
29 5
|
5月前
|
机器学习/深度学习
损失函数大全Cross Entropy Loss/Weighted Loss/Focal Loss/Dice Soft Loss/Soft IoU Loss
损失函数大全Cross Entropy Loss/Weighted Loss/Focal Loss/Dice Soft Loss/Soft IoU Loss
66 2
|
机器学习/深度学习 PyTorch 算法框架/工具
criterion = torch.nn.MSELoss() ;loss = criterion(y_pred.squeeze(), Y_train.squeeze()) 其中loss.item()的结果是指当前批次所有样本的mse总和还是平均值?
loss.item()的结果是当前批次所有样本的均方误差(MSE)值,而不是总和。这是因为torch.nn.MSELoss()默认返回的是每个样本的MSE值之和,并且在计算总体损失时通常会将其除以样本数量来得到平均损失。 在代码中,loss = criterion(y_pred.squeeze(), Y_train.squeeze())语句计算了y_pred和Y_train之间的MSE损失,然后通过调用item()方法获取了该批次训练样本的平均MSE损失。如果希望获取该批次训练样本的总MSE损失,可以使用loss.item() * batch_size来计算,其中batch_size是该批次
352 0
|
SQL PyTorch 算法框架/工具
pytorch损失函数binary_cross_entropy和binary_cross_entropy_with_logits的区别
binary_cross_entropy和binary_cross_entropy_with_logits都是来自torch.nn.functional的函数
1552 0
|
机器学习/深度学习 PyTorch 算法框架/工具
PyTorch中 torch.nn与torch.nn.functional的区别
PyTorch中 torch.nn与torch.nn.functional的区别
241 2