Skip to main content

A scikit-learn compatible neural network library that wraps MLX.

Project description

SKLX

Ruff Pytest image

A scikit-learn compatible neural network library that wraps MLX. Highly inspired by skorch.

Examples

import numpy as np
from sklearn.datasets import make_classification
from mlx import nn
from sklx import NeuralNetClassifier

X, y = make_classification(1000, 20, n_informative=10, random_state=0)
X = X.astype(np.float32)
y = y.astype(np.int64)

class MyModule(nn.Module):
    def __init__(self, num_units=10, nonlin=nn.ReLU()):
        super().__init__()

        self.dense0 = nn.Linear(20, num_units)
        self.nonlin = nonlin
        self.dropout = nn.Dropout(0.5)
        self.dense1 = nn.Linear(num_units, num_units)
        self.output = nn.Linear(num_units, 2)
        self.softmax = nn.Softmax(dim=-1)

    def forward(self, X, **kwargs):
        X = self.nonlin(self.dense0(X))
        X = self.dropout(X)
        X = self.nonlin(self.dense1(X))
        X = self.softmax(self.output(X))
        return X

net = NeuralNetClassifier(
    MyModule,
    max_epochs=10,
    lr=0.1,
)

net.fit(X, y)
y_proba = net.predict_proba(X)

In an sklearn Pipeline:

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

pipe = Pipeline([
    ('scale', StandardScaler()),
    ('net', net),
])

pipe.fit(X, y)
y_proba = pipe.predict_proba(X)

With grid search:

from sklearn.model_selection import GridSearchCV

params = {
    'lr': [0.01, 0.02],
    'max_epochs': [10, 20],
    'module__num_units': [10, 20],
}
gs = GridSearchCV(net, params, refit=False, cv=3, scoring='accuracy', verbose=2)

gs.fit(X, y)
print("best score: {:.3f}, best params: {}".format(gs.best_score_, gs.best_params_))

Future Roadmap

  1. Completing Feature Parity with Skorch
    1. Pipeline Support
    2. Grid Search Support
    3. Learning Rate Scheduler https://github.com/lazarust/sklx/issues/6
    4. Scoring https://github.com/lazarust/sklx/issues/7
    5. Early Stopping https://github.com/lazarust/sklx/issues/8
    6. Checkpointing https://github.com/lazarust/sklx/issues/9
    7. Parameter Freezing https://github.com/lazarust/sklx/issues/10
    8. Progress Bar https://github.com/lazarust/sklx/issues/11

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

sklx-0.0.9.tar.gz (233.0 kB view details)

Uploaded Source

Built Distribution

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

sklx-0.0.9-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file sklx-0.0.9.tar.gz.

File metadata

  • Download URL: sklx-0.0.9.tar.gz
  • Upload date:
  • Size: 233.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sklx-0.0.9.tar.gz
Algorithm Hash digest
SHA256 89773ac98259c00e0781ad31fd6642be87569112b57454cd5271fb1106f4234b
MD5 2ae71a564fc74365a5548eefc74b3267
BLAKE2b-256 ed66dce9d8948a43fe490a454ff0a62fd4900b2c311b760bcea755b07ae94abc

See more details on using hashes here.

Provenance

The following attestation bundles were made for sklx-0.0.9.tar.gz:

Publisher: python-publish.yml on lazarust/sklx

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

File details

Details for the file sklx-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: sklx-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sklx-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 3e8f1b1ba2f76a69d5e86c055c94998d17d1dfdb125defc2fd29ce09bc7a023b
MD5 a57aae1b6b512d94bc2d4b80edc9c6d4
BLAKE2b-256 5548745fe26f7c709c530cc5a47a38ad2b1bf1f945f9b6cde0aa2bb6279133e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for sklx-0.0.9-py3-none-any.whl:

Publisher: python-publish.yml on lazarust/sklx

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