NNNN
is a fully connected feedforward neural network with stochastic gradient descent written in Python+NumPy
- Supports classification and regression
- Depends on
numpy
only - 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)
layers
is the network structure as a list of int withlayers[0] = n_dimensions
the input dimensionlayers[-1] = n_features
the output dimension
regression
optimizes the network for regression (False
) or classification (True
)
network.train(data_train, target, iterations = 100, rate = 0.001, alpha = 0.0001)
data_train
is the input data withdata_train.shape = (n_samples, n_dimensions)
target
is the output target withtarget.shape = (n_samples, n_features) or (n_samples,)
iterations
is the number of gradient descent runsrate
is the training rate (default:0.001
)alpha
is the regularization factor (default:0.0001
)
prediction = network.predict(data_test)
data_test
is the input data withdata_test.shape = data_train.shape
prediction
is 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/