0
0
PyTorchml~15 mins

Model parameters inspection in PyTorch - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Model parameters inspection
Problem:You have trained a simple neural network on a classification task. You want to understand the model better by inspecting its parameters such as weights and biases.
Current Metrics:Training accuracy: 85%, Validation accuracy: 82%
Issue:You do not know how to access and interpret the model's parameters to verify their shapes and values.
Your Task
Learn how to access, print, and interpret the parameters of a PyTorch model, including their shapes and sample values.
Use PyTorch only.
Do not modify the model architecture or training code.
Focus only on parameter inspection.
Hint 1
Hint 2
Hint 3
Solution
PyTorch
import torch
import torch.nn as nn

# Define a simple model
class SimpleNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(10, 5)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(5, 2)

    def forward(self, x):
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        return x

# Instantiate the model
model = SimpleNN()

# Inspect model parameters
for name, param in model.named_parameters():
    print(f"Parameter name: {name}")
    print(f"Shape: {param.shape}")
    print(f"Sample values: {param.view(-1)[:5]}")
    print("---")
Added code to loop through model.named_parameters() to access each parameter's name and tensor.
Printed the shape of each parameter tensor to understand its dimensions.
Printed the first 5 values of each parameter tensor to get a sense of the values.
Results Interpretation

Before: No knowledge of model parameters.

After: Able to see parameter names, shapes, and sample values.

Inspecting model parameters helps you understand the model's structure and verify that weights and biases are correctly initialized and updated.
Bonus Experiment
Try modifying one of the model's parameters manually and observe how it affects the model's output on a sample input.
💡 Hint
Use torch.no_grad() to safely change parameter values without tracking gradients.