Best way to iterate over weights of model

I think you almost did it.
A string compare should need almost no computational cost. If you initialize the l1_reg with zero you do not need the second if-closure which also saves a minimal amount of computation time. AFAIK there is no more efficient way to do so.

l1_reg = 0
for name, W in net.named_parameters():
    if 'weight' in name:
        l1_reg = l1_reg + W.norm(1)
3 Likes