Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Understand the anatomy of Multi-Layer Perceptrons (MLP): input layers, hidden layers, output layers, and parameter dimensions.
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Number of input neurons | Incoming connections | |
| Number of output neurons | Neurons in current layer |
Connecting layer with 100 inputs to a hidden layer of 50 neurons.
Result: 5,050 learnable parameters
Dimensionality matches input feature vector length D.
Compresses or expands feature dimensions using non-linear activations.
1 neuron for regression/binary classification; C neurons with Softmax for multi-class.
Problem: Analyzing 50 banking customer features.
Solution: 3-layer MLP [50 -> 64 -> 32 -> 1] achieves high-throughput low-latency inference.
Interactive activity component
Print layer-by-layer parameter summaries of a deep architecture.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install torch1 import torch.nn as nn
import torch.nn as nn β PyTorch neural network module
1 class MultiLayerPerceptron(nn.Module): 2 def __init__(self): 3 super().__init__() 4 self.fc1 = nn.Linear(20, 64) # 20 inputs -> 64 neurons 5 self.relu = nn.ReLU() 6 self.fc2 = nn.Linear(64, 1) # 64 neurons -> 1 output 7 8 def forward(self, x): 9 return self.fc2(self.relu(self.fc1(x))) 10 11 net = MultiLayerPerceptron() 12 print("Network Architecture:\n", net) 13 for name, param in net.named_parameters(): 14 print(f"{name}: shape {list(param.shape)} -> {param.numel()} parameters")
class MultiLayerPerceptron(nn.Module):
Define network class
def __init__(self):
Constructor
super().__init__()
Initialize base module
self.fc1 = nn.Linear(20, 64) # 20 inputs -> 64 neurons
Hidden layer 1
self.relu = nn.ReLU()
Activation
self.fc2 = nn.Linear(64, 1) # 64 neurons -> 1 output
Output layer
def forward(self, x):
Forward computation graph
return self.fc2(self.relu(self.fc1(x)))
Chain layers
net = MultiLayerPerceptron()
Instantiate network
print("Network Architecture:\n", net)
Print structure
for name, param in net.named_parameters():
Loop over layers
print(f"{name}: shape {list(param.shape)} -> {param.numel()} parameters")
Print tensor shapes
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Inspecting Model Architecture in PyTorch 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install torch 8 9 # Step 2: Import libraries 10 import torch.nn as nn 11 12 # Step 3: Run Demonstration 13 class MultiLayerPerceptron(nn.Module): 14 def __init__(self): 15 super().__init__() 16 self.fc1 = nn.Linear(20, 64) # 20 inputs -> 64 neurons 17 self.relu = nn.ReLU() 18 self.fc2 = nn.Linear(64, 1) # 64 neurons -> 1 output 19 20 def forward(self, x): 21 return self.fc2(self.relu(self.fc1(x))) 22 23 net = MultiLayerPerceptron() 24 print("Network Architecture:\n", net) 25 for name, param in net.named_parameters(): 26 print(f"{name}: shape {list(param.shape)} -> {param.numel()} parameters")
Weights [64, 20] = 1,280 + Biases [64] = 1,344 parameters.
Weights [1, 64] = 64 + Bias [1] = 65 parameters.
1,409 total parameters ready for gradient descent.
20-dimensional input vector
Network Architecture: MultiLayerPerceptron( (fc1): Linear(in_features=20, out_features=64, bias=True) (relu): ReLU() (fc2): Linear(in_features=64, out_features=1, bias=True) ) fc1.weight: shape [64, 20] -> 1280 parameters fc1.bias: shape [64] -> 64 parameters fc2.weight: shape [1, 64] -> 64 parameters fc2.bias: shape [1] -> 1 parameters
PyTorch automatically instantiates weight matrices and bias vectors matching exact input and output dimensions.
CNN, MNIST dataset, backpropagation, loss functions, and optimization.
Test your understanding with these questions. Remember, this is for learning, not examination!