• Stars
    star
    113
  • Rank 310,115 (Top 7 %)
  • Language
    Python
  • Created over 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Convert scikit-learn models to PyTorch modules

sk2torch

sk2torch converts scikit-learn models into PyTorch modules that can be tuned with backpropagation and even compiled as TorchScript.

Problems solved by this project:

  1. scikit-learn cannot perform inference on a GPU. Models like SVMs have a lot to gain from fast GPU primitives, and converting the models to PyTorch gives immediate access to these primitives.
  2. While scikit-learn supports serialization through pickle, saved models are not reproducible across versions of the library. On the other hand, TorchScript provides a convenient, safe way to save a model with its corresponding implementation. The resulting models can be loaded anywhere that PyTorch is installed, even without importing sk2torch.
  3. While certain models like SVMs and linear classifiers are theoretically end-to-end differentiable, scikit-learn provides no mechanism to compute gradients through trained models. PyTorch provides this functionality mostly for free.

See Usage for a high-level example of using the library. See How it works to see which modules are supported.

For fun, here's a vector field produced by differentiating the probability predictions of a two-class SVM (produced by this script):

A vector field quiver plot with two modes

Usage

First, train a model with scikit-learn as usual:

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

x, y = create_some_dataset()
model = Pipeline([
    ("center", StandardScaler(with_std=False)),
    ("classify", SGDClassifier()),
])
model.fit(x, y)

Then call sk2torch.wrap on the model to create a PyTorch equivalent:

import sk2torch
import torch

torch_model = sk2torch.wrap(model)
print(torch_model.predict(torch.tensor([[1., 2., 3.]]).double()))

You can save a model with TorchScript:

import torch.jit

torch.jit.script(torch_model).save("path.pt")

# ... sk2torch need not be installed to load the model.
loaded_model = torch.jit.load("path.pt")

For a full example of training a model and using its PyTorch translation, see examples/svm_vector_field.py.

How it works

sk2torch contains PyTorch re-implementations of supported scikit-learn models. For a supported estimator X, a class TorchX in sk2torch will be able to read the attributes of X and convert them to torch.Tensor or simple Python types. TorchX subclasses torch.nn.Module and has a method for each inference API of X (e.g. predict, decision_function, etc.).

Which modules are supported? The easiest way to get an up-to-date list is via the supported_classes() function, which returns all wrap()able scikit-learn classes:

>>> import sk2torch
>>> sk2torch.supported_classes()
[<class 'sklearn.tree._classes.DecisionTreeClassifier'>, <class 'sklearn.tree._classes.DecisionTreeRegressor'>, <class 'sklearn.dummy.DummyClassifier'>, <class 'sklearn.ensemble._gb.GradientBoostingClassifier'>, <class 'sklearn.preprocessing._label.LabelBinarizer'>, <class 'sklearn.svm._classes.LinearSVC'>, <class 'sklearn.svm._classes.LinearSVR'>, <class 'sklearn.neural_network._multilayer_perceptron.MLPClassifier'>, <class 'sklearn.kernel_approximation.Nystroem'>, <class 'sklearn.pipeline.Pipeline'>, <class 'sklearn.linear_model._stochastic_gradient.SGDClassifier'>, <class 'sklearn.preprocessing._data.StandardScaler'>, <class 'sklearn.svm._classes.SVC'>, <class 'sklearn.svm._classes.NuSVC'>, <class 'sklearn.svm._classes.SVR'>, <class 'sklearn.svm._classes.NuSVR'>, <class 'sklearn.compose._target.TransformedTargetRegressor'>]

Comparison to sklearn-onnx

sklearn-onnx is an open source package for converting trained scikit-learn models into ONNX. Like sk2torch, sklearn-onnx re-implements inference functions for various models, meaning that it can also provide serialization and GPU acceleration for supported modules.

Naturally, neither library will support modules that aren't manually ported. As a result, the two libraries support different subsets of all available models/methods. For example, sk2torch supports the SVC probability prediction methods predict_proba and predict_log_prob, whereas sklearn-onnx does not.

While sklearn-onnx exports models to ONNX, sk2torch exports models to Python objects with familiar method names that can be fine-tuned, backpropagated through, and serialized in a user-friendly way. PyTorch is strictly more general than ONNX, since PyTorch models can be converted to ONNX if desired.

