Ivan Dobryaev
Ivan Dobryaev

Reputation: 170

how to add L2 regulazation to cost function pytorch?

I try add L1 and L2 regulazation in my Loss function. But I fail.

My code:

criterion = nn.NLLLoss() + nn.L1Loss()

it should be perfect for something like this:

criterion = nn.NLLLoss() + _lambda * nn.L1Loss()

How can i do it?

Upvotes: 0

Views: 45

Answers (1)

Hossein
Hossein

Reputation: 25924

You need to first instantiate them both and them add them. they both expect two arguments :


nll_loss = nn.NLLLoss()
l1_loss = nn.L1Loss()
loss = nll_loss(x, y) + _lambda * l1_loss(x, y) 

Upvotes: 1

Related Questions