Skip to main content

A lightweight deep learning framework built from scratch using NumPy

Project description

Kronyx

A lightweight deep learning framework built from first principles using NumPy. Designed for education, research, and production use with a clean Keras-like API.

PyPI Documentation Python 3.10+ License: MIT Tests Ruff mypy

Features

Feature Description
Pure NumPy No external ML dependencies, just NumPy
Clean API Keras-like Sequential model interface
Educational Built for learning with visualization and inspection tools
Layers Dense, Conv2D, Flatten, Dropout, BatchNormalization
Activations ReLU, Sigmoid, Tanh, Softmax
Optimizers SGD, Adam with full state management
Callbacks EarlyStopping, ModelCheckpoint, CSVLogger, ReduceLROnPlateau
Serialization Save/load models with .krx format
Visualization history.plot(), model.visualize(), model.summary()

Installation

pip install kronyx

For development:

git clone https://github.com/Kronyx/kronyx.git
cd kronyx
pip install -e ".[dev]"

Quick Start

import numpy as np
from kronyx import Sequential, Dense, ReLU, Sigmoid, BinaryCrossEntropy, Adam, Accuracy

# XOR problem - binary classification
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])

model = Sequential()
model.add(Dense(2, 8))  # input_size=2, output_size=8
model.add(ReLU())
model.add(Dense(8, 1))
model.add(Sigmoid())

model.compile(
    loss=BinaryCrossEntropy(),
    optimizer=Adam(learning_rate=0.1),
    metric=Accuracy()
)

model.fit(X, y, epochs=1000)
predictions = model.predict(X)
print(f"Accuracy: {(predictions.round() == y).mean():.2%}")

Binary Classification Example

import numpy as np
from kronyx import Sequential, Dense, ReLU, Sigmoid, BinaryCrossEntropy, Adam, Accuracy

X_train = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y_train = np.array([[0], [1], [1], [0]])

model = Sequential()
model.add(Dense(2, 16))
model.add(ReLU())
model.add(Dense(16, 1))
model.add(Sigmoid())

model.compile(
    loss=BinaryCrossEntropy(),
    optimizer=Adam(learning_rate=0.1),
    metric=Accuracy()
)

history = model.fit(X_train, y_train, epochs=500)
model.summary()

Multi-class Classification Example

import numpy as np
from kronyx import Sequential, Dense, ReLU, SoftmaxCategoricalCrossEntropy, Adam, Accuracy

# One-hot encoded labels
X = np.random.randn(100, 4)
y = np.eye(3)[np.random.randint(0, 3, 100)]

model = Sequential()
model.add(Dense(4, 32))
model.add(ReLU())
model.add(Dense(32, 3))
model.add(Softmax())

model.compile(
    loss=SoftmaxCategoricalCrossEntropy(),
    optimizer=Adam(learning_rate=0.01),
    metric=Accuracy()
)

model.fit(X, y, epochs=100)

Convolutional Neural Network Example

import numpy as np
from kronyx import Sequential, Conv2D, ReLU, Flatten, Dense, Softmax

# Simple image input (batch, height, width, channels)
X = np.random.randn(10, 8, 8, 1)
y = np.eye(2)[np.random.randint(0, 2, 10)]

model = Sequential()
model.add(Conv2D(filters=8, kernel_size=3, padding='same'))
model.add(ReLU())
model.add(Flatten())
model.add(Dense(64, 2))
model.add(Softmax())

model.compile(
    loss=SoftmaxCategoricalCrossEntropy(),
    optimizer=Adam(learning_rate=0.01),
    metric=Accuracy()
)

model.fit(X, y, epochs=10)

Saving and Loading Models

# Save complete model with architecture, weights, and configuration
model.save('model.krx')

# Load complete model
loaded = load_model('model.krx')

# Continue training or make predictions
loaded.predict(X_test)

Saving and Loading Weights

# Save only trainable weights
model.save_weights('weights.npz')

# Create a new model with matching architecture
new_model = Sequential()
new_model.add(Dense(2, 8))
new_model.add(ReLU())
new_model.add(Dense(8, 1))
new_model.add(Sigmoid())

# Load weights into the new model
new_model.load_weights('weights.npz')

Exporting JSON Architecture

# Export architecture to JSON string
json_str = model.to_json()

# Create model from JSON (weights initialized randomly)
model = Sequential.from_json(json_str)

.krx Archive Format

The .krx format is a zip archive containing:

model.krx
├── metadata.json      # Framework version, Python/numpy versions, timestamp
├── architecture.json  # Layer configuration, loss, optimizer settings
├── weights.npz        # Trainable weights and biases (numpy archive)
└── optimizer.npz      # Optional: optimizer state for resumable training

Examples

  • examples/xor.py - XOR problem with binary classification
  • examples/iris_classification.py - Iris dataset multi-class classification
  • examples/iris_dropout.py - Dropout regularization example
  • examples/iris_l2.py - L2 weight regularization
  • examples/batchnorm_iris.py - Batch normalization demonstration
  • examples/flatten_demo.py - Multi-dimensional input handling
  • examples/conv2d_demo.py - Convolutional neural network example
  • examples/mnist_classifier.py - MNIST digit classification

Documentation

kronyx/
├── activations.py    # ReLU, Sigmoid, Tanh, Softmax
├── layers.py         # Dense, Conv2D, Flatten, Dropout, BatchNormalization
├── model.py          # Sequential, History
├── optimizers.py     # SGD, Adam
├── losses.py         # BinaryCrossEntropy, CategoricalCrossEntropy
├── metrics.py        # Accuracy
├── callbacks.py      # Callback base, EarlyStopping, etc.
├── regularizers.py   # L2
├── initializers.py   # he_normal, xavier_uniform, lecun_normal
├── serialization.py  # Save/load model (.krx format)
├── utils.py          # Utility functions
└── exceptions.py     # Error types

Contributing

See CONTRIBUTING.md for development guidelines.

Roadmap

See ROADMAP.md for planned features.

License

MIT License - see LICENSE

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

kronyx-1.0.0.tar.gz (31.9 kB view details)

Uploaded Source

Built Distribution

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

kronyx-1.0.0-py3-none-any.whl (26.8 kB view details)

Uploaded Python 3

File details

Details for the file kronyx-1.0.0.tar.gz.

File metadata

  • Download URL: kronyx-1.0.0.tar.gz
  • Upload date:
  • Size: 31.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for kronyx-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8a67585861e6d7f2bfff9fc4d88ec36d31ae273a3e3d30dfaf99e902fa262326
MD5 3a5b4e079137c95e71f98bee3dc67b6b
BLAKE2b-256 57ebe547c5e462216cec85ce0dc850a0fbad91b46738ce8ea59d29c15d9b3485

See more details on using hashes here.

File details

Details for the file kronyx-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: kronyx-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 26.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for kronyx-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c572995728875bcd2deb3cc7f0b79fda9626d072654a52d6d9ae67910b920275
MD5 ffd7ab97265df7f2a104cf4cb904da2e
BLAKE2b-256 e43003ab873ce0a1cc48713fb9141bb9ec809c45cae8bf101e45956aecee5c93

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