{"id":24595,"library":"skorch","title":"skorch","description":"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.","status":"active","version":"1.3.1","language":"python","source_language":"en","source_url":"https://github.com/skorch-dev/skorch","tags":["pytorch","neural-network","scikit-learn","deep-learning"],"install":[{"cmd":"pip install skorch","lang":"bash","label":"Install from PyPI"}],"dependencies":[{"reason":"Core dependency: skorch wraps PyTorch modules","package":"torch","optional":false},{"reason":"Provides compatibility with sklearn API, pipelines, and utilities like GridSearchCV","package":"scikit-learn","optional":false}],"imports":[{"note":"Standard import path","symbol":"NeuralNetClassifier","correct":"from skorch import NeuralNetClassifier"},{"note":"Standard import path","symbol":"NeuralNetRegressor","correct":"from skorch import NeuralNetRegressor"},{"note":"Using internal modules is fragile; import from top-level skorch","wrong":"from skorch.net import NeuralNet","symbol":"NeuralNet","correct":"from skorch import NeuralNet"}],"quickstart":{"code":"import torch\nimport torch.nn as nn\nimport numpy as np\nfrom sklearn.datasets import make_classification\nfrom sklearn.model_selection import cross_val_score\nfrom skorch import NeuralNetClassifier\n\nclass MyModule(nn.Module):\n    def __init__(self, num_units=10):\n        super().__init__()\n        self.dense0 = nn.Linear(20, num_units)\n        self.nonlin = nn.ReLU()\n        self.dropout = nn.Dropout(0.5)\n        self.dense1 = nn.Linear(num_units, 2)\n        self.softmax = nn.Softmax(dim=-1)\n\n    def forward(self, X, **kwargs):\n        X = self.nonlin(self.dense0(X))\n        X = self.dropout(X)\n        X = self.softmax(self.dense1(X))\n        return X\n\nX, y = make_classification(1000, 20, n_informative=10, random_state=0)\nX = X.astype(np.float32)\ny = y.astype(np.int64)\n\nnet = NeuralNetClassifier(\n    MyModule,\n    max_epochs=10,\n    lr=0.1,\n    device='cpu',\n    iterator_train__shuffle=True,\n)\nn_scores = cross_val_score(net, X, y, cv=3, scoring='accuracy')\nprint(f\"Cross-validation accuracy: {n_scores.mean():.3f} ± {n_scores.std():.3f}\")","lang":"python","description":"Quickstart: define a PyTorch module, wrap it with NeuralNetClassifier, and use cross_val_score from scikit-learn."},"warnings":[{"fix":"Replace `train_split=None` with `train_split=False`.","message":"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`.","severity":"breaking","affected_versions":">= 1.0"},{"fix":"Cast X to np.float32 and y to np.int64 (or torch tensors with corresponding dtype).","message":"Input data types: skorch expects `X` as float32 and `y` as int64 for classification. Using wrong dtypes may cause silent errors or poor performance.","severity":"gotcha","affected_versions":"all"},{"fix":"Ensure your input tensors are on the correct device, or use `device='cpu'`.","message":"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.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Set `train_split=False` explicitly instead of `train_split=None`.","cause":"Passing a numpy array or PyTorch tensor where a scalar boolean is expected, often due to incorrect `train_split` or callbacks.","error":"ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"},{"fix":"Flatten your input data or modify the module to accept 3D input and set `module__input_shape` accordingly.","cause":"Passing 3D input (e.g., images with channel dimension not flattened) but skorch expects 2D inputs by default.","error":"TypeError: X should be a 2D array-like, got 3D array"},{"fix":"Ensure skorch >= 0.9.0 (for sklearn compatibility) and define `softmax` in your module's forward.","cause":"Using an older version of skorch where `predict_proba` was not exposed; or missing `forward` with softmax.","error":"AttributeError: 'NeuralNetClassifier' object has no attribute 'predict_proba'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}