Extracting the weights from a saved model's state_dict()?

I have a saved state_dict() in a .pth file and I’m trying to extract only the weights from this dictionary.

I tried loading the state_dict() using load_state_dict() and accessing the weights as follows

m = model.load_state_dict()

However, this is giving me the following error: TypeError: tuple indices must be integers or slices, not str

which tells me I can’t access the weights by just using the key, how can I obtain the weights in this case?

I’m not actually sure what the return value of load_state_dict is, is it the state dict itself?

m = model.load_state_dict()
d = model.state_dict()

work for you?

So I looked it up and load_state_dict() is just initializing m to whatever my previously saved model is. Also I tried what you suggested with a slight modification:

m = model.load_state_dict()
d = m.state_dict()

and it printed the list of keys in the state_dict!

So I see that the keys are labelled as main.0.weight, main.1.weight, main.1.bias and so on - I want the weights and biases separately, so do I just filter by type ? Is there an easier way to do this?