mardi 30 mars 2021

python variant is not defined in ' if ' behind ' for ' [closed]

I am trying to load a pretained checkpoint to my own network

net.load_state_dict(checkpoint)

net_dict = net.state_dict() # the load in checkpoint
model_dict = model.state.dict() # the own network model

new_state_dict = {k:v for k, v in net_dict.items() if k in model_dict}

but it returns *** NameError: name 'model_dict' is not defined

I feel quite confused because I do define the model_dict

so I try:

'model_dict' in dir()
return: True

and

{k:v for k, v in net_dict.items() if 'model_dict' in dir()}
return: {}

which means 'model_dict' in dir() returns False in this situation.

Why this happens? What should I do to get k in model_dict?

Aucun commentaire:

Enregistrer un commentaire