A minimal deep learning library using numpy.
Project description
Torchling is a minimal neural network library inspired by PyTorch, Keras, and micrograd, created for educational purposes. Vectorisation and N-dimensional array handling is powered by NumPy.
torchling is a work in progress.
Installation
pip install torchling
Initial Roadmap
- Reverse-mode automatic differentiation
- ✅ Basic arithmetic operations
- ✅ Matrix multiplication
- Layers
- ✅ Linear
- Convolutional (2D)
- Batch normalisation
- ✅ Softmax
- ✅ Relu
- ✅ Sigmoid
- ✅ Tanh
- Models
- ✅ Sequential
- Decoder-only Transformer
- Losses
- ✅ Mean Squared Error
- ✅ Categorical Cross-Entropy
- Optimisers
- ✅ Stochastic gradient descent
- Adam
- Weight initialisation
- ✅ Drawn from normal distribution
- Xavier/Glorot initialisation
- Regularisation
- L1
- L2
- Dropout
- Miscellaneous
- Skip connections
Example
Simple example of training on the MNIST dataset.
import numpy as np
import tensorflow as tf
from torchling.models import Sequential
from torchling.layers import Linear, Relu, Softmax
from torchling.losses import CCE
from torchling.optimisers import SGD
def load_mnist():
(X_train, Y_train), (X_test, Y_test) = tf.keras.datasets.mnist.load_data()
# Preprocess data
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1]*X_train.shape[2]).astype(np.float64) / 255.0
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1]*X_test.shape[2]).astype(np.float64) / 255.0
Y_train_one_hot = np.eye(10)[Y_train]
return X_train, Y_train_one_hot, X_test, Y_test
X, Y, X_test, Y_test = load_mnist()
input_size = X.shape[1]
num_classes = Y.shape[1]
epochs = 30
batch_size = 256
layers = [256, 256]
print_every = 5
lr = 0.09
model = Sequential(
Linear(layers[0], input_size=input_size),
Relu(),
Linear(layers[1]),
Relu(),
Linear(num_classes),
Softmax(),
)
optimizer = SGD(alpha=lr)
loss = CCE()
model.train(X, Y, optimizer, loss, epochs=epochs, batch_size=batch_size, print_every=print_every)
probabilities = model.predict(X_test)
prediction = np.argmax(probabilities, axis=0)
accuracy = np.mean(prediction == Y_test)
print(f"Prediction accuracy: {accuracy * 100:.2f}%")
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torchling-1.0.0.tar.gz.
File metadata
- Download URL: torchling-1.0.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e2367fdb332380e4a1b9c1cafcd5f7596fb8105177293d9428043c8119a52ac
|
|
| MD5 |
182f393a47152a08ee5d42f2bf689d05
|
|
| BLAKE2b-256 |
441b867bbef7ed5f735b8e6b67bc01cc857d9b40ab450b6998dc9be8ccc272f3
|
Provenance
The following attestation bundles were made for torchling-1.0.0.tar.gz:
Publisher:
release.yaml on pbrut/torchling
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchling-1.0.0.tar.gz -
Subject digest:
1e2367fdb332380e4a1b9c1cafcd5f7596fb8105177293d9428043c8119a52ac - Sigstore transparency entry: 436189911
- Sigstore integration time:
-
Permalink:
pbrut/torchling@8508bda3c49a224a5e22135befb7fba5bdaabe5d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/pbrut
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@8508bda3c49a224a5e22135befb7fba5bdaabe5d -
Trigger Event:
push
-
Statement type:
File details
Details for the file torchling-1.0.0-py3-none-any.whl.
File metadata
- Download URL: torchling-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4d38568ffba0919bf73242dbc007368bba6a0cffec066172a28cfc266b0e8fe
|
|
| MD5 |
f55993f4596fb38b219c30b6df36a65e
|
|
| BLAKE2b-256 |
266f63bbc8dc2db1f52151a68d7870fddec885e5a099f3ca06c2a9f8d7002cab
|
Provenance
The following attestation bundles were made for torchling-1.0.0-py3-none-any.whl:
Publisher:
release.yaml on pbrut/torchling
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torchling-1.0.0-py3-none-any.whl -
Subject digest:
f4d38568ffba0919bf73242dbc007368bba6a0cffec066172a28cfc266b0e8fe - Sigstore transparency entry: 436189944
- Sigstore integration time:
-
Permalink:
pbrut/torchling@8508bda3c49a224a5e22135befb7fba5bdaabe5d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/pbrut
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@8508bda3c49a224a5e22135befb7fba5bdaabe5d -
Trigger Event:
push
-
Statement type: