skorch
raw JSON → 1.3.1 verified Fri May 01 auth: no python
A scikit-learn compatible neural network library that wraps PyTorch models, enabling easy integration with scikit-learn's API, including cross-validation, GridSearchCV, and pipelines. Current version is 1.3.1, released roughly every few months.
pip install skorch Common errors
error ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() ↓
cause Passing a numpy array or PyTorch tensor where a scalar boolean is expected, often due to incorrect `train_split` or callbacks.
fix
Set
train_split=False explicitly instead of train_split=None. error TypeError: X should be a 2D array-like, got 3D array ↓
cause Passing 3D input (e.g., images with channel dimension not flattened) but skorch expects 2D inputs by default.
fix
Flatten your input data or modify the module to accept 3D input and set
module__input_shape accordingly. error AttributeError: 'NeuralNetClassifier' object has no attribute 'predict_proba' ↓
cause Using an older version of skorch where `predict_proba` was not exposed; or missing `forward` with softmax.
fix
Ensure skorch >= 0.9.0 (for sklearn compatibility) and define
softmax in your module's forward. Warnings
breaking Deprecation of `train_split=None` for disabling validation: In skorch 1.0, passing `train_split=None` to disable validation was deprecated. For explicit no validation, use `train_split=False`. ↓
fix Replace `train_split=None` with `train_split=False`.
gotcha Input data types: skorch expects `X` as float32 and `y` as int64 for classification. Using wrong dtypes may cause silent errors or poor performance. ↓
fix Cast X to np.float32 and y to np.int64 (or torch tensors with corresponding dtype).
gotcha Device specification: when using `device='cuda'`, the entire model and data must be on the same device. Forgetting to move data to the GPU can cause runtime errors. ↓
fix Ensure your input tensors are on the correct device, or use `device='cpu'`.
Imports
- NeuralNetClassifier
from skorch import NeuralNetClassifier - NeuralNetRegressor
from skorch import NeuralNetRegressor - NeuralNet wrong
from skorch.net import NeuralNetcorrectfrom skorch import NeuralNet
Quickstart
import torch
import torch.nn as nn
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from skorch import NeuralNetClassifier
class MyModule(nn.Module):
def __init__(self, num_units=10):
super().__init__()
self.dense0 = nn.Linear(20, num_units)
self.nonlin = nn.ReLU()
self.dropout = nn.Dropout(0.5)
self.dense1 = 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.softmax(self.dense1(X))
return X
X, y = make_classification(1000, 20, n_informative=10, random_state=0)
X = X.astype(np.float32)
y = y.astype(np.int64)
net = NeuralNetClassifier(
MyModule,
max_epochs=10,
lr=0.1,
device='cpu',
iterator_train__shuffle=True,
)
n_scores = cross_val_score(net, X, y, cv=3, scoring='accuracy')
print(f"Cross-validation accuracy: {n_scores.mean():.3f} ± {n_scores.std():.3f}")