FT-TensorLib v0.1.0 Released on PyPI • Zero External Dependencies • Pure Python Engine
Developer Core • Open Source Engine

TensorLib

A zero-dependency open-source Machine Learning & Deep Learning library engineered completely from scratch in Python. Delivering PyTorch and Scikit-Learn style abstractions with dynamic backpropagation, neural operators, and classical ML algorithms.

0 Dependencies
100% Pure Python
DAG Autograd Engine
PACKAGE INSTALLATION PyPI VERIFIED
bash — pip install
$ pip install ft-tensorlib
Successfully installed ft-tensorlib-0.1.0
$ python -c "import tensorlib as tl; print(tl.Tensor([1, 2, 3]))"
Tensor([1.0, 2.0, 3.0], shape=(3,))
System Architecture

Uses row-major contiguous list representation, stride index maps, dynamic topological sort, and automated reverse backpropagation.

Engine Architecture

From Raw Tensors to Autonomous Backpropagation

Built with modularity and educational transparency at its core.

Core Storage tensorlib.Tensor
Graph Tracker Context & DAG
Diff Engine autograd.backward
Optimization tensorlib.optim

To Be Mentioned

Core Tensor Engine

N-dimensional array storage with row-major memory layouts, stride indexing, broadcasting, and matrix multiplication.

Reverse Autograd

Dynamic computational graph tracking, topological sorting, and reverse backpropagation via .backward().

Neural Network Layers

Dense (Linear), Conv2D, MaxPool2D, Sequential, Dropout, BatchNorm1d, Embeddings, LayerNorm, and RMSNorm.

Modern Optimizers

Complete optimizer suite including SGD with momentum, Adam, AdamW with decoupled weight decay, and RMSprop.

Classical ML Suite

Linear/Logistic Regression, Decision Trees, Random Forest, K-Means Clustering, K-NN, and PCA built on Tensor primitives.

Data & Utilities

TensorDataset, DataLoader mini-batching, StandardScaler, OneHotEncoder, graph visualizer (render_graph), and model saving.

Quickstart Code Examples

1. Tensor Math & Computational Graph (Autograd)

Compute forward pass, render ASCII computational graph, and backpropagate gradients.

Python
from tensorlib import Tensor
from tensorlib.utils import render_graph

# Create tensors with autograd enabled
x = Tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
W = Tensor([[0.5, -0.5], [1.0, 2.0]], requires_grad=True)

# Forward matrix multiplication and loss calculation
y = x @ W
loss = (y ** 2).sum()

# Print ASCII computational graph
print(render_graph(loss))

# Reverse Backpropagation
loss.backward()

print("x.grad:", x.grad)
print("W.grad:", W.grad)

2. Neural Network Model Training (Multi-Layer Perceptron)

Build custom sequential models with standard neural layers, optimizers, and losses.

Python
from tensorlib import Tensor
from tensorlib.nn import Sequential, Linear, ReLU, CrossEntropyLoss
from tensorlib.optim import Adam
from tensorlib.data import TensorDataset, DataLoader

# Define dataset
X = Tensor([[1.0, 2.0], [1.5, 1.8], [5.0, 5.0], [6.0, 7.0]])
y = Tensor([0.0, 0.0, 1.0, 1.0])

dataset = TensorDataset(X, y)
loader = DataLoader(dataset, batch_size=2, shuffle=True)

# Build model
model = Sequential(
    Linear(in_features=2, out_features=8),
    ReLU(),
    Linear(in_features=8, out_features=2)
)

optimizer = Adam(model.parameters(), lr=0.05)
criterion = CrossEntropyLoss()

# Training loop
for epoch in range(20):
    for batch_X, batch_y in loader:
        optimizer.zero_grad()
        logits = model(batch_X)
        loss = criterion(logits, batch_y)
        loss.backward()
        optimizer.step()

3. Classical Machine Learning Suite

Scikit-Learn style interface operating seamlessly over Tensor primitives.

Python
from tensorlib import Tensor
from tensorlib.ml import LogisticRegression, RandomForestClassifier, KMeans, PCA

X = Tensor([[1.0, 1.0], [1.5, 2.0], [6.0, 6.0], [7.0, 8.0]])
y = Tensor([0.0, 0.0, 1.0, 1.0])

# Logistic Regression
clf = LogisticRegression(lr=0.1, epochs=100).fit(X, y)
print("LogReg Predictions:", clf.predict(X).data)

# Random Forest Classifier
rf = RandomForestClassifier(n_estimators=5, max_depth=3).fit(X, y)
print("Random Forest Predictions:", rf.predict(X).data)

