Skip to main content

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 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.1.tar.gz (342.1 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.1-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.1.tar.gz.

File metadata

  • Download URL: neural_scratch-0.1.1.tar.gz
  • Upload date:
  • Size: 342.1 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.1.tar.gz
Algorithm Hash digest
SHA256 b0f3e802e8598368c55de376f22f5c1ac5e4822fa05a868945c7c8befbc8f91c
MD5 32e6b36d88edd63a3ebdfcd3f3fc4a87
BLAKE2b-256 99d6430f542ad77c171b6999ffdf39aefb3c0d4383c7a8667cd8b3e6cb0c6ebc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for neural_scratch-0.1.1-cp314-cp314-macosx_26_0_arm64.whl
Algorithm Hash digest
SHA256 8773d37d81a14847c7dcd5fc98d807e18992ff2dca341330c80e23dcdc72c4aa
MD5 8ab3358e55a8f6e0c8b48aaad269fb8e
BLAKE2b-256 9626e0eda3b3908a1459918f5f22e4a5c3624b47289b145a6cdbd7188445389c

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