Tensor Networks for Machine Learning
Project description
Tensor Networks for Machine Learning
tn4ml is a Python library that handles tensor networks for machine learning applications.
It is built on top of Quimb, for Tensor Network objects, and JAX, for optimization pipeline.
For now, the library supports 1D Tensor Network structures:
- Matrix Product State
- Matrix Product Operator
- Spaced Matrix Product Operator
It supports different embedding functions, initialization techniques, objective functions and optimization strategies.
Installation
First create a virtualenv using pyenv or conda. Then install the package and its dependencies.
With pip (tag v1.0.5):
pip install tn4ml
or directly from github:
pip install -U git+https://github.com/bsc-quantic/tn4ml.git
If you want to test and edit the code, you can clone the local version of the package and install it.
git clone https://github.com/bsc-quantic/tn4ml.git
pip install -e tn4ml/
If you want to install dependices for docs, test and examples:
pip install "tn4ml[docs]"
pip install "tn4ml[test]"
pip install "tn4ml[examples]"
Accelerated runtime
(Optional) To improve runtime precision set these flags:
jax.config.update("jax_enable_x64", True)
jax.config.update('jax_default_matmul_precision', 'highest')
Running on GPU
Before everything install JAX version that supports CUDA and its suitable for runs on GPU.
Checkout how to install here: jax[cuda]
Next, at the beginning of your script set:
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0" # Use GPU 0 - or set any GPU ID
import jax
jax.config.update("jax_platform_name", 'gpu')
Then when training Model set:
device = 'gpu'
model.configure(device=device)
Documentation
Visit tn4ml.readthedocs.io
Example notebooks
TN for Classification
TN for Anomaly Detection
TN for Anomaly Detection with DMRG-like method
Examples from the paper
Breast Cancer Classification
Unsupervised learning with MNIST
Citation
If you use tn4ml in your work, please cite the following paper: arXiv:2502.13090
@article{puljak2025tn4mltensornetworktraining,
title={tn4ml: Tensor Network Training and Customization for Machine Learning},
author={Ema Puljak and Sergio Sanchez-Ramirez and Sergi Masot-Llima and Jofre Vallès-Muns and Artur Garcia-Saez and Maurizio Pierini},
year={2025},
eprint={2502.13090},
archivePrefix={arXiv},
primaryClass={cs.LG},
url={https://arxiv.org/abs/2502.13090},
}
License
MIT license - check it out here
Project details
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 tn4ml-1.0.5.tar.gz.
File metadata
- Download URL: tn4ml-1.0.5.tar.gz
- Upload date:
- Size: 48.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9206933e8754b6dae8afcdc1cb4818ab82ba35718cd0f975fe8f799d6598666
|
|
| MD5 |
259042c3a0e6f8f58a3d0bb4f517ffb2
|
|
| BLAKE2b-256 |
08e18904c8bc31ff983338476305b030a0ca11249d6e4c4155a5e75078eb39c2
|
File details
Details for the file tn4ml-1.0.5-py3-none-any.whl.
File metadata
- Download URL: tn4ml-1.0.5-py3-none-any.whl
- Upload date:
- Size: 55.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77eb7aa85f51aeb53a85305de26fa23db8cd281c8622272ea043798bf21387ef
|
|
| MD5 |
f91279de18d3b6fa8a41c23c07af430b
|
|
| BLAKE2b-256 |
46cc78de622337fd3196465336b34681a21cf23b0399fa38cd8364ff208428e0
|