我正在用一个单层的神经网络实现一个非线性回归。然而,使用激活函数作为ReLu或Softmax,损失会被卡住,值不会随着样本的增加而减少,并且预测值是常数。因此,我用ReLu代替了LeakyReLU,损失大大减少了,预测不再是常量,甚至跟踪了原始函数。
然而,在我正在工作的上下文中,Softmax函数将更合适。然而,消失梯度问题依然存在。我试过用小重量初始化,但它不起作用。我想知道是否有人能给我一个想法,如何增加软件最大函数的陡度,因为它与LeakyReLU一起工作。
class NeuralNetwork(nn.Module):
def __init__(self,inputsize,outputsize):
super(NeuralNetwork, self).__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Linear(inputsize, outputsize),
nn.Softmax(),
)
nn.init.uniform_(w,a=-1,b=1)
def forward(self, x):
x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logits
我使用的超参数如下:
inputDim = 1 # takes variable 'x'
outputDim = 1 # takes variable 'y'
learningRate = 0.001
epochs = 100000
weight=torch.empty(3)
model = NeuralNetwork(inputDim, outputDim)
if torch.cuda.is_available():
model.cuda()
如果需要的话,我可以提供模拟数据。
criterion = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learningRate)
for epoch in range(epochs):
# Converting inputs and labels to Variable
if torch.cuda.is_available():
inputs = Variable(torch.from_numpy(vS0).cuda().float())
labels = Variable(torch.from_numpy(vC).cuda().float())
else:
inputs = Variable(torch.from_numpy(vS0).float())
labels = Variable(torch.from_numpy(vC).float())
optimizer.zero_grad()
# get output from the model, given the inputs
outputs = model(inputs)
# get loss for the predicted output
loss = criterion(outputs, labels)
# get gradients w.r.t to parameters
loss.backward()
# update parameters
optimizer.step()
print('epoch {}, loss {}'.format(epoch, loss.item()))
发布于 2022-09-28 07:01:07
软件极大( Softmax )是交叉熵损失的多类版本,已知存在消失梯度问题.将Softmax激活更改为ReLU。
https://datascience.stackexchange.com/questions/114725
复制相似问题