简体   繁体   English

这是应用 softmax 的正确方法吗?

[英]is this the right way to apply softmax?

self.classifier = nn.Sequential(
        nn.Flatten(),
        nn.Linear(in_features = 32*8*8, out_features = 26),
        nn.ReLU(),
        nn.Linear(in_features = 26, out_features = output_shape),
        nn.Softmax(dim=1)
    )

and my loss fn is我的损失 fn 是

loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(params = model_0.parameters(),
                            lr = 0.07)

Is that the right way to use softmax?那是使用 softmax 的正确方法吗? output_shape is equal to num of class (this is multi class classification) output_shape等于num of class(这是多class分类)

If my implementation isn't wrong, then why do all of my data in 1 batch output the same class (even each data has very similar output probability)如果我的实现没有错,那么为什么我的一批数据 output 中的所有数据都与 class 相同(甚至每个数据都具有非常相似的 output 概率)

在此处输入图像描述

No, CrossEntropyLoss doesn't require Softmax as it already includes it (or actually LogSoftmax ): https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html?highlight=crossentropy#torch.nn.CrossEntropyLoss .不, CrossEntropyLoss不需要Softmax ,因为它已经包含它(或者实际上是LogSoftmax ): https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html?highlight=crossentropy#torch.nn.CrossEntropyLoss .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在同一个 groupby 上应用 softmax function pandas dataframe - Apply a softmax function on groupby in the same pandas dataframe Tensorflow仅将softmax功能应用于过滤器 - Tensorflow Apply softmax function only to filters 循环遍历 pandas dataframe 并应用条件的正确方法是什么? - What is the right way to loop over a pandas dataframe and apply a condition? 如何将 softmax 应用于 CNN-LSTM 时间序列 - How to apply softmax to CNN-LSTM time series 在Keras中,如何在权重矩阵的每一行上应用softmax函数? - In Keras, how to apply softmax function on each row of the weight matrix? pytorch是否在nn中自动应用softmax - Does pytorch apply softmax automatically in nn.Linear 如何在TensorFlow中将softmax应用于具有正负很大的数组/向量上? - How to apply softmax on an array/vector with huge positive and negative values in TensorFlow? 有没有一种方法可以向神经网络输出添加约束,但仍然具有softmax激活功能? - Is there a way to add constraints to a neural network output but still with softmax activation function? 将预训练的 scikit-learn model 应用于 dask 数组的正确方法? - Right way to apply pre-trained scikit-learn model to dask array? 当最后一层不是“softmax”(例如集成模型)时,如何应用 Cleverhans 攻击? - How to apply a Cleverhans attack when the final layer is not `softmax` (e.g. ensemble models)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM