[英]Does pytorch apply softmax automatically in nn.Linear
在pytorch
,分类网络模型定义为:
class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__()
self.hidden = torch.nn.Linear(n_feature, n_hidden) # hidden layer
self.out = torch.nn.Linear(n_hidden, n_output) # output layer
def forward(self, x):
x = F.relu(self.hidden(x)) # activation function for hidden layer
x = self.out(x)
return x
是否在这里应用softmax? 以我的理解,事情应该像
class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__()
self.hidden = torch.nn.Linear(n_feature, n_hidden) # hidden layer
self.relu = torch.nn.ReLu(inplace=True)
self.out = torch.nn.Linear(n_hidden, n_output) # output layer
self.softmax = torch.nn.Softmax(dim=n_output)
def forward(self, x):
x = self.hidden(x) # activation function for hidden layer
x = self.relu(x)
x = self.out(x)
x = self.softmax(x)
return x
我知道F.relu(self.relu(x))
也在应用relu,但是第一段代码并不应用softmax,对吗?
继续@jodag在评论中已经说过的内容,并对其进行扩展以形成完整的答案:
不 ,PyTorch不会自动应用softmax,您可以随时根据需要应用torch.nn.Softmax()
。 但是 ,softmax存在一些与数值稳定性有关的问题 ,我们希望尽可能避免这些问题 。 一种解决方案是使用log-softmax,但这往往比直接计算要慢。
特别是当我们使用负对数似然作为损失函数时(在PyTorch中,这是torch.nn.NLLLoss
,我们可以利用以下事实:(log-)softmax + NLLL的导数实际上在数学上非常好并且很简单,这是为什么将两者组合成一个函数/元素才有意义,结果便是torch.nn.CrossEntropyLoss
再次注意,这仅直接适用于网络的最后一层,任何其他计算均不受任何影响这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.