SNAP Library 4.0, Developer Reference  2017-07-27 13:18:06
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
n2v.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "n2v.h"
3 
4 void node2vec(PWNet& InNet, double& ParamP, double& ParamQ, int& Dimensions,
5  int& WalkLen, int& NumWalks, int& WinSize, int& Iter, bool& Verbose,
6  TIntFltVH& EmbeddingsHV) {
7  //Preprocess transition probabilities
8  PreprocessTransitionProbs(InNet, ParamP, ParamQ, Verbose);
9  TIntV NIdsV;
10  for (TWNet::TNodeI NI = InNet->BegNI(); NI < InNet->EndNI(); NI++) {
11  NIdsV.Add(NI.GetId());
12  }
13  //Generate random walks
14  int64 AllWalks = (int64)NumWalks * NIdsV.Len();
15  TVVec<TInt, int64> WalksVV(AllWalks,WalkLen);
16  TRnd Rnd(time(NULL));
17  int64 WalksDone = 0;
18  for (int64 i = 0; i < NumWalks; i++) {
19  NIdsV.Shuffle(Rnd);
20 #pragma omp parallel for schedule(dynamic)
21  for (int64 j = 0; j < NIdsV.Len(); j++) {
22  if ( Verbose && WalksDone%10000 == 0 ) {
23  printf("\rWalking Progress: %.2lf%%",(double)WalksDone*100/(double)AllWalks);fflush(stdout);
24  }
25  TIntV WalkV;
26  SimulateWalk(InNet, NIdsV[j], WalkLen, Rnd, WalkV);
27  for (int64 k = 0; k < WalkV.Len(); k++) {
28  WalksVV.PutXY(i*NIdsV.Len()+j, k, WalkV[k]);
29  }
30  WalksDone++;
31  }
32  }
33  if (Verbose) {
34  printf("\n");
35  fflush(stdout);
36  }
37  //Learning embeddings
38  LearnEmbeddings(WalksVV, Dimensions, WinSize, Iter, Verbose, EmbeddingsHV);
39 }
40 
41 void node2vec(PNGraph& InNet, double& ParamP, double& ParamQ, int& Dimensions,
42  int& WalkLen, int& NumWalks, int& WinSize, int& Iter, bool& Verbose,
43  TIntFltVH& EmbeddingsHV) {
44  PWNet NewNet = PWNet::New();
45  for (TNGraph::TEdgeI EI = InNet->BegEI(); EI < InNet->EndEI(); EI++) {
46  if (!NewNet->IsNode(EI.GetSrcNId())) { NewNet->AddNode(EI.GetSrcNId()); }
47  if (!NewNet->IsNode(EI.GetDstNId())) { NewNet->AddNode(EI.GetDstNId()); }
48  NewNet->AddEdge(EI.GetSrcNId(), EI.GetDstNId(), 1.0);
49  }
50  node2vec(NewNet, ParamP, ParamQ, Dimensions, WalkLen, NumWalks, WinSize, Iter,
51  Verbose, EmbeddingsHV);
52 }
53 
54 void node2vec(PNEANet& InNet, double& ParamP, double& ParamQ,
55  int& Dimensions, int& WalkLen, int& NumWalks, int& WinSize, int& Iter, bool& Verbose,
56  TIntFltVH& EmbeddingsHV) {
57  PWNet NewNet = PWNet::New();
58  for (TNEANet::TEdgeI EI = InNet->BegEI(); EI < InNet->EndEI(); EI++) {
59  if (!NewNet->IsNode(EI.GetSrcNId())) { NewNet->AddNode(EI.GetSrcNId()); }
60  if (!NewNet->IsNode(EI.GetDstNId())) { NewNet->AddNode(EI.GetDstNId()); }
61  NewNet->AddEdge(EI.GetSrcNId(), EI.GetDstNId(), InNet->GetFltAttrDatE(EI,"weight"));
62  }
63  node2vec(NewNet, ParamP, ParamQ, Dimensions, WalkLen, NumWalks, WinSize, Iter,
64  Verbose, EmbeddingsHV);
65 }
Definition: dt.h:11
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:588
static TPt New()
Definition: bd.h:479
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:586
void SimulateWalk(PWNet &InNet, int64 StartNId, int &WalkLen, TRnd &Rnd, TIntV &WalkV)
Simulates one walk and writes it into Walk vector.
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:426
Definition: ds.h:2222
void node2vec(PWNet &InNet, double &ParamP, double &ParamQ, int &Dimensions, int &WalkLen, int &NumWalks, int &WinSize, int &Iter, bool &Verbose, TIntFltVH &EmbeddingsHV)
Calculates node2vec feature representation for nodes and writes them into EmbeddinsHV, see http://arxiv.org/pdf/1607.00653v1.pdf.
Definition: n2v.cpp:4
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:537
void PreprocessTransitionProbs(PWNet &InNet, double &ParamP, double &ParamQ, bool &Verbose)
Preprocesses transition probabilities for random walks. Has to be called once before SimulateWalk cal...
Edge iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1867
void LearnEmbeddings(TVVec< TInt, int64 > &WalksVV, int &Dimensions, int &WinSize, int &Iter, bool &Verbose, TIntFltVH &EmbeddingsHV)
Learns embeddings using SGD, Skip-gram with negative sampling.
Definition: word2vec.cpp:148
void PutXY(const TSizeTy &X, const TSizeTy &Y, const TVal &Val)
Definition: ds.h:2266
long long int64
Definition: bd.h:27
Definition: hash.h:97
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
Definition: bd.h:196
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602