NNNN is a fully connected feedforward neural network with stochastic gradient descent written in Python+NumPy
- Supports classification and regression
- Depends on
numpyonly - Weights < 200 LOC
from nnnn import NNNN
data_train = ...
data_test = ...
network = NNNN(layers = [64, 16, 10], regression = False)
network.train(data_train, target, iterations = 100, rate = 0.001, alpha = 0.0001)
prediction = network.predict(data_test)(or better, use sklearn.neural_network.MLPClassifier or sklearn.neural_network.MLPRegressor)
network = NNNN(layers = [64, 16, 10], regression = False)layersis the network structure as a list of int withlayers[0] = n_dimensionsthe input dimensionlayers[-1] = n_featuresthe output dimension
regressionoptimizes the network for regression (False) or classification (True)
network.train(data_train, target, iterations = 100, rate = 0.001, alpha = 0.0001)data_trainis the input data withdata_train.shape = (n_samples, n_dimensions)targetis the output target withtarget.shape = (n_samples, n_features) or (n_samples,)iterationsis the number of gradient descent runsrateis the training rate (default:0.001)alphais the regularization factor (default:0.0001)
prediction = network.predict(data_test)data_testis the input data withdata_test.shape = data_train.shapepredictionis the output prediction withprediction.shape = target.shape
MNIST database with a 3 layers classification network, 1617 training samples and 180 testing samples
(see examples/nnnn_example.py)
training accuracy = 99%
testing accuracy = 93%
| Training | Testing |
|---|---|
![]() |
![]() |
Activation functions:
- ReLU on the hidden layers
- No activation function on the output layer for regression
- Logistic on the output layer for binary classification
- Softmax on the output layer for multiclass classification
Optimization algorithm:
- Stochastic gradient descent with regularization on the network weights
- Mean squared error loss function for regression
- Mean cross-entropy loss function for classification
numpy>=1.19.2
- Backpropagation algorithm derivation in matrix form: https://sudeepraja.github.io/Neural/
- Cross-entropy loss functions and derivations: https://peterroelants.github.io/posts/cross-entropy-logistic/, https://peterroelants.github.io/posts/cross-entropy-softmax/
- Input, weight and bias initialization: https://cs231n.github.io/neural-networks-2/

