Skip to main content

Multiply row vector with sparse matrix in tensorflow

Project description

Binder

keras-dense-sparse-matmul

This package only contains one utility function dense_sparse_matmul to multiply a (dense) row vector with sparse matrix in tensorflow.

Installation

The keras-dense-sparse-matmul git repo is available as PyPi package

pip install keras-dense-sparse-matmul
pip install git+ssh://git@github.com/ulf1/keras-dense-sparse-matmul.git

Usage

import tensorflow as tf
from keras_dense_sparse_matmul import dense_sparse_matmul

# 1x3 row vector
h = tf.constant([1., 2., 3.])

# 3x4 sparse matrix
W = tf.sparse.SparseTensor(
    indices=([0, 1], [1, 1], [1, 2], [2, 0], [2, 2], [0, 3], [2, 3]),
    values=[1., 2., 3., 4., 5., 6., 7.],
    dense_shape=(3, 4))
W = tf.sparse.reorder(W)

# result is a 1x4 row vector
net = dense_sparse_matmul(h, W)

Check the example notebook.

Appendix

Install in a virtual environment

If you want to check the function dense_sparse_matmul first, you can use Binder or install a virtual environment on your local computer

python3.6 -m venv .venv
source .venv/bin/activate
pip3 install --upgrade pip
pip3 install -r requirements.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

  • Activate the virtual env: source .venv/bin/activate
  • Jupyter for the examples: jupyter lab
  • Check syntax: flake8 --ignore=F401 --exclude=$(grep -v '^#' .gitignore | xargs | sed -e 's/ /,/g')
  • Run Unit Tests: pytest
  • Upload to PyPi with twine: python setup.py sdist && twine upload -r pypi dist/*

Clean up commands

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

Support

Please open an issue for support.

Contributing

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

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

keras-dense-sparse-matmul-0.1.0.tar.gz (3.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page