More Repositories

1

gobfuscate

Obfuscate Go binaries and packages
Go
1,323
star
2

JamWiFi

A GUI, easy to use WiFi network jammer for Mac OS X
Objective-C
779
star
3

kahoot-hack

Reverse engineering kahoot.it
Go
470
star
4

muniverse

µniverse: RL environments for HTML5 games
JavaScript
361
star
5

Giraffe

Encode animated GIF files on the iPhone
Objective-C
261
star
6

weakai

AI algorithms implemented in Go
Go
235
star
7

anyrl-py

A reinforcement learning framework
Python
156
star
8

obs-tower2

My solution to the Unity Obstacle Tower Challenge
Python
136
star
9

model3d

Create & render beautiful 3D models
Go
126
star
10

audioset

Fetch and use Google's AudioSet dataset
Go
123
star
11

num-analysis

Learning some Numerical Analysis
Go
95
star
12

cbyge

Reverse engineering Cync (formerly "C by GE") WiFi devices
Go
94
star
13

fbmsgr

Reverse engineering Facebook Messenger
Go
89
star
14

ANImageBitmapRep

A set of classes for easily manipulating images with bitmap data or CoreGraphics
Objective-C
85
star
15

car-data

Scraping and predicting car info
Python
82
star
16

vq-vae-2

A PyTorch implementation of the VQ-VAE-2 paper
Python
74
star
17

Benchmarks

Some language performance comparisons.
Rust
66
star
18

SnapchatHax

Hacking away at Snapchat from iOS!
Objective-C
65
star
19

learn-nerf

Learning about Neural Radiance Fields
Python
63
star
20

ImageReflection

A simple addition to UIImage allowing the reflection of images
Objective-C
55
star
21

cve-2018-4407

Crash macOS and iOS devices with one packet
Go
48
star
22

vq-voice-swap

Voice swapping with VQ-VAE and diffusion models
Python
48
star
23

GifPro

My new and improved Gif encoder for Mac
Objective-C
43
star
24

LibOrange

A simple AOL Instant Messenger implementation for Objective-C
Objective-C
42
star
25

vae-textures

Texture mapping with variational auto-encoders
Python
41
star
26

vq-draw

A discrete sequential VAE
Jupyter Notebook
38
star
27

PathIntersection

A class that can be used to find line intersections of CGPaths
Objective-C
36
star
28

learn-quantum

Learning about quantum computing
Go
36
star
29

anynet

Framework for artificial neural networks
Go
35
star
30

MP4Audio

A partially broken Objective-C API for extracting audio from MP4 files and editing metadata.
Objective-C
35
star
31

ANColorPicker

A custom mac-like color well for iPhone
Objective-C
33
star
32

sgdstore

Augmented RNN memory via live SGD
Go
32
star
33

Mac-Utils

A series of small applications to increase the Mac OS X experience
Objective-C
31
star
34

whichlang

Using ML to recognize programming languages
Go
27
star
35

spherenet

Implementing Deep Hyperspherical Learning
Python
27
star
36

cuda

Go bindings for CUDA, done right.
Go
26
star
37

svm-playground

Play around with SVMs in the browser
JavaScript
25
star
38

hopfield

Hopfield networks in TensorFlow
Python
23
star
39

char-rnn

Generate text with recurrent neural nets
Go
22
star
40

ddim

Denoising Diffusion Implicit Models
Jupyter Notebook
22
star
41

demoverse

Record demonstrations for µniverse
Go
21
star
42

alux

A lightweight C++ kernel designed to run a JavaScript or Dart VM
C++
21
star
43

rwa

RWA recurrent neural networks
Go
20
star
44

camera-hijack

A chrome extension to mess with the webcam
JavaScript
20
star
45

treeagent

Decision tree ensembles as RL policies
Go
19
star
46

SoundArt

Draw sound waves and hear them, iOS only
Objective-C
19
star
47

learnos

Reminding myself everything I knew about OSDev (and more)
C
19
star
48

ANExpressionParser

Terrible, old, Objective-C expression parser.
Objective-C
19
star
49

ImageTransfer

Bluetooth image transferring app for the iPhone
18
star
50

