项目作者: ulf1

项目描述 :
Generate different types of sparsity pattern for sparse matrices.
高级语言: Python
项目地址: git://github.com/ulf1/sparsity-pattern.git
创建时间: 2020-03-08T16:43:05Z
项目社区:https://github.com/ulf1/sparsity-pattern

开源协议:Apache License 2.0

下载


PyPI version
PyPi downloads
DOI
sparsity-pattern
Total alerts
Language grade: Python

sparsity-pattern

Generate different types of sparsity pattern for sparse matrices.

Installation

The sparsity-pattern git repo is available as PyPi package

  1. pip install sparsity-pattern
  2. pip install git+ssh://git@github.com/ulf1/sparsity-pattern.git

Usage

The block-diagonal pattern for tensorflow

  1. import sparsity_pattern
  2. import tensorflow as tf
  3. n_rows, n_cols = 10, 12
  4. idx = sparsity_pattern.get('block', min(n_rows, n_cols), block_sizes=[3, 1, 2])
  5. mat = tf.sparse.SparseTensor(
  6. dense_shape=(n_rows, n_cols),
  7. indices=tf.convert_to_tensor(idx, dtype=tf.int64),
  8. values=range(1, len(idx)+1))
  9. print(tf.sparse.to_dense(mat))

The circle pattern for pytorch

  1. import sparsity_pattern
  2. import torch
  3. n_rows, n_cols = 5, 7
  4. idx = sparsity_pattern.get('circle', min(n_rows, n_cols), offsets=[1, 2])
  5. mat = torch.sparse_coo_tensor(
  6. indices=torch.tensor(idx).transpose(0, 1),
  7. values=range(1, len(idx)+1),
  8. size=[n_rows, n_cols])
  9. print(mat.to_dense())

The triu pattern for scipy

  1. import sparsity_pattern
  2. import scipy.sparse
  3. import numpy as np
  4. n, k = 4, -1
  5. idx = sparsity_pattern.get('triu', n, k)
  6. idx_rows, idx_cols = np.array(idx)[:, 0], np.array(idx)[:, 1]
  7. mat = scipy.sparse.lil_matrix((n, n), dtype=np.int64)
  8. mat[idx_rows, idx_cols] = range(1, len(idx)+1)
  9. print(mat.todense())

Check the examples folder for more notebooks.

Appendix

Install a virtual environment

  1. python3 -m venv .venv
  2. source .venv/bin/activate
  3. pip3 install --upgrade pip
  4. pip3 install -r requirements-dev.txt
  5. pip3 install -r requirements-demo.txt

(If your git repo is stored in a folder with whitespaces, then don’t use the subfolder .venv. Use an absolute path without whitespaces.)

Python commands

  • Jupyter for the examples: jupyter lab
  • Check syntax: flake8 --ignore=F401 --exclude=$(grep -v '^#' .gitignore | xargs | sed -e 's/ /,/g')
  • Run Unit Tests: pytest

Publish

  1. pandoc README.md --from markdown --to rst -s -o README.rst
  2. python setup.py sdist
  3. twine upload -r pypi dist/*

Clean up

  1. find . -type f -name "*.pyc" | xargs rm
  2. find . -type d -name "__pycache__" | xargs rm -r
  3. rm -r .pytest_cache
  4. rm -r .venv

Support

Please open an issue for support.

License and citation

This software is licensed under Apache License 2.0 and archived on Zenodo.
If you would like to cite the software, please use this DOI: 10.5281/zenodo.4357290.

Contributing

Please contribute using Github Flow. Create a branch, add commits, and open a pull request.