Getting only weights (not biases) from a Module

Anyone have any good ways to grab just the weights and not the biases from a module?

I can think of the following kinda hacky solution to filter by dimension since all biases should be 1 dim, maybe? :

for param in model.parameters():
    if param.dim() > 1:
        #this is a weight parameter

There is a very recent PR that adds a named_parameters functionality, so that you can select the names of the parameters that you want. You need to have the master branch of pytorch for it to work. Here is an example

for name, param in model.named_parameters():
    if 'bias' not in name:
        # do something
2 Likes