PIXELBANKv9.1.0
Menu

Modify State Dict Before Loading

Problem Statement

Modify a state dict by renaming keys before loading into a model with different naming conventions.

Background

When loading weights from a model with different layer names, you may need to rename keys in the state dict to match the target model.

Your Task

The starter code creates model_old (layer1, layer2) and model_new (fc1, fc2). Rename the keys in the old model's state dict to match the new model's naming conventions, then load it.

The loading and verification code is pre-filled.

Output Format

Returns a dictionary with "old_keys", "new_keys", "weights_match", and "load_success".

Example:

Input:
None
Output:
{'old_keys': ['layer1.weight', 'layer1.bias', 'layer2.weight', 'layer2.bias'], 'new_keys': ['fc1.weight', 'fc1.bias', 'fc2.weight', 'fc2.bias'], 'weights_match': True, 'load_success': True}
Reasoning:
  • The function modify_state_dict_test() starts by creating two models, model_old and model_new, with different layer names.
  • It then retrieves the state dictionary from model_old, which contains keys like 'layer1.weight' and 'layer2.bias', and renames them to match the layer names in model_new, resulting in keys like 'fc1.weight' and 'fc2.bias'.
  • The renamed state dictionary is loaded into model_new, and the function checks if the weights of fc1 in model_new match the original weights of layer1 in model_old, which they do since the renaming and loading were successful.
  • The function returns a dictionary containing the original state dictionary keys, the renamed keys, a boolean indicating whether the weights match, and a boolean indicating whether the loading was successful, resulting in the given sample output.

Constraints:

  • Rename keys by string replacement
  • Load modified state dict into new model
  • Verify weights transferred correctly
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Modify State Dict Before Loading - Medium | PixelBank