Skip to main content

A sparse tensor framework for Pytorch.

Project description

PyTorch Sparse Tensors

A small package that implements the basic sparse tensor of any dimension.

Key features

  • Torch-like API for sparse tensor of n-dimension
  • Support basic element-wise operations
  • Support reduction (mean and sum) over 1 or multiple dimensions
  • Support reshaping and broadcasting
  • Support concatenation over 1 or multiple dimensions

Installation

pip install torch-sparse-tensor

Demo

Import package

import torch
from sparse import SparseTensor

Build sparse tensors

a = SparseTensor(
    torch.tensor([[0, 3, 1, 1, 2, 2, 3], [0, 0, 1, 2, 1, 2, 3]], dtype=torch.long),
    torch.tensor([[1], [5], [1], [1], [1], [1], [1]], dtype=torch.float32),
    shape=(4, 4),
)
b = SparseTensor(
    torch.tensor([[0, 1, 1, 2, 3], [0, 1, 2, 2, 3]], dtype=torch.long),
    torch.tensor([[1], [2], [1], [1], [1]], dtype=torch.float32),
    shape=(4, 4),
)

To cuda device

a = a.to("cuda")
b = b.to("cuda")

Conversion to dense representation

print(a.to_dense())
print(b.to_dense())

Basic element-wise operations

c=a+b
print(c.to_dense())

d=a*b
print(d.to_dense())

Reduction operations

print(a.sum(0).to_dense())
print(a.mean(0).to_dense())

Indexing and broadcasting

print((a[:, None, :] + a[:, :, None]).sum(0).to_dense())

Concatenation over several dimensions

print(SparseTensor.cat((a, b), dim=1).to_dense())
print(SparseTensor.cat((a, b), dim=(0,1)).to_dense())

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

torch_sparse_tensor-0.2.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

torch_sparse_tensor-0.2.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file torch_sparse_tensor-0.2.0.tar.gz.

File metadata

  • Download URL: torch_sparse_tensor-0.2.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.14 Linux/6.5.0-1018-azure

File hashes

Hashes for torch_sparse_tensor-0.2.0.tar.gz
Algorithm Hash digest
SHA256 099a107703751a4f2e44264f216b7cc8fae1e7bcff099c311cc4820f19d5f146
MD5 ecd65e7684f4b7a53de2e83c92693124
BLAKE2b-256 c626a5bb3db94ee4c19eb9229c550137383835cdbba6af661ddf1790b782729b

See more details on using hashes here.

File details

Details for the file torch_sparse_tensor-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for torch_sparse_tensor-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 21e2adac9e6541c7443b5de77ea431297973d0faccb0495f05b93f4d9ab20dfc
MD5 7f9c70ddd41832597a97a1197845ff42
BLAKE2b-256 8a81db66f18db9cb7a8b0c752b62980636f3cc23ff6b0deccd2fe9554f95088e

See more details on using hashes here.

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