SocketKit

A C socket wrapper (with SSL) written in Objective-C
17
star
51

ScreenPear

A remote displays application for OS X, still in the works.
Objective-C
16
star
52

heatgrid

Emulate heat conduction in a solid
JavaScript
16
star
53

uno-ai

AI for the game Uno
Python
16
star
54

FreeRez

A GUI Mac OS X application for setting the native resolution on a Retina MBP
Objective-C
15
star
55

voronoi-interp

Create cool animations by gradually adding pixels to an interpolated image.
Go
15
star
56

sentigraph

Graph sentiment throughout a piece of text
Go
15
star
57

bezier-mnist

MNIST, but with Bezier curves instead of pixels
Python
15
star
58

ANDownload

A small download manager with pause&resume support for iphone and mac
Objective-C
15
star
59

anyrl

[Deprecated] APIs for Reinforcement Learning
Go
14
star
60

VideoExporter

A basic Objective-C wrapper for AV Foundation's AVAssetWriter
Objective-C
14
star
61

SpinWheel

A UIView that the user can spin with touch events
Objective-C
14
star
62

godsalg

Trying to find God's algorithm on a Rubik's cube
Go
14
star
63

statushub

A simple log aggregation tool
JavaScript
13
star
64

cnn-toys

Playing around with CNNs
Python
13
star
65

Wolfram-API

An Objective-C implementation of the Wolfram API 2.0
Objective-C
13
star
66

dist-sys

Teaching myself about distributed systems
Go
12
star
67

essentials

Things I wish were Go built-ins
Go
12
star
68

chatbot

Instant messaging with a neural network
Go
12
star
69

neuralspell

Spell and pronounce words with a neural network
Go
12
star
70

polish

Denoising networks for ray traced images
Go
12
star
71

text2emoji

Neural network that produces emojis from text
Python
12
star
72

ffmpego

A Go package for encoding and decoding video and audio files.
Go
12
star
73

torch-bandpass

An implementation of the Prism layer (https://arxiv.org/abs/2011.04823)
Jupyter Notebook
11
star
74

packet-proxy

A proxy for reverse engineering a communication protocol
Go
11
star
75

setres

A CLI for setting the resolution on Mac OS X on the retina MBPs
Objective-C
11
star
76

markovchain

Markov chains for text and anything else
Go
11
star
77

mnistdemo

Test MNIST classifiers from your browser
Go
11
star
78

cubezapp

An amazing cube timer
JavaScript
11
star
79

Expressions

An object-oriented mathematical expression parser for Objective-C
Objective-C
10
star
80

uber-ga

Implementation of Uber's genetic algorithm for RL
Python
10
star
81

learning-tf

Learning TensorFlow
Python
10
star
82

pca-compress

Compressing neural network initializations with PCA
Python
10
star
83

tweetembed

Build word embeddings for Tweets
Go
10
star
84

LassoCapture-old

Extended screenshot options for Mac OS X
Objective-C
10
star
85

SlideToUnlock

A slide-to-unlock interface for iOS
Objective-C
10
star
86

anarch

API for architecture-specific abstractions in OS kernels
C++
10
star
87

tf-env

RL environments written in pure TensorFlow
Python
10
star
88

agg

Command-line tool for numerical aggregates
Go
9
star
89

payrange

Tracking laundry machines
Rust
9
star
90

wav

A WAV encoding/decoding library for Go
Go
9
star
91

voronoi-glass

Create a cool glass-like pattern using Voronoi cells
Go
9
star
92

ANHTML

A lightweight HTML parser for Objective-C (ARC only)
Objective-C
9
star
93

captcha-crack

Cracking a simple captcha system
Python
9
star
94

ErrorScatter

A small prank application for Mac OS X
Objective-C
9
star
95

anyvec

Precision-agnostic vector abstractions
Go
9
star
96

solid-trace

Visualize 3D solids implemented as JavaScript boolean functions
JavaScript
9
star
97

smallpng

Lossy compression for PNG files
Go
9
star
98

speechrecog

Tools for speech recognition
Go
9
star
99

wavenet

A convenient TensorFlow package for the WaveNet architecture
Python
9
star
100

gospeech

An attempt at speech synthesis in Go
Go
9
star