Skip to content

Commit 9165c76

Browse files
committed
Merge tag 'v1.0.4' into develop
- (new) Cupy implementation of KPM - (fix) fix tf version in requirements
2 parents 78ca892 + 7c90c19 commit 9165c76

File tree

11 files changed

+94
-28
lines changed

11 files changed

+94
-28
lines changed

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# ![eMaTe](emate.png)
22

3-
eMaTe is a python package implemented in tensorflow which the main goal is provide useful methods capable of estimate spectral densities and trace functions of large sparse matrices.
3+
eMaTe it is a python package which the main goal is to provide methods capable of estimating the spectral densities and trace
4+
functions of large sparse matrices. eMaTe can run in both CPU and GPU and can estimate the spectral density and related trace functions, such as entropy and Estrada index, even in directed or undirected networks with million of nodes.
45

56
## Install
67
```
78
pip install emate
89
```
910

11+
If you a have a GPU you should also install cupy.
1012
## Kernel Polynomial Method (KPM)
1113

1214
The Kernel Polynomial Method can estimate the spectral density of large sparse Hermitan matrices with a computational cost almost linear. This method combines three key ingredients: the Chebyshev expansion + the stochastic trace estimator + kernel smoothing.
@@ -26,15 +28,15 @@ vals = np.linalg.eigvalsh(W).real
2628
```
2729

2830
```python
29-
from emate.hermitian import pykpm
31+
from emate.hermitian import tfkpm
3032
from stdog.utils.misc import ig2sparse
3133

3234
W = ig2sparse(G)
3335

34-
num_moments = 300
35-
num_vecs = 200
36+
num_moments = 40
37+
num_vecs = 40
3638
extra_points = 10
37-
ek, rho = pykpm(W, num_moments, num_vecs, extra_points)
39+
ek, rho = tfkpm(W, num_moments, num_vecs, extra_points)
3840
```
3941

4042
```python
@@ -43,6 +45,18 @@ plt.hist(vals, density=True, bins=100, alpha=.9, color="steelblue")
4345
plt.scatter(ek, rho, c="tomato", zorder=999, alpha=0.9, marker="d")
4446

4547
```
48+
If the CUPY package it is available in your machine, you can also use the cupy implementation. When compared to tf-kpm, the
49+
Cupy-kpm is slower for median matrices (100k) and faster for larger matrices (> 10^6). The main reason it's because the tf-kpm was implemented in order to calc all te moments in a single step.
50+
51+
```python
52+
from emate.hermitian import cupykpm
53+
54+
num_moments = 40
55+
num_vecs = 40
56+
extra_points = 10
57+
ek, rho = cupykpm(W, num_moments, num_vecs, extra_points)
58+
```
59+
4660

4761
![](docs/source/imgs/kpm.png)
4862

emate.png

9.84 KB
Loading

emate/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
from emate import linalg
55
from emate import utils
6+
7+
try:
8+
import cupy as cp
9+
except:
10+
print("Warning: Cupy package not found")
11+
612
__version__ = "1.0.4"
713
__license__ = ""
814
__author__ = "Bruno Messias; Thomas K Peron"

emate/hermitian/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from emate.hermitian.kpm import pykpm, cupykpm
1+
from emate.hermitian.kpm import pykpm, cupykpm, tfkpm
22
from emate.hermitian import tfops, cupyops
33

4-
__all__ = ["pykpm", "tfops", "cupykpm", "cupykpm"]
4+
__all__ = ["pykpm", "tfops", "cupykpm", "cupykpm", "tfkpm"]

emate/hermitian/cupyops/kpm.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@
2020
2121
"""
2222
import numpy as np
23-
import cupy as cp
23+
try:
24+
import cupy as cp
25+
except:
26+
cp = None
27+
2428
from emate.utils.cupyops.signal import dctIII
2529

2630

2731
def get_moments(
2832
H_rescaled,
2933
num_moments,
3034
dimension,
31-
cp_complex=cp.complex64
35+
precision=32
3236
):
3337
"""
3438
Parameters
@@ -44,7 +48,10 @@ def get_moments(
4448
Returns
4549
-------
4650
"""
47-
51+
cp_complex = cp.complex64
52+
if precision == 64:
53+
cp_complex = cp.complex128
54+
4855
alpha0 = cp.exp(1j*2*cp.pi*cp.random.rand(dimension))
4956
alpha1 = H_rescaled.dot(alpha0)
5057
mu = cp.zeros(num_moments, dtype=cp_complex)

emate/hermitian/kpm.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
"""
3333
import numpy as np
3434
import tensorflow as tf
35-
import cupy as cp
35+
try:
36+
import cupy as cp
37+
except:
38+
cp = None
3639

3740
from emate.linalg import rescale_matrix, get_bounds
3841
from emate.linalg.misc import rescale_cupy
@@ -206,10 +209,10 @@ def cupykpm(
206209
H, scale_fact_a, scale_fact_b = rescale_cupy(H, lmin, lmax, epsilon)
207210

208211
moments = cp.array([
209-
cupyops.get_moments(H, num_moments, dimension)
212+
cupyops.get_moments(H, num_moments, dimension, precision=precision)
210213
for i in range(num_vecs)
211214
])
212-
kernel0 = cupy_jackson(num_moments, precision=32)
215+
kernel0 = cupy_jackson(num_moments, precision=precision)
213216

214217
ek, rho = cupyops.apply_kernel(
215218
moments,

emate/linalg/misc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
"""
1111
import scipy
1212
import scipy.sparse.linalg
13-
import cupy as cp
13+
try:
14+
import cupy as cp
15+
except:
16+
cp = None
1417

1518

1619
def get_bounds(

emate/utils/cupyops/kernels.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
- jackson
1313
"""
1414

15-
import cupy as cp
15+
try:
16+
import cupy as cp
17+
except:
18+
cp = None
1619

1720

1821
def jackson(

emate/utils/cupyops/signal.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
Cosine transform of type III.
1212
"""
1313

14-
15-
import cupy as cp
14+
try:
15+
import cupy as cp
16+
except:
17+
cp = None
1618

1719

1820
def dctIII(x, precision=32):

examples/kpm.ipynb

Lines changed: 38 additions & 10 deletions
Large diffs are not rendered by default.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name="emate",
9-
version="v1.0.3",
9+
version="v1.0.4",
1010
packages=find_packages(exclude=["build", ]),
1111
long_description=README_TEXT,
1212
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)