Loading...
Extract a model's state dictionary and inspect its contents.
model.state_dict() returns an OrderedDict mapping parameter names to tensors. This is the standard way to save and inspect model weights.
The starter code creates a model with named layers. Extract the model's state dictionary to inspect its parameter names and shapes.
The inspection code (listing keys, shapes) is pre-filled.
Returns a dictionary with "keys", "num_params", "fc1_weight_shape", and "fc2_bias_shape".
None
{'keys': ['fc1.weight', 'fc1.bias', 'fc2.weight', 'fc2.bias'], 'num_params': 4, 'fc1_weight_shape': [8, 4], 'fc2_bias_shape': [2]}state_dict_test() starts by seeding with torch.manual_seed(42) to ensure reproducibility.nn.Sequential model with two named modules: "fc1" (nn.Linear(4, 8)) and "fc2" (nn.Linear(8, 2)), resulting in four parameter tensors: fc1.weight, fc1.bias, fc2.weight, and fc2.bias.model.state_dict(), which returns an OrderedDict containing these four parameter tensors, leading to the list of keys: ['fc1.weight', 'fc1.bias', 'fc2.weight', 'fc2.bias'].fc1.weight and fc2.bias are determined by the nn.Linear layers: fc1.weight has shape [8,4] (8 outputs, 4 inputs) and fc2.bias has shape [2] (2 outputs), resulting in the output dictionary with the specified values.