Closed-form linear regression models (OLS, Ridge, Ridge variants) using PyTorch.
Project description
torch_linear_regression
A very simple library containing closed-form linear regression models using PyTorch. Accepts both NumPy arrays and PyTorch tensors, follows the scikit-learn estimator interface (fit/predict/score), and can run on GPU.
Models
- OLS -- Ordinary Least Squares:
(X'X)^-1 X'Y - Ridge -- Ridge regression with a fixed scalar penalty:
(X'X + lambda*I)^-1 X'Y - ReducedRankRidgeRegression -- Ridge regression followed by SVD truncation of the weight matrix
- RidgeMML -- Ridge regression with automatic per-column lambda estimation (Karabatsos 2017).
The closed-form approach results in fast and accurate results under most
conditions. However, when n_features is large and/or very underdetermined
(n_samples << n_features), the closed-form solution will start to
diverge from other solutions. Also, if the input X matrix is singular (has
redundant columns), an error will be thrown. If you encounter these issues,
consider using SVD / PCA to reduce the redundancy in your input matrix.
OLS, Ridge, and ReducedRankRidgeRegression include a model.prefit() method
that precomputes the inverse matrix. This is useful when fitting the same X
to multiple targets.
Installation
Install stable version:
pip install torch_linear_regression
Install development version:
pip install git+https://github.com/RichieHakim/torch_linear_regression.git
Usage
OLS / Ridge
See the demo notebook for more examples.
import torch_linear_regression as tlr
import numpy as np
from sklearn.datasets import make_regression
X, Y = make_regression(n_samples=100, n_features=10, noise=5, random_state=42)
model = tlr.OLS()
model.fit(X=X, y=Y)
Y_pred = model.predict(X)
print(f"R^2: {model.score(X=X, y=Y):.4f}")
RidgeMML
Ridge regression where each column of Y gets its own regularization parameter, estimated automatically via marginal maximum likelihood.
X columns are z-scored internally (required by the algorithm for lambda
comparability across features). Coefficients are un-z-scored before
storage, so predict(X_raw) works on raw data.
import torch_linear_regression as tlr
import numpy as np
## Multi-target regression
X = np.random.randn(500, 20)
beta_true = np.random.randn(20, 5)
Y = X @ beta_true + 0.5 * np.random.randn(500, 5)
model = tlr.RidgeMML()
model.fit(X, Y)
print(f"R^2: {model.score(X, Y):.4f}")
print(f"Lambdas: {model.lambdas_}") ## per-column regularization
print(f"Converged: {~model.convergence_failures_.any()}")
## Pre-supplied lambdas (skip MML optimization)
model_fixed = tlr.RidgeMML(lambdas=np.ones(5) * 10.0)
model_fixed.fit(X, Y)
## GPU acceleration
model_gpu = tlr.RidgeMML(device="cuda")
model_gpu.fit(X, Y)
## Control memory for large p_y
model_batched = tlr.RidgeMML(batch_size_solve=50)
model_batched.fit(X, Y)
Key differences from Ridge:
Ridge |
RidgeMML |
|
|---|---|---|
| Lambda | Single scalar, user-specified | Per-column of Y, estimated via MML |
| X standardization | None | Intrinsic z-scoring (ddof=1) |
| Intercept | Optional | Always (derived from column means) |
| Dependencies | numpy, torch | numpy, torch |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torch_linear_regression-0.2.0.tar.gz.
File metadata
- Download URL: torch_linear_regression-0.2.0.tar.gz
- Upload date:
- Size: 21.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1682251f368b02dd352a62c76089194faa114e212a805ef068678e0b26a1af75
|
|
| MD5 |
506982c35b93f74eb6bea2ddb599a4e3
|
|
| BLAKE2b-256 |
a74e0ac2ff0d4e484ca10bb86ce6f416f4d68c0100f67772599d13b0573c1353
|
Provenance
The following attestation bundles were made for torch_linear_regression-0.2.0.tar.gz:
Publisher:
pypi_release.yml on RichieHakim/torch_linear_regression
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torch_linear_regression-0.2.0.tar.gz -
Subject digest:
1682251f368b02dd352a62c76089194faa114e212a805ef068678e0b26a1af75 - Sigstore transparency entry: 1632200791
- Sigstore integration time:
-
Permalink:
RichieHakim/torch_linear_regression@1596b307862559883caa603489bacff1a4dd6087 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/RichieHakim
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi_release.yml@1596b307862559883caa603489bacff1a4dd6087 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file torch_linear_regression-0.2.0-py3-none-any.whl.
File metadata
- Download URL: torch_linear_regression-0.2.0-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d857e0a7b608c1e48ab9b5dba61caed065c2b61d72e25a9e7180acac9b7aa8d7
|
|
| MD5 |
785a2c71743a867d2c089f3d09c0742b
|
|
| BLAKE2b-256 |
dbf81061ae24c5951590201cb14fd7116685c93b041403836cdbfae9f16c78df
|
Provenance
The following attestation bundles were made for torch_linear_regression-0.2.0-py3-none-any.whl:
Publisher:
pypi_release.yml on RichieHakim/torch_linear_regression
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torch_linear_regression-0.2.0-py3-none-any.whl -
Subject digest:
d857e0a7b608c1e48ab9b5dba61caed065c2b61d72e25a9e7180acac9b7aa8d7 - Sigstore transparency entry: 1632200809
- Sigstore integration time:
-
Permalink:
RichieHakim/torch_linear_regression@1596b307862559883caa603489bacff1a4dd6087 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/RichieHakim
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi_release.yml@1596b307862559883caa603489bacff1a4dd6087 -
Trigger Event:
workflow_dispatch
-
Statement type: