Transfer Weights Between Models
Problem Statement
Transfer matching weights from a pretrained model to a new model with a different final layer.
Background
In transfer learning, you load weights for layers that match in shape and skip incompatible ones.
Your Task
The starter code creates a pretrained model (3 output classes) and a new model (5 output classes). Transfer only the parameters that match in both name and shape between the two models. fc1 should transfer (same shape), fc2 should not (different output size).
Output Format
Returns a dictionary with "fc1_weights_match", "fc1_bias_match", "fc2_weights_match", and "transferred_keys".
Example
None
{'fc1_weights_match': True, 'fc1_bias_match': True, 'fc2_weights_match': False, 'transferred_keys': ['fc1.weight', 'fc1.bias']}- The function
transfer_weights_test()initializes two models: a pretrained model with a final layer of 3 classes and a new model with a final layer of 5 classes. - It transfers the weights of
fc1from the pretrained model to the new model, which includes both the weights and bias offc1, resulting in transferred keys['fc1.weight', 'fc1.bias']. - Since the shapes of
fc2differ between the two models, its weights are not transferred, sofc2_weights_matchisFalse. - The function then verifies that the weights and bias of
fc1match between the two models, resulting infc1_weights_matchandfc1_bias_matchboth beingTrue.
Constraints
- Only transfer matching layers
- fc2 has different shapes so cannot be transferred
- Verify with torch.equal