neural_scratch
neural_scratch is a high-performance, educational neural network library built entirely from scratch using pure NumPy.
It provides a clean, Keras-like object-oriented API for building and training deep neural networks. To guarantee blazing-fast execution speeds and strong protection against reverse engineering, all the core math and backpropagation logic is compiled directly into native C-extensions via Cython.
Table of Contents
Installation
Install directly from PyPI:
pip install neural_scratch
Note: Because neural_scratch distributes raw C source code for cross-platform compatibility, your system will automatically compile it during installation. You must have a C compiler installed on your system.
How It Works
Building a model in neural_scratch follows a simple 4-step pipeline:
- Instantiate a
Sequentialmodel. - Add your
Denselayers andActivationlayers sequentially. - Configure the loss function and its derivative using
.use(). - Train the model using
.fit()with your data, epochs, and learning rate.
Core Components Reference
The Model (Sequential)
The Sequential class is the container for your network. It passes data forward through your layers during prediction and backwards during training to update weights.
Methods:
add(layer): Appends a layer (Dense or Activation) to the network.use(loss, loss_prime): Sets the loss function and its derivative for training.predict(input_data): Runs a forward pass on an array of inputs sequentially, returning a list of outputs for each sample.predict_batch(input_data): Runs a vectorized forward pass on an entire batch at once (much faster, recommended for evaluation).fit(x_train, y_train, epochs, learning_rate, batch_size=None, verbose=True):x_train,y_train: Your NumPy training data.epochs: Integer. How many times to loop over the entire dataset.learning_rate: Float. How big of a step the optimizer takes during gradient descent. Too high causes instability; too low causes slow learning.batch_size: Integer (Optional). If provided, trains using mini-batches (stochastic gradient descent), which is much faster and helps escape local minima. If omitted, uses full-batch gradient descent.verbose: Boolean. If True, prints a progress bar (viatqdm) and the mean error at each epoch.
Layers (Dense)
The Dense layer is a standard fully connected neural network layer where every input neuron is connected to every output neuron.
- Syntax:
Dense(input_size, output_size, seed=None) - Parameters:
input_size: The number of neurons coming in to this layer.output_size: The number of neurons out of this layer.seed(Optional): An integer to lock the random number generator for reproducible weight initialization.
- Note:
Denseautomatically uses He Initialization to optimally scale random weights based on layer size, preventing the vanishing/exploding gradient problem.
Activations
Activation functions introduce non-linearity, allowing the network to learn complex patterns instead of just straight lines. You add them directly after a Dense layer.
ReLU(): (Rectified Linear Unit). Outputs the input if positive, otherwise 0. Best default for hidden layers.Sigmoid(): Squashes outputs to a range between 0 and 1. Best for binary classification (yes/no).Tanh(): Squashes outputs to a range between -1 and 1. Usually outperforms Sigmoid in hidden layers.Softmax(): Converts a vector of raw scores (logits) into a probability distribution that sums to 1. Best for the final layer in multi-class classification.
Loss Functions
The loss function measures how "wrong" the network's predictions are. The network's goal is to minimize this value.
- Mean Squared Error (MSE)
- Functions:
mse,mse_prime - Use case: Regression tasks (predicting continuous numbers) or simple binary classification.
- Functions:
- Softmax Cross-Entropy
- Class:
SoftmaxCrossEntropy() - Use case: Multi-class classification (like identifying digits 0-9).
- Note: This class elegantly combines Softmax and Cross-Entropy internally for extreme numerical stability. Do not add a
Softmax()activation layer to your model if you are usingSoftmaxCrossEntropy()as your loss function. The loss function handles the Softmax calculation mathematically during backpropagation.
- Class:
Optimizers
SGD(learning_rate): Stochastic Gradient Descent. (Currently, the SGD logic is built directly into theSequential.fitmethod for efficiency based on your providedlearning_rateandbatch_sizeparameters).
Complete Tutorials
1. Solving the XOR Problem
The XOR problem is a classic neural network benchmark. It requires at least one hidden layer with a non-linear activation because the data is not linearly separable.
import numpy as np
from neural_scratch import Sequential, Dense, Tanh, mse, mse_prime
# 1. Prepare Data
# Inputs (4 samples, 2 features)
X_train = np.array([[[0,0]], [[0,1]], [[1,0]], [[1,1]]])
# Expected Outputs (4 samples, 1 feature)
y_train = np.array([[[0]], [[1]], [[1]], [[0]]])
# 2. Build Model
model = Sequential()
model.add(Dense(2, 3)) # Input: 2, Hidden Neurons: 3
model.add(Tanh()) # Hidden Activation
model.add(Dense(3, 1)) # Hidden: 3, Output: 1
model.add(Tanh()) # Output Activation
# 3. Configure & Train
model.use(mse, mse_prime)
model.fit(X_train, y_train, epochs=1000, learning_rate=0.1, verbose=True)
# 4. Predict
predictions = model.predict(X_train)
print("Predictions:\\n", predictions)
2. Multi-class Classification (MNIST)
For classifying images into multiple distinct categories (like identifying handwritten digits from 0 to 9), we use mini-batches and SoftmaxCrossEntropy.
import numpy as np
from neural_scratch import Sequential, Dense, ReLU, SoftmaxCrossEntropy
# Assume X_train is (60000, 784) and Y_train is one-hot encoded (60000, 10)
model = Sequential()
model.add(Dense(784, 128)) # Flattened 28x28 image = 784 inputs
model.add(ReLU())
model.add(Dense(128, 64))
model.add(ReLU())
model.add(Dense(64, 10)) # 10 output classes (digits 0-9)
# Use SoftmaxCrossEntropy for stable multi-class probability loss
loss_fn = SoftmaxCrossEntropy()
model.use(loss_fn, loss_fn.prime)
# Train using mini-batches for significant speedups
model.fit(X_train, Y_train, epochs=10, learning_rate=0.01, batch_size=32)
# To evaluate the model, get raw logits from the network via predict_batch
logits = model.predict_batch(X_test)
# Apply Softmax manually to the output to view final probabilities
from neural_scratch import Softmax
probabilities = Softmax().forward(logits)
predictions = np.argmax(probabilities, axis=1)
Metadata
Release files for neural-scratch 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| neural_scratch-0.1.3.tar.gz | 343.9 kB | Without provenance | Details for neural_scratch-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| neural_scratch-0.1.3-cp314-cp314-macosx_26_0_arm64.whl | CPython 3.14 | CPython 3.14 | macOS 26.0+ ARM64 | Details for neural_scratch-0.1.3-cp314-cp314-macosx_26_0_arm64.whl |
Total release size: 480.9 kB
Release files / neural_scratch-0.1.3.tar.gz
| Download URL | neural_scratch-0.1.3.tar.gz |
|---|---|
| Size | 343.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3e7a9b2b5eaf921af7becf5033acf511ed781f829eff72214804f44e2ccb01e3
|
|
BLAKE2b-256 checksum How to use checksums |
28a3cbe5196d8317ed451dc250f0eef492023c2b4a032e1652115d0f798870da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Release files / neural_scratch-0.1.3-cp314-cp314-macosx_26_0_arm64.whl
| Download URL | neural_scratch-0.1.3-cp314-cp314-macosx_26_0_arm64.whl |
|---|---|
| Size | 137.0 kB |
| Tags | CPython 3.14 macOS 26.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
a14a8ba5035b7908f5bde961a7b8ab7beecccb77e4c8538d1917c472e07eebf0
|
|
BLAKE2b-256 checksum How to use checksums |
88251bb0c97dc89851875426db7f5fa1474da95c05979a9e5ab7007169a02683
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Security for neural-scratch 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.