I’m going to compare the difference between with and without regularization, so I want to custom two loss functions.
My loss function with L2 norm:
###NET
class CNN(nn.Module):
def __init__(self):
super(CNN,self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(3, 16, kernel_size = 5, padding=2),
nn.ReLU(),
nn.MaxPool2d(2))
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size = 5, padding=2),
nn.ReLU(),
nn.MaxPool2d(2))
self.layer3 = nn.Sequential(
nn.Conv2d(32, 32, kernel_size = 5, padding=2),
nn.ReLU(),
nn.MaxPool2d(4))
self.fc = nn.Linear(32*32*32,11)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = out.view(out.size(0), -1)
out = self.fc(out)
return out
net = CNN()
###OPTIMIZER
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr = LR, momentum = MOMENTUM)
1.How can I add a L2 norm in my loss function?
2.If I want to write the loss function by myself (without using optim.SGD) and do the grad-decent by autograd, how can I do?
Thanks for your help!
asked May 3, 2018 at 7:34
Weimin Chan
3671 gold badge3 silver badges19 bronze badges
-
You don't need to write two different loss functions if you want to try with and without regularization. You just need to write the one with regularization, and set the damping parameter alpha to zero when you want to try without regularization. Please edit and write the loss function with regularization so we can guide you.Kefeng91– Kefeng912018年05月03日 08:32:44 +00:00Commented May 3, 2018 at 8:32
-
@Kefeng91 criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr = LR, weight_decay=0.01) -------------- I got an advice that I can add a parameter 'weight_decay'. If I set it to zero, it means the loss function without regularization, and if I set to to arbitrary value I'll get what I need. Right?Weimin Chan– Weimin Chan2018年05月03日 08:51:20 +00:00Commented May 3, 2018 at 8:51
-
1What I meant is that SO is not a "please write the code for me" forum. You should first try to write the forward function yourself and then come back to us with more details about what you tried.Kefeng91– Kefeng912018年05月03日 08:56:47 +00:00Commented May 3, 2018 at 8:56
-
1@Kefeng91 Sure, there's some misunderstanding.I wrote the net by myself but I don't know how to make a custom loss function. This is just a tiny question/part in my code.And I'm sorry for that I didn't notice that this is almost the main structure of all network.Weimin Chan– Weimin Chan2018年05月03日 09:10:43 +00:00Commented May 3, 2018 at 9:10
-
Given the equation of the entropy, if you set alpha to zero, you will have no regularization. If you set alpha to anything else, you will have regularization.Kefeng91– Kefeng912018年05月03日 09:15:01 +00:00Commented May 3, 2018 at 9:15
1 Answer 1
You can explicitly compute the norm of the weights yourself, and add it to the loss.
reg = 0
for param in CNN.parameters():
reg += 0.5 * (param ** 2).sum() # you can replace it with abs().sum() to get L1 regularization
loss = criterion(CNN(x), y) + reg_lambda * reg # make the regularization part of the loss
loss.backward() # continue as usuall
See this thread for more info.
answered Jan 2, 2019 at 6:52
Shai
115k39 gold badges259 silver badges399 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py