Skip to content

Commit

Permalink
Add verbose option
Browse files Browse the repository at this point in the history
Print time usage for random walk generation and skip-gram trainnig when set to True
  • Loading branch information
RemyLau committed Oct 2, 2021
1 parent 04585ef commit 6a0a733
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/pecanpy/node2vec.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Different strategies for generating node2vec walks."""

from time import time

import numpy as np
from gensim.models import Word2Vec
from numba import get_num_threads, jit, prange
Expand Down Expand Up @@ -171,7 +173,8 @@ def preprocess_transition_probs(self):
"""Null default preprocess method."""
pass

def embed(self, dim=128, num_walks=10, walk_length=80, window_size=10, epochs=1):
def embed(self, dim=128, num_walks=10, walk_length=80, window_size=10,
epochs=1, verbose=False):
"""Generate embeddings.
This is a shortcut function that combines ``simulate_walks`` with
Expand All @@ -190,15 +193,24 @@ def embed(self, dim=128, num_walks=10, walk_length=80, window_size=10, epochs=1)
``Word2Vec`` model, default is 10
epochs (int): number of epochs for training ``Word2Vec``, default
is 1
verbose (bool): print time usage for random walk generation and
skip-gram training if set to True
Return:
numpy.ndarray: The embedding matrix, each row is a node embedding
vector. The index is the same as that for the graph.
"""
t = time()
walks = self.simulate_walks(num_walks=num_walks, walk_length=walk_length)
if verbose:
print(f"Took {time() - t:.2f} sec to generate walks")

t = time()
w2v = Word2Vec(walks, vector_size=dim, window=window_size, sg=1,
min_count=0, workers=self.workers, epochs=epochs)
if verbose:
print(f"Took {time() - t:.2f} sec to train")

# index mapping back to node IDs
idx_list = [w2v.wv.get_index(i) for i in self.IDlst]
Expand Down

0 comments on commit 6a0a733

Please sign in to comment.