A neural network library built from scratch with NumPy.
Project description
neural_scratch
A high-performance, educational neural network library built completely from scratch using NumPy.
neural_scratch provides a Keras-like object-oriented API for building and training neural networks. It is designed to be lightweight, avoiding heavy dependencies like TensorFlow or PyTorch, while leveraging highly-optimized C-Extensions (via Cython) for speed and strong protection against reverse engineering.
Table of Contents
Features
- Pure NumPy Math: Built entirely on standard matrix operations without heavy machine learning frameworks.
- Keras-like API: Intuitive
Sequentialmodel structure that makes building networks incredibly easy. - C-Extension Compilation: Python code is compiled via Cython into native machine code
.soobjects, rendering it practically impossible to decompile or reverse engineer. - Customizable: Control the exact size and shape of every layer and activation function.
Installation
You can install neural_scratch directly via pip once it is published to PyPI:
pip install neural_scratch
(Note: If building from source, ensure you have a C compiler installed, then run pip install . to compile the Cython extensions)
Quick Start
Here is a simple example demonstrating how to build a model to solve the XOR problem:
import numpy as np
from neural_scratch import Sequential, Dense, ReLU, SoftmaxCrossEntropy
from neural_scratch.activations import Softmax
# 1. Create Data (XOR problem)
X_train = np.array([[0,0], [0,1], [1,0], [1,1]])
Y_train = np.array([[1, 0], [0, 1], [0, 1], [1, 0]]) # One-hot encoded
# 2. Build the Model
model = Sequential()
# First layer: 2 input neurons (for the 2 XOR inputs), 3 output neurons
model.add(Dense(input_size=2, output_size=3))
model.add(ReLU())
# Second layer: 3 input neurons (must match previous layer), 2 output neurons
model.add(Dense(input_size=3, output_size=2))
# Note: We output raw logits directly to the loss function for numerical stability.
# 3. Compile and Train
loss = SoftmaxCrossEntropy()
model.use(loss, loss.prime)
model.fit(X_train, Y_train, epochs=1000, learning_rate=0.1, batch_size=4)
# 4. Predict
predictions = model.predict_batch(X_train)
# Apply softmax to raw logits to get final probabilities
probs = Softmax().forward(predictions)
print(probs)
API Reference
Models
Sequential()
The core container for stacking layers.
add(layer): Appends a layer (Dense or Activation) to the network.use(loss, loss_prime): Sets the loss function and its derivative.fit(x_train, y_train, epochs, learning_rate, batch_size, verbose): Trains the model.predict(input_data): Runs a forward pass on individual samples.predict_batch(input_data): Runs a vectorized forward pass on a batch of samples.
Layers
Dense(input_size, output_size, seed=None)
A standard fully-connected neural network layer.
input_size: The number of input neurons. This must match theoutput_sizeof the previous layer, or the feature dimension of your dataset for the first layer.output_size: The number of output neurons.- Uses He Initialization automatically to prevent vanishing or exploding gradients.
Activations
You can append activation functions directly to your Sequential model:
ReLU(): Rectified Linear Unit. The standard for hidden layers.Sigmoid(): Squashes outputs to a[0, 1]range.Tanh(): Squashes outputs to a[-1, 1]range.Softmax(): Converts a vector of logits into a probability distribution.
Losses
Loss functions are used to calculate the network's error.
mse(y_true, y_pred)&mse_prime(y_true, y_pred): Mean Squared Error.categorical_crossentropy(y_true, y_pred)&categorical_crossentropy_prime(y_true, y_pred): Cross-Entropy loss.SoftmaxCrossEntropy(): A highly recommended class that combines Softmax and Cross-Entropy for optimal numerical stability during backpropagation.
Optimizers
SGD(learning_rate): Stochastic Gradient Descent (used automatically bySequential.fit()).
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file neural_scratch-0.1.2.tar.gz.
File metadata
- Download URL: neural_scratch-0.1.2.tar.gz
- Upload date:
- Size: 341.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8f9010e60871c9a5de6bafd945c322cc5b0bf603054e447b67c5351463f133b
|
|
| MD5 |
45f1bd250722262cebf680f27ab2e1bf
|
|
| BLAKE2b-256 |
cc3c043d946743dc46d2b9027ec822fb745d67c501e3b6ed5f242e7774d52950
|
File details
Details for the file neural_scratch-0.1.2-cp314-cp314-macosx_26_0_arm64.whl.
File metadata
- Download URL: neural_scratch-0.1.2-cp314-cp314-macosx_26_0_arm64.whl
- Upload date:
- Size: 135.9 kB
- Tags: CPython 3.14, macOS 26.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3ddfb7666d53bb4f90d667d3a1c65a19f079721b9d828bc5907ad1f2a62a893
|
|
| MD5 |
8511c3e589f27fb636590337acfa3992
|
|
| BLAKE2b-256 |
bc5e9f28023ccca15fd246c7f0afdf385501729debed35b6802ecc1092b89e93
|