NN From Scratch
NN From Scratch
Choosing a Dataset
For this walkthrough, we will focus on importing the MNIST dataset and using that as
the input to our deep neural networks. Note that this is purely a demonstration of
how to make a neural network from scratch, and it is NOT the recommended
architecture for solving the MNIST problem. We will reuse some code from one of the
other articles on Activation Functions Explained.
In [ ]: from sklearn.datasets import fetch_openml
from keras.utils import to_categorical
import numpy as np
from sklearn.model_selection import train_test_split
import time
file:///Users/mythao/Desktop/NN_From_Scratch.html 1/5
02/11/2024, 12:19 NN_From_Scratch
/usr/local/lib/python3.10/dist-packages/sklearn/datasets/_openml.py:968: F
utureWarning: The default value of `parser` will change from `'liac-arff'`
to `'auto'` in 1.4. You can set `parser='auto'` to silence this warning. T
herefore, an `ImportError` will be raised from 1.4 if the dataset is dense
and pandas is not installed. Note that the pandas parser may return differ
ent data types. See the Notes Section in fetch_openml's API doc for detail
s.
warn(
PyTorch
PyTorch is yet another popular framework for computations, and this one is widely
used in the research community. In PyTorch, you still have to do a lot of work yourself
by aligning all the dimensions of the data and specifying the layers and forward pass
in an exact manner. This is not a bad thing though, and it's much easier to customize
components of a neural network.
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
train_loader = torch.utils.data.DataLoader(
datasets.MNIST('data', train=True, download=True, transform=transform
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('data', train=False, transform=transform))
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to
data/MNIST/raw/train-images-idx3-ubyte.gz
100%|██████████| 9912422/9912422 [00:00<00:00, 84765527.67it/s]
Extracting data/MNIST/raw/train-images-idx3-ubyte.gz to data/MNIST/raw
Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to
data/MNIST/raw/train-labels-idx1-ubyte.gz
100%|██████████| 28881/28881 [00:00<00:00, 91079469.04it/s]
Extracting data/MNIST/raw/train-labels-idx1-ubyte.gz to data/MNIST/raw
Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to
data/MNIST/raw/t10k-images-idx3-ubyte.gz
file:///Users/mythao/Desktop/NN_From_Scratch.html 2/5
02/11/2024, 12:19 NN_From_Scratch
Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to
data/MNIST/raw/t10k-labels-idx1-ubyte.gz
100%|██████████| 4542/4542 [00:00<00:00, 2136667.65it/s]
Extracting data/MNIST/raw/t10k-labels-idx1-ubyte.gz to data/MNIST/raw
class Net(nn.Module):
def __init__(self, epochs=10):
super(Net, self).__init__()
self.linear1 = nn.Linear(784, 128)
self.linear2 = nn.Linear(128, 64)
self.linear3 = nn.Linear(64, 10)
self.epochs = epochs
file:///Users/mythao/Desktop/NN_From_Scratch.html 3/5
02/11/2024, 12:19 NN_From_Scratch
In [ ]: model = Net()
In [ ]: import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.losses import BinaryCrossentropy
In [ ]: model = tf.keras.Sequential([
Flatten(input_shape=(28, 28)),
Dense(128, activation='sigmoid'),
Dense(64, activation='sigmoid'),
Dense(10)
file:///Users/mythao/Desktop/NN_From_Scratch.html 4/5
02/11/2024, 12:19 NN_From_Scratch
])
model.compile(optimizer='SGD',
loss=BinaryCrossentropy(),
metrics=['accuracy'])
Epoch 1/10
1875/1875 [==============================] - 11s 3ms/step - loss: 1.5230 -
accuracy: 0.3088
Epoch 2/10
1875/1875 [==============================] - 5s 3ms/step - loss: 0.6463 -
accuracy: 0.5299
Epoch 3/10
1875/1875 [==============================] - 6s 3ms/step - loss: 2.5284 -
accuracy: 0.1874
Epoch 4/10
1875/1875 [==============================] - 5s 3ms/step - loss: 3.4365 -
accuracy: 0.1396
Epoch 5/10
1875/1875 [==============================] - 6s 3ms/step - loss: 5.0578 -
accuracy: 0.2379
Epoch 6/10
1875/1875 [==============================] - 5s 3ms/step - loss: 5.9086 -
accuracy: 0.1849
Epoch 7/10
1875/1875 [==============================] - 5s 3ms/step - loss: 7.6598 -
accuracy: 0.0974
Epoch 8/10
1875/1875 [==============================] - 6s 3ms/step - loss: 7.6598 -
accuracy: 0.0974
Epoch 9/10
1875/1875 [==============================] - 5s 3ms/step - loss: 7.6598 -
accuracy: 0.0974
Epoch 10/10
1875/1875 [==============================] - 6s 3ms/step - loss: 7.6598 -
accuracy: 0.0974
Out[ ]: <keras.src.callbacks.History at 0x7ed9adea0e80>
file:///Users/mythao/Desktop/NN_From_Scratch.html 5/5