Safe lab preview
MNIST Classification with Our Own Framework
This is a sanitized, read-only preview. Nothing executes in this page.
Lab instructions
MNIST Classification with Our Own Framework
Use this guided lab to apply the lesson concepts.
Task
Solve the MNIST handwritten digit classification problem using 1-, 2- and 3-layered perceptron. Use the neural network framework we have developed in the lesson.
Start with the notebook
Start the lab by opening MyFW_MNIST.ipynb
Questions
As a result of this lab, try to answer the following questions:
- Does the inter-layer activation function affect network performance?
- Do we need 2- or 3-layered network for this task?
- Did you experience any problems training the network? Especially as the number of layers increased.
- How do weights of the network behave during training? You may plot max abs value of weights vs. epoch to understand the relation.
Read-only
Notebook preview
My FW_MNIST
> **Integrated runtime note:** This preview uses a small deterministic, rights-safe offline fixture for reproducible learning. Full-scale results require the lesson's documented dataset or model in an approved external environment.
# MNIST Digit Classification with our own Framework
Lab Assignment from AI for Beginners Curriculum.
### Use the bundled digits fixture
> This edition uses scikit-learn's bundled handwritten-digits sample, enlarged deterministically from 8×8 to 28×28. It is smaller than MNIST and requires no network access, so sample counts and results differ from the original MNIST exercise.
import numpy as np
from sklearn.datasets import load_digits
from types import SimpleNamespace
_course_digits = load_digits()
_course_images = np.kron(_course_digits.images.astype(np.float32) / 16.0, np.ones((3, 3), dtype=np.float32))
_course_images = np.pad(_course_images, ((0, 0), (2, 2), (2, 2)))
mnist = SimpleNamespace(
data=(_course_images.reshape(len(_course_images), -1) * 255.0).astype(np.float32),
target=np.asarray(_course_digits.target, dtype=np.int64),
)all_features = np.asarray(mnist.data, dtype=np.float32)
all_labels = np.asarray(mnist.target, dtype=np.int64)
train_size = int(0.8 * len(all_features))
MNIST = {
"Train": {
"Features": all_features[:train_size],
"Labels": all_labels[:train_size],
},
"Test": {
"Features": all_features[train_size:],
"Labels": all_labels[train_size:],
},
}labels = MNIST['Train']['Labels']
data = MNIST['Train']['Features']Let's see what is the shape of data that we have:
data.shape### Splitting the Data
We will use Scikit Learn to split the data between training and test dataset:
from sklearn.model_selection import train_test_split
features_train, features_test, labels_train, labels_test = train_test_split(data,labels,test_size=0.2)
print(f"Train samples: {len(features_train)}, test samples: {len(features_test)}")### Instructions
1. Take the framework code from the lesson and paste it into this notebook, or (even better) into a separate Python module
1. Define and train one-layered perceptron, observing training and validation accuracy during training
1. Try to understand if overfitting took place, and adjust layer parameters to improve accuracy
1. Repeat previous steps for 2- and 3-layered perceptrons. Try to experiment with different activation functions between layers.
1. Try to answer the following questions:
- Does the inter-layer activation function affect network performance?
- Do we need 2- or 3-layered network for this task?
- Did you experience any problems training the network? Especially as the number of layers increased.
- How do weights of the network behave during training? You may plot max abs value of weights vs. epoch to understand the relation.
Outputs, execution counts, widgets, and active content were removed during import. Run notebooks only in an external environment you trust.
Record your practice
Optional self-reporting helps you remember what you practiced and never gates course completion.