Skip to main content

A from-scratch neural network and tensor library built in Python for learning, experimentation, and deep understanding of modern ML systems.

Project description

PyPI version

NTK Banner

Neural Tool Kit (NTK)

Neural Tool Kit (NTK) is a machine learning framework built from scratch in Python on top of NumPy. The project aims to provide a hands-on exploration of the core systems behind modern deep learning frameworks, including tensors, automatic differentiation, neural network layers, optimization, and training workflows.

NTK was created as a learning-focused project to explore the fundamental systems behind modern deep learning frameworks. While it serves as a personal educational project, it may also be useful to students, hobbyists, and anyone interested in understanding how machine learning frameworks work internally.

Motivation

I built NTK to develop a deeper understanding of the systems that power modern deep learning frameworks. By implementing these components from scratch, I can explore how they work internally rather than treating them as black boxes.

Features

  • Tensor abstraction with automatic differentiation
  • Dense, Conv2D, and MaxPool layers
  • Built-in activation functions
    • ReLU
    • Sigmoid
    • Tanh
    • Softmax
    • Linear
  • Optimizers and loss functions
  • Dataset and DataLoader abstractions
  • Training utilities and trainer system
  • Sequential container module
  • Support for custom layers, activations, losses, and optimizers

Current Limitations

NTK is currently CPU-only and remains under active development. The following features are planned but not yet available:

  • GPU computation
  • Additional container modules
  • RNN support
  • Transformer support
  • A polished reinforcement learning API

Installation

python -m pip install ntk-ml

Import

import neuraltoolkit as ntk

Quickstart

import neuraltoolkit as ntk


x = ntk.Tensor(training_data)
y = ntk.Tensor(training_labels)

model = ntk.Sequential(
  ntk.Dense(input_shape=4, output_shape=32),
  ntk.Relu(),
  ntk.Dense(input_shape=32, output_shape=10),
  ntk.Tanh()
)

trainer = ntk.Trainer(
  module=model,
  optimizer=ntk.Adam(parameters=model.parameters(), learning_rate=3e-4),
  loss=ntk.MeanSquaredError()
)

trainer.fit(x, y, epochs=100)

predictions = model(X)

Examples

XOR

import neuraltoolkit as ntk
import numpy as np
import matplotlib.pyplot as plt

x = ntk.Tensor([
    [0, 0],
    [0, 1],
    [1, 0],
    [1, 1]
])

y = ntk.Tensor([
    [0],
    [1],
    [1],
    [0]
])

model = ntk.Sequential(
    ntk.Dense(input_shape=2, output_shape=4),
    ntk.Tanh(),
    ntk.Dense(input_shape=4, output_shape=1),
    ntk.Sigmoid()
)

trainer = ntk.Trainer(
    module=model,
    optimizer=ntk.Adam(parameters=model.parameters(), learning_rate=0.01),
    loss=ntk.BinaryCrossEntropy()
)


history = trainer.fit(x, y, epochs=500)
print(model(x))
history.plot("loss")

# ----------------------Visualizing-------------------------

# defining a boundary
x_min, x_max = 0, 1
y_min, y_max = 0, 1

#Creating the grid
step_size = 0.01
xx, yy = np.meshgrid(
    np.arange(x_min, x_max, step_size),
    np.arange(y_min, y_max, step_size)
)

grid_points = np.c_[xx.ravel(), yy.ravel()]
predictions = model(ntk.Tensor(grid_points))

Z = predictions.data.reshape(xx.shape)
print(Z)

# Plotting

plt.figure(figsize=(6, 5))
plt.contourf(xx, yy, Z, alpha=0.8, cmap="coolwarm")
plt.contour(xx, yy, Z, colors='k', levels=[0.5], linewidths=1.5)

plt.title("XOR Decision Boundary")
plt.show()

Mnist Digits (CNN)

import numpy as np
import neuraltoolkit as ntk

model = ntk.Sequential(
    ntk.Conv2d(1, 32, 3, 1, 0),
    ntk.Relu(),
    ntk.Adaptive_Max_Pool2d(13, 13),
    ntk.Conv2d(32, 64, 3, 1, 0),
    ntk.Relu(),
    ntk.Adaptive_Max_Pool2d(5, 5),
    ntk.Flatten(),
    ntk.Dense(1600, 128),
    ntk.Relu(),
    ntk.Dense(128, 10)
)

train_dataset, val_dataset = ntk.datasets.mnist()

trainer = ntk.Trainer(
    module=model,
    optimizer=ntk.Adam(parameters=model.parameters(), learning_rate=3e-4),
    loss=ntk.CategoricalCrossEntropy()
)

history = trainer.fit(
    data=train_dataset,
    epochs=1,
    validation_data=val_dataset,
    batch_size=32,
    shuffle=True
)

with ntk.no_grad():
    predictions = model(val_dataset.x)

predictions_argmax = np.argmax(predictions.data, axis=-1)
labels_argmax = np.argmax(val_dataset.y.data, axis=-1)

percentage = np.mean(predictions_argmax == labels_argmax) * 100
print(f"Test Accuracy: {percentage}%")

model.save(".model_conv")
print("Model Saved!")

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

ntk_ml-1.0.4.tar.gz (40.0 kB view details)

Uploaded Source

Built Distribution

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

ntk_ml-1.0.4-py3-none-any.whl (54.2 kB view details)

Uploaded Python 3

File details

Details for the file ntk_ml-1.0.4.tar.gz.

File metadata

  • Download URL: ntk_ml-1.0.4.tar.gz
  • Upload date:
  • Size: 40.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.14

File hashes

Hashes for ntk_ml-1.0.4.tar.gz
Algorithm Hash digest
SHA256 bb3cb4d2caa8da383b71aeea344503f0b7d1c599812181863ed03f7e82314096
MD5 747a980514bbfe05c53c276c5c3c7b9a
BLAKE2b-256 4cd17767e7e53a8db5bfc8da0e54b6e83c8f97dabfad0a1a7af64299854b8564

See more details on using hashes here.

File details

Details for the file ntk_ml-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: ntk_ml-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 54.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.14

File hashes

Hashes for ntk_ml-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e4cfcfb459ef01d4633427570e46ad7e932934408ee705b3cd5a5a4264f9285e
MD5 6c6879b21b45120ac9180780f876c72c
BLAKE2b-256 34ff1a66d93e6c812139c69e1951dd2084a3733eeab197373467c450257a7f5f

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