# K-Means Clustering
kmeans = KMeans(n_clusters=2, random_state=42).fit(X)
print("Cluster Assignments:", kmeans.predict(X).data)

# Principal Component Analysis
pca = PCA(n_components=1).fit(X)
X_reduced = pca.transform(X)
print("PCA Shape:", X_reduced.shape)

API Reference: Core & Autograd

tensorlib.Tensor(data, shape=None, requires_grad=False) Core Primitive

N-dimensional array with automated computational graph tracking and operator overloading.

.backward() .zero_grad() .matmul() .reshape() .transpose() .softmax() .to_list()
Factory Constructors tensorlib

Utility functions for instantiating initialized Tensors quickly.

zeros(shape) ones(shape) randn(shape, mean, std) randint(low, high, shape) arange(start, stop, step)

API Reference: Neural Networks (tensorlib.nn)

Category Components Description
Base Abstractions Module, Parameter Base class tracking weights, sub-modules, and training/eval state.
Layers Linear, Conv2D, MaxPool2D, Sequential, Flatten, Dropout, BatchNorm1d, Embedding, LayerNorm, RMSNorm Complete layer set for Feedforward, CNNs, and Transformer models.
Activations ReLU, Sigmoid, Tanh, Softmax, LeakyReLU, GELU, SiLU Differentiable non-linear transformation functions.
Loss Functions MSELoss, L1Loss, BCEWithLogitsLoss, CrossEntropyLoss Numerically stable loss functions for regression and multi-class target loss.
Transformer Engine GPT, TransformerBlock, CausalSelfAttention, FeedForward, GPTConfig Complete Causal Language Model supporting auto-regressive generation.

API Reference: Optimizers (tensorlib.optim)

SGD(params, lr=1e-3, momentum=0.0, weight_decay=0.0) Optimizer

Stochastic Gradient Descent with momentum velocity and weight decay regularization.

Adam(params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0.0) Optimizer

Adaptive Moment Estimation computing bias-corrected 1st and 2nd raw moment estimates.

AdamW(params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=1e-2) Optimizer

Adam with decoupled weight decay as introduced by Loshchilov & Hutter.

API Reference: Classical ML (tensorlib.ml)

Algorithm Class Key Parameters
Linear & Logistic Regression LinearRegression, LogisticRegression lr=0.01, epochs=1000
Decision Tree & Forest DecisionTreeClassifier, RandomForestClassifier max_depth=5, n_estimators=10
Clustering KMeans n_clusters=8, max_iters=300
K-Nearest Neighbors KNeighborsClassifier n_neighbors=5
Dimensionality Reduction PCA n_components=2

API Reference: Data & Evaluation Metrics

tensorlib.data Data Utilities

Dataset loaders, preprocessors, and tokenizers.

TensorDataset(X, y) DataLoader(dataset, batch_size, shuffle) StandardScaler() OneHotEncoder() CharTokenizer() BPETokenizer()
tensorlib.metrics Evaluation

Standard performance evaluation metrics.

accuracy_score(y_true, y_pred) confusion_matrix(y_true, y_pred) precision_score() recall_score() f1_score() r2_score() mean_squared_error()

Repository Structure

TensorLib Repository Tree
TensorLib/
├── pyproject.toml        # Package metadata & build backend
├── README.md            # Documentation overview
├── tensorlib/           # Core Package Directory
│   ├── tensor.py       # N-dimensional Tensor & operator overloads
│   ├── autograd.py     # Reverse-mode autograd engine
│   ├── ops.py          # Pure-Python matrix & broadcasting routines
│   ├── nn/             # Neural network layers, activations, losses, Transformer
│   ├── optim/          # SGD, Adam, AdamW, RMSprop optimizers
│   ├── ml/             # Classical ML suite
│   ├── data/           # Datasets, DataLoader, Tokenizers
│   ├── metrics/        # Accuracy, F1, R2, Confusion Matrix
│   └── utils/          # ASCII graph renderer & model serialization
├── tests/              # 100% standard library unittest suite
└── examples/           # Runnable demonstration scripts

Running Tests & Examples

Run the unit test suite built entirely with Python standard library unittest:

Terminal
python -m unittest discover -s tests -p "test_*.py"

Run example scripts:

Terminal
python -m examples.01_tensor_autograd_basics
python -m examples.02_mlp_mnist_classification
python -m examples.03_classical_ml_regression_clustering
python -m examples.04_cnn_image_classifier
python -m examples.05_llm_gpt_training
Open Source Craft

Built for research, learning, and developer clarity.

TensorLib is licensed under MIT. Feel free to inspect, extend, or contribute to the source repository.

View on PyPI GitHub Repository