-
-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pagerank
example
#673
Open
mtsokol
wants to merge
4
commits into
main
Choose a base branch
from
pagerank-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add pagerank
example
#673
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import importlib | ||
import os | ||
import time | ||
|
||
import sparse | ||
|
||
import networkx as nx | ||
from networkx.algorithms.link_analysis.pagerank_alg import _pagerank_scipy | ||
|
||
import numpy as np | ||
import scipy.sparse as sp | ||
|
||
|
||
def pagerank(G, alpha=0.85, max_iter=100, tol=1e-6) -> dict: | ||
N = len(G) | ||
if N == 0: | ||
return {} | ||
|
||
alpha = sparse.asarray(alpha) | ||
nodelist = list(G) | ||
A = nx.to_scipy_sparse_array(G, dtype=float, format="csc") | ||
A = sparse.asarray(A) | ||
S = sparse.sum(A, axis=1) | ||
S = sparse.where(sparse.asarray(0.0) != S, sparse.asarray(1.0) / S, S) | ||
|
||
# TODO: spdiags https://github.com/willow-ahrens/Finch.jl/issues/499 | ||
Q = sparse.asarray(sp.csc_array(sp.spdiags(S.todense(), 0, *A.shape))) | ||
A = Q @ A | ||
|
||
# initial vector | ||
x = sparse.full((1, N), fill_value=1.0 / N) | ||
|
||
# personalization vector | ||
p = sparse.full((1, N), fill_value=1.0 / N) | ||
|
||
# Dangling nodes | ||
dangling_weights = p | ||
|
||
# power iteration: make up to max_iter iterations | ||
for _ in range(max_iter): | ||
xlast = x | ||
x_dangling = sparse.where(S[None, :] == sparse.asarray(0.0), x, sparse.asarray(0.0)) | ||
x = ( | ||
alpha * (x @ A + sparse.asarray(sparse.sum(x_dangling)) * dangling_weights) | ||
+ (sparse.asarray(1) - alpha) * p | ||
) | ||
# check convergence, l1 norm | ||
err = sparse.sum(sparse.abs(x - xlast)) | ||
if err < N * tol: | ||
return dict(zip(nodelist, map(float, x[0, :]), strict=False)) | ||
|
||
raise nx.PowerIterationFailedConvergence(max_iter) | ||
|
||
|
||
if __name__ == "__main__": | ||
G = nx.DiGraph(nx.path_graph(4)) | ||
ITERS = 3 | ||
|
||
os.environ[sparse._ENV_VAR_NAME] = "Finch" | ||
importlib.reload(sparse) | ||
|
||
# compile | ||
pagerank(G) | ||
print("compiled") | ||
|
||
# finch | ||
start = time.time() | ||
for i in range(ITERS): | ||
print(f"finch iter: {i}") | ||
pr = pagerank(G) | ||
elapsed = time.time() - start | ||
print(f"Finch took {elapsed / ITERS} s.") | ||
|
||
# scipy | ||
start = time.time() | ||
for i in range(ITERS): | ||
print(f"scipy iter: {i}") | ||
scipy_pr = _pagerank_scipy(G) | ||
elapsed = time.time() - start | ||
print(f"SciPy took {elapsed / ITERS} s.") | ||
|
||
np.testing.assert_almost_equal(list(pr.values()), list(scipy_pr.values())) | ||
print(f"finch: {pr}") | ||
print(f"scipy: {scipy_pr}") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC This is equivalent to https://data-apis.org/array-api/latest/extensions/generated/array_api.linalg.diagonal.html#diagonal, which is supported by the Numba backend. Let's use that instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Finch, we can add support for it later, as the example only needs to work on Numba for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might need
Q[None, :]
to match the results, but otherwise this is equivalent.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the
pagerank
example is run either with Finch or Numba backend. when we run with Finch backend (and I think it should be runnable with Finch) then we can't execute one line with Numba.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to keep these examples Finch and Numba ready, and update implementation once both backends support something new.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the deliverable, we just need a script that will demonstrate a speedup against the "old" code. If you would prefer, we can always keep this PR open until
xp.diagonal
is supported by Finch, but I wouldn't densify.