Access to model parameters

Hello everyone, is there a way to read and write the model parameters of a scripted model? I need, in some way, the same function as in PyTorch with ‘model.parameters()’ .

You can try model.named_parameters() ? It generates an object you will have to loop over.

for name, weights in model.named_parameters():
    print(name, weights)

Hi, thanks for your answer. This solution works in if I can execute the scripted model in python. I forgot to mention that the model should run on Android. In that case, the method named_parameters() is no longer available. Additionally, I would also need to find a way to write the parameters back into the model

No problem. Are you using TorchScript ?

Yes, I use TorchScript. Here is the Python script I use to convert the models. Just a brief explanation: set_param writes a member variable that can be later read. However, this has the disadvantage that the model becomes almost twice as large, and I can only read the parameters and not write them. In the meantime, I have already found that the parameters are stored in the constants of the scripted model. I suspect, therefore, that there is actually no way around this. Or is there?

convert_model_name = 'my_model'
device             = 'cpu'
edit_model         = False;
write_param        = True;

# load original model
model_fn = convert_model_name+'.pt'
model = torch.load('models/orig/'+model_fn, map_location=device)

param = None
if(write_param):
    param = model.parameters()

# write model parameters
model.set_param(param)

# convert for mobile
scripted_module = torch.jit.script(model)

if(edit_model):
    # Export full jit version model (not compatible mobile interpreter), leave it here for comparison
    scripted_module.save('models/scripted/'+model_fn)
    
    with ZipFile('models/scripted/'+model_fn, 'r') as zip_orig: 
        zip_orig.extractall( path='temp')

    input = input("Hit enter")

    os.system('cd temp; zip -r '+convert_model_name+'.pt '+convert_model_name)
    
    scripted_module = torch.load('temp/'+convert_model_name+'.pt')

# Export mobile interpreter version model (compatible with mobile interpreter)
fn_label = ''
if(write_param):
    optimized_scripted_module = optimize_for_mobile(scripted_module, preserved_methods=['parameters_mobile', 'set_parameters']) #, 'set_parameters'
    fn_label = '__tv'
else:
    optimized_scripted_module = optimize_for_mobile(scripted_module)
    

optimized_scripted_module._save_for_lite_interpreter('models/lite/'+convert_model_name+fn_label+'.ptl')

I don’t think you can rewrite parameters of scripted models, in any case. You can try to re-make the entire models layer by layer (if it’s not too large) and just given them you desired parameters.