SNAP Library 4.0, User 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 File Reference
#include "stdafx.h"
#include "n2v.h"

Go to the source code of this file.

Functions

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. More...
 
void node2vec (PNGraph &InNet, double &ParamP, double &ParamQ, int &Dimensions, int &WalkLen, int &NumWalks, int &WinSize, int &Iter, bool &Verbose, TIntFltVH &EmbeddingsHV)
 Version for unweighted graphs. More...
 
void node2vec (PNEANet &InNet, double &ParamP, double &ParamQ, int &Dimensions, int &WalkLen, int &NumWalks, int &WinSize, int &Iter, bool &Verbose, TIntFltVH &EmbeddingsHV)
 Version for weighted graphs. Edges must have TFlt attribute "weight". More...
 

Function Documentation

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 at line 4 of file n2v.cpp.

6  {
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 }
Definition: dt.h:11
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void SimulateWalk(PWNet &InNet, int64 StartNId, int &WalkLen, TRnd &Rnd, TIntV &WalkV)
Simulates one walk and writes it into Walk vector.
Definition: ds.h:2222
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...
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
long long int64
Definition: bd.h:27
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
void node2vec ( PNGraph InNet,
double &  ParamP,
double &  ParamQ,
int &  Dimensions,
int &  WalkLen,
int &  NumWalks,
int &  WinSize,
int &  Iter,
bool &  Verbose,
TIntFltVH EmbeddingsHV 
)

Version for unweighted graphs.

Definition at line 41 of file n2v.cpp.

43  {
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 }
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
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:586
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:426
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
Definition: bd.h:196
void node2vec ( PNEANet InNet,
double &  ParamP,
double &  ParamQ,
int &  Dimensions,
int &  WalkLen,
int &  NumWalks,
int &  WinSize,
int &  Iter,
bool &  Verbose,
TIntFltVH EmbeddingsHV 
)

Version for weighted graphs. Edges must have TFlt attribute "weight".

Definition at line 54 of file n2v.cpp.

56  {
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 }
static TPt New()
Definition: bd.h:479
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
Edge iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1867
Definition: bd.h:196