Skip to main content

A machine learning library written from scratch - with runtime switchable backend!

Project description

swi-ml

A machine learning library written from scratch - with runtime switchable backend!

Logo

Provides a single interface to interact with single-core CPU operations (with NumPy backend), as well as thousands of cores on a GPU (with CuPy backend), in runtime!

Python Code style Stargazers Codecov Personal Website MIT License

NOTE: This is NOT an alternative to libraries like scikit-learn and cuML. Their interfaces are complete on their own!

Prerequsites

swi-ml is built on bare Python and NumPy backbones, all other dependencies are optional!

Installation

  1. (Optional) Setup a virtual environment using virtualenv or anaconda.
  2. Install NumPy by following their insallation guide or simply via pip:
    pip install numpy
    
  3. (Optional) For GPU-supported backend, setup a working installation of CuPy by following their installation guide.
    python -c 'import cupy; cupy.show_config()'
    
  4. (Optional) Install Matplotlib to plot specific curves. (via their installation guide)
  5. Install swi-ml:
    pip install swi-ml  # from PyPI
    pip install git+https://github.com/aitikgupta/swi-ml  # from GitHub
    
  6. (Optional) To run the pre-defined tests, install pytest by following their installation guide or simply via pip:
    pip install pytest
    

Usage

Switching backend

from swi_ml import set_backend

# numpy backend (CPU)
set_backend("numpy")

# cupy backend (GPU)
set_backend("cupy")

Automatic fallback

Don't have a physical GPU, or don't know if you have a proper setup for a GPU-enabled backend?

Set automatic fallback (to NumPy - the only hard dependency):

from swi_ml import set_automatic_fallback

# this has been enabled by default for tests
# see https://github.com/aitikgupta/swi-ml/blob/master/tests/__init__.py
set_automatic_fallback(True)

A simple Linear Regression with Gradient Descent

from swi_ml.regression import LinearRegressionGD

data = [[1], [2], [3]]
labels = [2, 4, 6]

model = LinearRegressionGD(
    num_iterations=3,
    learning_rate=0.1,
    normalize=False,
    initialiser="uniform",
    verbose="DEBUG",
)

model.fit(data, labels)

print("Current MSE:", model.curr_loss)

Output:

INFO: Backend is not set, using default `numpy`
INFO: Setting backend: numpy
INFO: MSE (1/3): 13.93602
INFO: MSE (2/3): 0.22120
INFO: MSE (3/3): 0.05478
INFO: Training time: 0.00035 seconds
Current MSE: 0.054780625247184585

For more concrete examples, please refer to examples directory.

Running the tests

To run the testing suite, execute the following command in the root directory:

python -mpytest  # run the whole suite
python -mpytest tests/test_module.py  # run the specific test module

Contributing

Contributions are what makes the open source community such an amazing place to learn, inspire, and create. Any contributions are much appreciated!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Acknowledgements

About

Aitik Gupta - Personal Website

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

swi-ml-0.0.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

swi_ml-0.0.1-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file swi-ml-0.0.1.tar.gz.

File metadata

  • Download URL: swi-ml-0.0.1.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for swi-ml-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c55c970d1c4919bf9f24a8f0319ae6c9b50e6116e09b197ccb034a515b829c9d
MD5 14c04e69bc52c53cc6ff0301f5920ace
BLAKE2b-256 52cc7ee657006469ce61825e210cf8ba602337a9c4212d05f92a3bbb2008046a

See more details on using hashes here.

File details

Details for the file swi_ml-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: swi_ml-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for swi_ml-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7c230e513dc162916448c799ca0701abaeb1a92ebf69f3bc12b2992483bdac41
MD5 bb11223074612e8e602e54759290ef9d
BLAKE2b-256 f45e08905cf1646dbc2319f1479347a8442d15727f0c2ccec8e684e08bc9cb2d

See more details on using hashes here.

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