权重衰减
权重衰减可以在损失函数中加入假如L2惩罚项\(\frac{\lambda}{2}||w||^2\),然后正常使用优化器进行梯度下降。但这种方式会使得计算量变大。
另一种方式是损失函数不计算L2惩罚项,直接在梯度下降时加入惩罚项的梯度,在pytorch的优化器中,有weight_decay
这个参数设置L2惩罚项的\(\lambda\)参数值,如果非零,则进行权重衰减的梯度下降,比如:
1
| loss_function = optim.SGD(model.parameters(), lr=0.01, weight_decay=1e-6)
|
Dropout
1
| torch.nn.Dropout(p=0.5, inplace=False)
|
p
:
这个参数控制着dropout的比例。例如,如果p设置为0.2,则在训练过程中,每个神经元有20%的概率被关闭(即其输出被置为0)。这个参数可以根据具体任务和过拟合的程度进行调整。
inplace
:
这个参数是一个优化选项。如果设置为True,Dropout操作将会直接在输入张量上进行,而不是创建一个新的张量来存储结果。这可以节省内存,但会改变输入张量的值,可能会影响到后续的操作。
在神经网络模块的训练模式时,pytorch会自动应用dropout;在评估模式时,pytorch则不会应用dropout
比如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| import torch import torch.nn as nn import torch.optim as optim
class SimpleNet(nn.Module): def __init__(self): super(SimpleNet, self).__init__() self.fc1 = nn.Linear(784, 500) self.dropout = nn.Dropout(0.5) self.fc2 = nn.Linear(500, 10)
def forward(self, x): x = torch.relu(self.fc1(x)) x = self.dropout(x) x = self.fc2(x) return x
model = SimpleNet()
criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=0.01)
model.train() for data, target in train_loader: optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() optimizer.step()
model.eval() with torch.no_grad(): for data, target in test_loader: output = model(data)
|