{"id":16518475,"url":"https://github.com/valentingol/torch_pca","last_synced_at":"2025-10-28T07:30:19.796Z","repository":{"id":244415259,"uuid":"814674071","full_name":"valentingol/torch_pca","owner":"valentingol","description":"Principal Component Anlaysis (PCA) in PyTorch.","archived":false,"fork":false,"pushed_at":"2024-06-24T09:03:48.000Z","size":140,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-09-27T08:07:22.067Z","etag":null,"topics":["pca","pca-analysis","principal-component-analysis","python","python3","pytorch","torch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/valentingol.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-13T13:22:05.000Z","updated_at":"2024-09-23T13:51:34.000Z","dependencies_parsed_at":"2024-06-14T15:49:09.738Z","dependency_job_id":"41cadaf2-2429-4aee-a4f3-ef24009b1466","html_url":"https://github.com/valentingol/torch_pca","commit_stats":null,"previous_names":["valentingol/torch_pca"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valentingol%2Ftorch_pca","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valentingol%2Ftorch_pca/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valentingol%2Ftorch_pca/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valentingol%2Ftorch_pca/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/valentingol","download_url":"https://codeload.github.com/valentingol/torch_pca/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219860032,"owners_count":16556029,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["pca","pca-analysis","principal-component-analysis","python","python3","pytorch","torch"],"created_at":"2024-10-11T16:36:13.976Z","updated_at":"2025-10-28T07:30:19.392Z","avatar_url":"https://github.com/valentingol.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pytorch Principal Component Analysis (PCA)\n\nPrincipal Component Anlaysis (PCA) in PyTorch. The intention is to provide a\nsimple and easy to use implementation of PCA in PyTorch, the most similar to\nthe `sklearn`'s PCA as possible (in terms of API and, of course, output).\nPlus, this implementation is **fully differentiable and faster** (thanks to GPU parallelization)!\n\n[![Release](https://img.shields.io/github/v/tag/valentingol/torch_pca?label=Pypi\u0026logo=pypi\u0026logoColor=yellow)](https://pypi.org/project/torch_pca/)\n![PythonVersion](https://img.shields.io/badge/python-3.8%20%7E%203.11-informational)\n![PytorchVersion](https://img.shields.io/badge/pytorch-1.8%20%7E%201.13%20%7C%202.0+-informational)\n\n[![GitHub User followers](https://img.shields.io/github/followers/valentingol?label=User%20followers\u0026style=social)](https://github.com/valentingol)\n[![GitHub User's User stars](https://img.shields.io/github/stars/valentingol?label=User%20Stars\u0026style=social)](https://github.com/valentingol)\n\n[![Ruff_logo](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v1.json)](https://github.com/charliermarsh/ruff)\n[![Black_logo](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\n[![Ruff](https://github.com/valentingol/torch_pca/actions/workflows/ruff.yaml/badge.svg)](https://github.com/valentingol/Dinosor/actions/workflows/ruff.yaml)\n[![Flake8](https://github.com/valentingol/torch_pca/actions/workflows/flake.yaml/badge.svg)](https://github.com/valentingol/Dinosor/actions/workflows/flake.yaml)\n[![MyPy](https://github.com/valentingol/torch_pca/actions/workflows/mypy.yaml/badge.svg)](https://github.com/valentingol/Dinosor/actions/workflows/mypy.yaml)\n[![PyLint](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/valentingol/8fb4f3f78584e085dd7b0cca7e046d1f/raw/torch_pca_pylint.json)](https://github.com/valentingol/torch_pca/actions/workflows/pylint.yaml)\n\n[![Tests](https://github.com/valentingol/torch_pca/actions/workflows/tests.yaml/badge.svg)](https://github.com/valentingol/torch_pca/actions/workflows/tests.yaml)\n[![Coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/valentingol/c5a6b5731db93da673f8e258b2669080/raw/torch_pca_tests.json)](https://github.com/valentingol/torch_pca/actions/workflows/tests.yaml)\n[![Documentation Status](https://readthedocs.org/projects/torch-pca/badge/?version=latest)](https://torch-pca.readthedocs.io/en/latest/?badge=latest)\n\n## Links\n\nGithub repository: https://github.com/valentingol/torch_pca\n\nPypi project: https://pypi.org/project/torch_pca/\n\nDocumentation: https://torch-pca.readthedocs.io/en/latest/\n\n## Installation\n\nSimply install it with pip:\n\n```bash\npip install torch-pca\n```\n\n## How to use\n\nExactly like `sklearn.decomposition.PCA` but it uses PyTorch tensors as input and output!\n\n```python\nfrom torch_pca import PCA\n\n# Create like sklearn.decomposition.PCA, e.g.:\npca_model = PCA(n_components=None, svd_solver='full')\n\n# Use like sklearn.decomposition.PCA, e.g.:\n\u003e\u003e\u003e new_train_data = pca_model.fit_transform(train_data)\n\u003e\u003e\u003e new_test_data = pca_model.transform(test_data)\n\u003e\u003e\u003e print(pca.explained_variance_ratio_)\n[0.756, 0.142, 0.062, ...]\n```\n\nMore details and features in the [API documentation](https://torch-pca.readthedocs.io/en/latest/api.html#torch_pca.pca_main.PCA).\n\n## Gradient backward pass\n\nUse the pytorch framework allows the automatic differentiation of the PCA!\n\nThe PCA transform method is always differentiable so it is always possible to\ncompute gradient like that:\n\n```python\npca = PCA()\nfor ep in range(n_epochs):\n    optimizer.zero_grad()\n    out = neural_net(inputs)\n    with torch.no_grad():\n        pca.fit(out)\n    out = pca.transform(out)\n    loss = loss_fn(out, targets)\n    loss.backward()\n```\n\nIf you want to compute the gradient over the full PCA model (including the\nfitted `pca.n_components`), you can do it by using the \"full\" SVD solver\nand removing the part of the `fit` method that enforce the deterministic\noutput by passing `determinist=False` in `fit` or `fit_transform` method.\nThis part sort the components using the singular values and change their sign\naccordingly so it is not differentiable by nature but may be not necessary if\nyou don't care about the determinism of the output:\n\n```python\npca = PCA(svd_solver=\"full\")\nfor ep in range(n_epochs):\n    optimizer.zero_grad()\n    out = neural_net(inputs)\n    out = pca.fit_transform(out, determinist=False)\n    loss = loss_fn(out, targets)\n    loss.backward()\n```\n\n## Comparison of execution time with sklearn's PCA\n\nAs we can see below the PyTorch PCA is faster than sklearn's PCA, in all the\nconfigs tested with the parameter by default (for each PCA model):\n\n![include](docs/_static/comparison.png)\n\n## Implemented features\n\n- [x] `fit`, `transform`, `fit_transform` methods.\n- [x] All attributes from sklean's PCA are available: `explained_variance_(ratio_)`,\n      `singular_values_`, `components_`, `mean_`, `noise_variance_`, ...\n- [x] Full SVD solver\n- [x] SVD by covariance matrix solver\n- [x] Randomized SVD solver\n- [x] (absent from sklearn) Decide how to center the input data in `transform` method\n  (default is like sklearn's PCA)\n- [x] Find number of components with explained variance proportion\n- [x] Automatically find number of components with MLE\n- [x] `inverse_transform` method\n- [x] Whitening option\n- [x] `get_covariance` method\n- [x] `get_precision` method and `score`/`score_samples` methods\n\n## To be implemented\n\n- [ ] Support sparse matrices with ARPACK solver\n\n## Contributing\n\nFeel free to contribute to this project! Just fork it and make an issue or a pull request.\n\nSee the [CONTRIBUTING.md](CONTRIBUTING.md) file for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalentingol%2Ftorch_pca","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvalentingol%2Ftorch_pca","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalentingol%2Ftorch_pca/lists"}