torch-geometric-median
A simplified version of the geom-median Python library, updated to be higher performance on Pytorch and with full type-hinting. Thanks to @themachinefan!
Installation
pip install torch-geometric-median
Usage
This library exports a single function, geometric_median, which takes a tensor of shape (N, D) where N is the number of samples, and D is the size of each sample, and returns the geometric median of the points in the tensor .
from torch_geometric_median import geometric_median
# Create a tensor of points
points = torch.tensor([
[0.0, 0.0],
[1.0, 1.0],
[2.0, 2.0],
[3.0, 3.0],
[4.0, 4.0],
])
# Compute the geometric median
median = geometric_median(points).median
Backprop
Like the original geom-median library, this library supports backpropagation through the geometric median computation.
median = geometric_median(points).median
torch.linalg.norm(out.median).backward()
# The gradient of the median with respect to the input points is now in `points.grad`
Extra options
The geometric_median function also supports a few extra options:
maxiter: The maximum number of iterations to run the optimization for. Default is 100.ftol: If objective value does not improve by at least thisftolfraction, terminate the algorithm. Default 1e-20.weights: A tensor of shape(N,)containing the weights for each point, whereNis the number of samples. Default isNone, which means all points are weighted equally.show_progress: IfTrue, show a progress bar for the optimization. Default isFalse.log_objective_values: IfTrue, log the objective value at each iteration under the keyobjective_values_log. Default isFalse.
median = geometric_median(
points,
maxiter=1000,
ftol=1e-10,
weights=torch.tensor([1.0, 2.0, 1.0, 1.0, 1.0]),
show_progress=True,
log_objective_values=True
).median
Why does this library exist?
It appears that the original geom-median library is no longer maintained, and as pointed out by @themachinefan, the original library is not very performant on Pytorch. This library is a repackaging of @themachinefan's improvements to the original geom-median library, simplying the code to just support pytorch, improving torch performance, and adding full type-hinting.
Acknowledgements
This library is a repackaging of the work done by the original geom-median library, and @themachinefan in their PR, and as such, all credit goes to these incredible authors. If you use this library, you should cite the original geom-median paper.
License
This library is licensed under a GPL license, as per the original geom-median library.
Contributing
Contributions are welcome! Please open an issue or a PR if you have any suggestions or improvements. This library uses PDM for dependency management, Ruff for linting, Pyright for type-checking, and Pytest for tests.
Metadata
Release files for torch-geometric-median 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torch_geometric_median-0.1.1.tar.gz | 16.7 kB | Without provenance | Details for torch_geometric_median-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torch_geometric_median-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for torch_geometric_median-0.1.1-py3-none-any.whl |
Total release size: 33.5 kB
Release files / torch_geometric_median-0.1.1.tar.gz
| Download URL | torch_geometric_median-0.1.1.tar.gz |
|---|---|
| Size | 16.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
00316a0237471d0628516db407a34fc99e04504b62575fd1c03670e839aba2c0
|
|
BLAKE2b-256 checksum How to use checksums |
781fa593a3f2f04ed90ca48b6bf4d614d52cb502cd50db6c4925a5e579ab2d16
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.2
|
Release files / torch_geometric_median-0.1.1-py3-none-any.whl
| Download URL | torch_geometric_median-0.1.1-py3-none-any.whl |
|---|---|
| Size | 16.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
20c26ef7a40d3cc3f6f29fa4cb9f78eebf56e8c7e702f4fb8e2d8b2910b3c386
|
|
BLAKE2b-256 checksum How to use checksums |
06f3b70c4295c91e1ef306a6d9b30a028072cf7e0f6bab29e4339a6cd2299fe2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.2
|
Security for torch-geometric-median 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.