node2vec
node2vec copied to clipboard
support python 3+ on learn_embedding method.
learn_embeddings needs minor modification to accept the python3+ 's map function. implemented below
def learn_embeddings(walks): ''' Learn embeddings by optimizing the Skipgram objective using SGD. ''' #print(type(walks)) #walks = [map(str, walk) for walk in walks] walks= [str(j) for i in walks for j in i]
Actually that alone does not fix the issue with Python3 implementation. Above solution I offered has to be changed to the following.
def learn_embeddings(walks): ''' Learn embeddings by optimizing the Skipgram objective using SGD. ''' walks = [str(walk) for walk in walks]
model = Word2Vec([walks], size=args.dimensions, window=args.window_size, min_count=1, sg=1, workers=args.workers, iter=args.iter)
model.wv.save_word2vec_format(args.output)
print('vocalublary', list(model.wv.vocab), 'length', len(list(model.wv.vocab)))
return
why you add '[]' for variable 'walks' again? this mean get walk list embedding instead of node in walks?
fix at #35
hi, thanks your modification! But in my try, walks = [str(walk) for walk in walks] doesn't work, and the final embeddings are of random walks rather than nodes. In my opinion, only list() is needed. Maybe, you can try this walks = [list(map(str, walk)) for walk in walks]. Meanwhile, '[]' can be removed.
can change this sentence to walks = np.array(walks, dtype=str).tolist()
the origin map function aims to change the type of each element into str
The following works fine on python3:
def learn_embeddings(walks):
'''
Learn embeddings by optimizing the Skipgram objective using SGD.
'''
walks = [list(map(str, walk)) for walk in walks]
model = Word2Vec(walks, size=args.dimensions, window=args.window_size, min_count=0, sg=1, workers=args.workers, iter=args.iter)
model.wv.save_word2vec_format(args.output)
print('vocalublary', list(model.wv.vocab), 'length', len(list(model.wv.vocab)))
return