PIXELBANKv9.1.0
Menu

Load State Dict with Strict Mode

Problem Statement

Load a state dict with strict and non-strict modes to handle missing/unexpected keys.

Background

strict=True (default) raises an error if keys don't match exactly. strict=False loads what it can and reports mismatches.

Your Task

The starter code creates two models (model_a with 3 layers, model_b with 2 layers). Try loading model_a's state dict into model_b. Handle the mismatch using both strict mode (which should fail) and non-strict mode (which should succeed with reported mismatches).

Output Format

Returns a dictionary with "strict_failed", "missing_keys", and "unexpected_keys".

Example:

Input:
None
Output:
{'strict_failed': True, 'missing_keys': [], 'unexpected_keys': ['fc3.weight', 'fc3.bias']}
Reasoning:
  • The function load_strict_test() creates two models, model_a and model_b, with model_a having an extra layer fc3 not present in model_b.
  • When loading model_a's state dict into model_b with strict=True, it raises a RuntimeError because model_b is missing the fc3 layer, confirming that strict loading fails.
  • With strict=False, the load operation succeeds, but model_b doesn't have the fc3 layer, so the keys fc3.weight and fc3.bias from model_a's state dict are reported as unexpected keys.
  • The function returns a dict with "strict_failed" set to True, "missing_keys" as an empty list since no keys were missing in model_b that were present in its own definition, and "unexpected_keys" containing ['fc3.weight', 'fc3.bias'], which were present in model_a but not in model_b.

Constraints:

  • Try strict=True first (should fail)
  • Then use strict=False
  • Report missing and unexpected keys
solution.py

Test Results

0/0
Run code to see test results.
Load State Dict with Strict Mode - Medium | PixelBank