Skip to main content

A minimal deep learning library using numpy.

Project description

torchling

A minimal neural network library. torchling is a work in progress.

Installation

pip install torchling

Example: Boston Housing Regression

import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler

from torchling.models import Sequential
from torchling.layers import Linear, Tanh
from torchling.losses import MSE
from torchling.optimisers import SGD


def load_dataset():
    url = "https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data"
    df = pd.read_csv(url, sep=r'\s+')
    X = df.iloc[:, :-1].values.astype(np.float64)
    Y = df.iloc[:, -1].values.astype(np.float64)
    return X, Y


def normalize(X, Y):
    scaler_X = StandardScaler()
    scaler_Y = StandardScaler()
    X_norm = scaler_X.fit_transform(X)
    Y_norm = scaler_Y.fit_transform(Y.reshape(-1, 1)).flatten()
    return X_norm, Y_norm, scaler_X, scaler_Y


def denormalize(scaler, arr):
    return scaler.inverse_transform(np.array(arr).reshape(-1, 1)).flatten()


X, Y = load_dataset()
X, Y, _, Y_metadata = normalize(X, Y)

X_TRAIN, Y_TRAIN = X[1:], Y[1:]
X_TEST, Y_TEST = X[:1], Y[0]

epochs = 3000
lr = 0.6
input_size = X_TRAIN.shape[1]

model = Sequential(
    Linear(50, input_size=input_size),
    Tanh(),
    Linear(50),
    Tanh(),
    Linear(1),
)

optimizer = SGD(alpha=lr)
loss = MSE()
model.train(X_TRAIN, Y_TRAIN, optimizer, loss, epochs=epochs, print_every=250)

pred = model.predict(X_TEST)
print(f"Prediction - {denormalize(Y_metadata, pred)} | Actual - {denormalize(Y_metadata, [Y_TEST])}")

License

MIT

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

torchling-0.1.14.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

torchling-0.1.14-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file torchling-0.1.14.tar.gz.

File metadata

  • Download URL: torchling-0.1.14.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for torchling-0.1.14.tar.gz
Algorithm Hash digest
SHA256 c7411a82e11455192b5c70e2990cd493f5a0849f0b27fa500b4c0066e6358d88
MD5 61ffac9647975af49f4d37dc00979622
BLAKE2b-256 9fc7c01f57ae5d93dc69a4693fcf58b3a7e0e4e83b47c0d07527cf533ac99b14

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchling-0.1.14.tar.gz:

Publisher: release.yaml on pbrut/torchling

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torchling-0.1.14-py3-none-any.whl.

File metadata

  • Download URL: torchling-0.1.14-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for torchling-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 23a70eef9c7c7e475d99b0751438af275d831b69003242b3bbceb919d11564b8
MD5 a69c2dda75b89e5d7908e0649ff9c3ea
BLAKE2b-256 e968190540b9179bede6e8c1fe6f63f73d8b78e55ad51f4c032ddc64d2c6c69c

See more details on using hashes here.

Provenance

The following attestation bundles were made for torchling-0.1.14-py3-none-any.whl:

Publisher: release.yaml on pbrut/torchling

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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