Skip to main content

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(in_shape=4, out_shape=32),
  ntk.Relu(),
  ntk.Dense(in_shape=32, out_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(in_shape=2, out_shape=4),
    ntk.Tanh(),
    ntk.Dense(in_shape=4, out_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()]
with ntk.no_grad():
    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(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=0),
    ntk.Relu(),
    ntk.Adaptive_Max_Pool2d(H_out=13, W_out=13),
    ntk.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=0),
    ntk.Relu(),
    ntk.Adaptive_Max_Pool2d(H_out=5, W_out=5),
    ntk.Flatten(),
    ntk.Dense(in_shape=1600, out_shape=128),
    ntk.Relu(),
    ntk.Dense(in_shape=128, out_shape=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.ntk")
print("Model Saved!")

Metadata

Release files for ntk-ml 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ntk-ml 1.0.6
File Size Uploaded
ntk_ml-1.0.6.tar.gz 40.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ntk-ml 1.0.6
File Interpreter ABI Platform
ntk_ml-1.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 95.3 kB

Release files / ntk_ml-1.0.6.tar.gz

Download URL ntk_ml-1.0.6.tar.gz
Size 40.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ac4d832575a4755a9fe5739005627c50f64e3a5bb5e1d716944a3a463397e1c7
BLAKE2b-256 checksum
How to use checksums
0c37a66726dc9823421de28dce01b6de377be576205a68639e5dd436bc2e80cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.14

Release files / ntk_ml-1.0.6-py3-none-any.whl

Download URL ntk_ml-1.0.6-py3-none-any.whl
Size 54.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d01fd46bf00eb23d73a4ddb7c47bccecdb850dd7e9cf21060d43497bdf3c874a
BLAKE2b-256 checksum
How to use checksums
92fcb06caa19f079471518ff21a7ac8a70f90bf16d95db72760ec3f125887bbd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.14

Release history Release notifications | RSS feed

1.0.12

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

This release

1.0.6 This release

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page