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.10.tar.gz (233.1 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.10-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sklx-0.0.10.tar.gz
Algorithm Hash digest
SHA256 344562b79d9ccff4f2ace38413ef832e9114f011c0544aa59165926561277bd0
MD5 0bfcd31570b904a20752acbb1b681128
BLAKE2b-256 6aad28afc458987f995ce8d4b99883c181c7975c7701e31781f0a111e06da552

See more details on using hashes here.

Provenance

The following attestation bundles were made for sklx-0.0.10.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.10-py3-none-any.whl.

File metadata

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

File hashes

Hashes for sklx-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 afe88b87cd5e717e1c708077d49c661c5a5a21a8f30ab4a5a1da999a571a28b3
MD5 7ef66a764274fb76edc7e3b2a288fc25
BLAKE2b-256 2d165a3e162d1bedb28a8cccc760fd8639eda5adc216c7f7bb3fb5235b7a14e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for sklx-0.0.10-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