Add Spectral Normalization to all Conv-Layers?

Is there a way to iterate over all modules of a model, let’s call it Generator, and equip all Conv2d- and ConvTranspose2d-layers with spectral normalization?

Sorry, if this has been discussed already, I couldn’t find it though…

Many thanks in advance!

Is that a valid solution? How can you check if Conv-layer is equiped with spectral norm?

# Make all layers to be spectral normalization layer
def add_sn(m):    
    if isinstance(m, (nn.Conv2d, nn.ConvTranspose2d)):
        return spectral_norm(m)
    else:
        return m
    
Generator.apply(add_sn)
1 Like

This is the correct anwer.

You can simply verify it by checking

conv.weight_u.size()

2 Likes