Skip to main content

A tiny 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 Sequential model structure that makes building networks incredibly easy.
  • C-Extension Compilation: Python code is compiled via Cython into native machine code .so objects, 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 the output_size of 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 by Sequential.fit()).

Anti-Reverse Engineering Security

This library distributes native C-Extensions instead of Python bytecode. All core logic (layers, backpropagation, and loss functions) is compiled via Cython into .so shared objects during the build process.

This design choice ensures that:

  1. Performance is maximized by removing Python interpreter overhead.
  2. Reverse Engineering is virtually impossible using standard Python decompilers (like uncompyle6, pycdc, etc.), as the distributed code is stripped of its Python AST and represented as optimized machine code.

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

neural_scratch-0.1.0.tar.gz (342.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

neural_scratch-0.1.0-cp314-cp314-macosx_26_0_arm64.whl (136.1 kB view details)

Uploaded CPython 3.14macOS 26.0+ ARM64

File details

Details for the file neural_scratch-0.1.0.tar.gz.

File metadata

  • Download URL: neural_scratch-0.1.0.tar.gz
  • Upload date:
  • Size: 342.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for neural_scratch-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c8aec982926a15857cd3181a153106b4d1628623c497702a5b07921d5bdfa3a4
MD5 a1812d27ed14143e75fd7fcd3447dbd4
BLAKE2b-256 b8110438b57846bd706bd5a63fb987e78ba987dac66c05496e299894374c499a

See more details on using hashes here.

File details

Details for the file neural_scratch-0.1.0-cp314-cp314-macosx_26_0_arm64.whl.

File metadata

File hashes

Hashes for neural_scratch-0.1.0-cp314-cp314-macosx_26_0_arm64.whl
Algorithm Hash digest
SHA256 3632b3cb540ee238e614d03bc9642e9ce8182b44e27d4ee59c79d65741a83b3e
MD5 77dad3c088f91264d3cdf70bb3056c73
BLAKE2b-256 f318991ae589cc8691de7ea440d47bae8c2e5d05266ab2a051d1d782108f4e6f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page