Neural Network Framework needs nothing beyond NumPy and Matplotlib to run. In this guide you will install those two dependencies, import the library, wire together a small three-layer network, and train it to solve the classic XOR problem — a non-linearly-separable binary classification task that requires at least one hidden layer to learn correctly. By the end of the page you will have a working training loop and know how to run inference on new inputs.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/adi3120/Neural-Network-Framework/llms.txt
Use this file to discover all available pages before exploring further.
Install dependencies
Neural Network Framework’s only runtime requirements are NumPy (for all tensor operations) and Matplotlib (for plotting the loss curve). Install them with pip:No other packages are required. If you later want to load the MNIST dataset using the provided example scripts, you can optionally install TensorFlow — but it is never needed for the core library.
Import the library
Place
ANN.py in the same directory as your script, then import everything from it with a wildcard import. This makes all layer classes, weight initialization helpers, and training loop functions available in your namespace:from ANN import * exposes InputLayer, HiddenLayer, OutputLayer, gradient_descent_epoch, gradient_descent_threshold, and the underlying NumPy namespace (np) — everything you need to build and train a network.Define your layers
Create each layer with its neuron count and activation function, then link hidden and output layers to their predecessors using
attach_after().For XOR, the input has 2 features, a single hidden layer has 2 neurons with sigmoid activation, and the output has 1 neuron with a linear ("none") activation and MSE loss:attach_after(layer) sets up the doubly-linked previous / next pointers that the forward and backward passes rely on to move activations and gradients through the network.Initialize weights and biases
Before training, every
HiddenLayer and OutputLayer must have its weight matrix and bias vector initialized. Call set_weights(method) and set_biases(method) on each trainable layer:"normal_random" draws weights from a standard normal distribution N(0, 1). "zeros" initializes every bias to 0.0. See the Introduction for the full list of supported initialization strategies.Build the ANN list
The training utilities expect a plain Python list that begins with an The order of elements in this list defines the forward-pass execution order. Both
InputLayer and ends with an OutputLayer:gradient_descent_epoch and gradient_descent_threshold iterate over this list from front to back during the forward pass and from back to front during backpropagation.Define your dataset and train
Set up the four XOR input/output pairs and call Each call to
gradient_descent_epoch with a learning rate and an epoch count. The function prints the loss and accuracy after every epoch and returns the updated network along with the full loss history:gradient_descent_epoch performs a complete forward pass, backward pass, and gradient-descent weight update for every sample in x on every epoch. The returned loss list contains one scalar loss value per epoch, making it straightforward to visualize convergence.Complete XOR training script
The code block below combines all of the steps above into a single, self-contained script you can run directly.Running inference after training
Once training is complete, you can run the network on any input by loading values into the input layer and triggering a forward pass through every layer. The final output is retrieved from the last layer viaoutput():
[0,0] and [1,1] and close to 1 for inputs [0,1] and [1,0].
Using gradient_descent_threshold instead
If you prefer to train until a quality target is met rather than for a fixed number of epochs, swap ingradient_descent_threshold and provide a target loss value instead of an epoch count:
loss ≤ 0.01 is reached, or immediately if the loss starts increasing between consecutive epochs — a built-in safeguard against divergence.