PIXELBANKv9.1.0
Menu

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:

Input:
None
Output:
{'fc1_weights_match': True, 'fc1_bias_match': True, 'fc2_weights_match': False, 'transferred_keys': ['fc1.weight', 'fc1.bias']}
Reasoning:
  • 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 fc1 from the pretrained model to the new model, which includes both the weights and bias of fc1, resulting in transferred keys ['fc1.weight', 'fc1.bias'].
  • Since the shapes of fc2 differ between the two models, its weights are not transferred, so fc2_weights_match is False.
  • The function then verifies that the weights and bias of fc1 match between the two models, resulting in fc1_weights_match and fc1_bias_match both being True.

Constraints:

  • Only transfer matching layers
  • fc2 has different shapes so cannot be transferred
  • Verify with torch.equal
🔒

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.