SNAP Library 6.0, User Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
n2v.h File Reference
#include "stdafx.h"
#include "Snap.h"
#include "biasedrandomwalk.h"
#include "word2vec.h"

Go to the source code of this file.

Functions

void node2vec (PWNet &InNet, const double &ParamP, const double &ParamQ, const int &Dimensions, const int &WalkLen, const int &NumWalks, const int &WinSize, const int &Iter, const bool &Verbose, const bool &OutputWalks, TVVec< TInt, int64 > &WalksVV, TIntFltVH &EmbeddingsHV)
 Calculates node2vec feature representation for nodes and writes them into EmbeddinsHV, see http://arxiv.org/pdf/1607.00653v1.pdf. More...
 
void node2vec (PWNet &InNet, const double &ParamP, const double &ParamQ, const int &Dimensions, const int &WalkLen, const int &NumWalks, const int &WinSize, const int &Iter, const bool &Verbose, TIntFltVH &EmbeddingsHV)
 Version without walk output flag. For backward compatibility. More...
 
void node2vec (const PNGraph &InNet, const double &ParamP, const double &ParamQ, const int &Dimensions, const int &WalkLen, const int &NumWalks, const int &WinSize, const int &Iter, const bool &Verbose, const bool &OutputWalks, TVVec< TInt, int64 > &WalksVV, TIntFltVH &EmbeddingsHV)
 Version for unweighted graphs. More...
 
void node2vec (const PNGraph &InNet, const double &ParamP, const double &ParamQ, const int &Dimensions, const int &WalkLen, const int &NumWalks, const int &WinSize, const int &Iter, const bool &Verbose, TIntFltVH &EmbeddingsHV)
 Version for unweighted graphs without walk output flag. For backward compatibility. More...
 
void node2vec (const PNEANet &InNet, const double &ParamP, const double &ParamQ, const int &Dimensions, const int &WalkLen, const int &NumWalks, const int &WinSize, const int &Iter, const bool &Verbose, const bool &OutputWalks, TVVec< TInt, int64 > &WalksVV, TIntFltVH &EmbeddingsHV)
 Version for weighted graphs. Edges must have TFlt attribute "weight". More...
 
void node2vec (const PNEANet &InNet, const double &ParamP, const double &ParamQ, const int &Dimensions, const int &WalkLen, const int &NumWalks, const int &WinSize, const int &Iter, const bool &Verbose, TIntFltVH &EmbeddingsHV)
 Version for weighted graphs. Edges must have TFlt attribute "weight". No walk output flag. For backward compatibility. More...
 

Function Documentation

void node2vec ( PWNet InNet,
const double &  ParamP,
const double &  ParamQ,
const int &  Dimensions,
const int &  WalkLen,
const int &  NumWalks,
const int &  WinSize,
const int &  Iter,
const bool &  Verbose,
const bool &  OutputWalks,
TVVec< TInt, int64 > &  WalksVV,
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.

8  {
9  //Preprocess transition probabilities
10  PreprocessTransitionProbs(InNet, ParamP, ParamQ, Verbose);
11  TIntV NIdsV;
12  for (TWNet::TNodeI NI = InNet->BegNI(); NI < InNet->EndNI(); NI++) {
13  NIdsV.Add(NI.GetId());
14  }
15  //Generate random walks
16  int64 AllWalks = (int64)NumWalks * NIdsV.Len();
17  WalksVV = TVVec<TInt, int64>(AllWalks,WalkLen);
18  TRnd Rnd(time(NULL));
19  int64 WalksDone = 0;
20  for (int64 i = 0; i < NumWalks; i++) {
21  NIdsV.Shuffle(Rnd);
22 #pragma omp parallel for schedule(dynamic)
23  for (int64 j = 0; j < NIdsV.Len(); j++) {
24  if ( Verbose && WalksDone%10000 == 0 ) {
25  printf("\rWalking Progress: %.2lf%%",(double)WalksDone*100/(double)AllWalks);fflush(stdout);
26  }
27  TIntV WalkV;
28  SimulateWalk(InNet, NIdsV[j], WalkLen, Rnd, WalkV);
29  for (int64 k = 0; k < WalkV.Len(); k++) {
30  WalksVV.PutXY(i*NIdsV.Len()+j, k, WalkV[k]);
31  }
32  WalksDone++;
33  }
34  }
35  if (Verbose) {
36  printf("\n");
37  fflush(stdout);
38  }
39  //Learning embeddings
40  if (!OutputWalks) {
41  LearnEmbeddings(WalksVV, Dimensions, WinSize, Iter, Verbose, EmbeddingsHV);
42  }
43 }
Definition: dt.h:11
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void LearnEmbeddings(TVVec< TInt, int64 > &WalksVV, const int &Dimensions, const int &WinSize, const int &Iter, const bool &Verbose, TIntFltVH &EmbeddingsHV)
Learns embeddings using SGD, Skip-gram with negative sampling.
Definition: word2vec.cpp:160
Definition: ds.h:2223
Node iterator. Only forward iteration (operator++) is supported.
Definition: network.h:537
void SimulateWalk(PWNet &InNet, int64 StartNId, const int &WalkLen, TRnd &Rnd, TIntV &WalkV)
Simulates one walk and writes it into Walk vector.
void PutXY(const TSizeTy &X, const TSizeTy &Y, const TVal &Val)
Definition: ds.h:2267
long long int64
Definition: bd.h:27
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Definition: ds.h:1335
void PreprocessTransitionProbs(PWNet &InNet, const double &ParamP, const double &ParamQ, const bool &Verbose)
Preprocesses transition probabilities for random walks. Has to be called once before SimulateWalk cal...
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
void node2vec ( PWNet InNet,
const double &  ParamP,
const double &  ParamQ,
const int &  Dimensions,
const int &  WalkLen,
const int &  NumWalks,
const int &  WinSize,
const int &  Iter,
const bool &  Verbose,
TIntFltVH EmbeddingsHV 
)

Version without walk output flag. For backward compatibility.

Definition at line 45 of file n2v.cpp.

48  {
49  TVVec <TInt, int64> WalksVV;
50  bool OutputWalks = 0;
51  node2vec(InNet, ParamP, ParamQ, Dimensions, WalkLen, NumWalks, WinSize,
52  Iter, Verbose, OutputWalks, WalksVV, EmbeddingsHV);
53 }
Definition: ds.h:2223
void node2vec(PWNet &InNet, const double &ParamP, const double &ParamQ, const int &Dimensions, const int &WalkLen, const int &NumWalks, const int &WinSize, const int &Iter, const bool &Verbose, const bool &OutputWalks, TVVec< TInt, int64 > &WalksVV, 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
void node2vec ( const PNGraph InNet,
const double &  ParamP,
const double &  ParamQ,
const int &  Dimensions,
const int &  WalkLen,
const int &  NumWalks,
const int &  WinSize,
const int &  Iter,
const bool &  Verbose,
const bool &  OutputWalks,
TVVec< TInt, int64 > &  WalksVV,
TIntFltVH EmbeddingsHV 
)

Version for unweighted graphs.

Definition at line 56 of file n2v.cpp.

60  {
61  PWNet NewNet = PWNet::New();
62  for (TNGraph::TEdgeI EI = InNet->BegEI(); EI < InNet->EndEI(); EI++) {
63  if (!NewNet->IsNode(EI.GetSrcNId())) { NewNet->AddNode(EI.GetSrcNId()); }
64  if (!NewNet->IsNode(EI.GetDstNId())) { NewNet->AddNode(EI.GetDstNId()); }
65  NewNet->AddEdge(EI.GetSrcNId(), EI.GetDstNId(), 1.0);
66  }
67  node2vec(NewNet, ParamP, ParamQ, Dimensions, WalkLen, NumWalks, WinSize, Iter,
68  Verbose, OutputWalks, WalksVV, EmbeddingsHV);
69 }
static TPt New()
Definition: bd.h:479
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:430
void node2vec(PWNet &InNet, const double &ParamP, const double &ParamQ, const int &Dimensions, const int &WalkLen, const int &NumWalks, const int &WinSize, const int &Iter, const bool &Verbose, const bool &OutputWalks, TVVec< TInt, int64 > &WalksVV, 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 ( const PNGraph InNet,
const double &  ParamP,
const double &  ParamQ,
const int &  Dimensions,
const int &  WalkLen,
const int &  NumWalks,
const int &  WinSize,
const int &  Iter,
const bool &  Verbose,
TIntFltVH EmbeddingsHV 
)

Version for unweighted graphs without walk output flag. For backward compatibility.

Definition at line 71 of file n2v.cpp.

74  {
75  TVVec <TInt, int64> WalksVV;
76  bool OutputWalks = 0;
77  node2vec(InNet, ParamP, ParamQ, Dimensions, WalkLen, NumWalks, WinSize,
78  Iter, Verbose, OutputWalks, WalksVV, EmbeddingsHV);
79 }
Definition: ds.h:2223
void node2vec(PWNet &InNet, const double &ParamP, const double &ParamQ, const int &Dimensions, const int &WalkLen, const int &NumWalks, const int &WinSize, const int &Iter, const bool &Verbose, const bool &OutputWalks, TVVec< TInt, int64 > &WalksVV, 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
void node2vec ( const PNEANet InNet,
const double &  ParamP,
const double &  ParamQ,
const int &  Dimensions,
const int &  WalkLen,
const int &  NumWalks,
const int &  WinSize,
const int &  Iter,
const bool &  Verbose,
const bool &  OutputWalks,
TVVec< TInt, int64 > &  WalksVV,
TIntFltVH EmbeddingsHV 
)

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

Definition at line 81 of file n2v.cpp.

85  {
86  PWNet NewNet = PWNet::New();
87  for (TNEANet::TEdgeI EI = InNet->BegEI(); EI < InNet->EndEI(); EI++) {
88  if (!NewNet->IsNode(EI.GetSrcNId())) { NewNet->AddNode(EI.GetSrcNId()); }
89  if (!NewNet->IsNode(EI.GetDstNId())) { NewNet->AddNode(EI.GetDstNId()); }
90  NewNet->AddEdge(EI.GetSrcNId(), EI.GetDstNId(), InNet->GetFltAttrDatE(EI,"weight"));
91  }
92  node2vec(NewNet, ParamP, ParamQ, Dimensions, WalkLen, NumWalks, WinSize, Iter,
93  Verbose, OutputWalks, WalksVV, EmbeddingsHV);
94 }
static TPt New()
Definition: bd.h:479
Edge iterator. Only forward iteration (operator++) is supported.
Definition: network.h:1867
void node2vec(PWNet &InNet, const double &ParamP, const double &ParamQ, const int &Dimensions, const int &WalkLen, const int &NumWalks, const int &WinSize, const int &Iter, const bool &Verbose, const bool &OutputWalks, TVVec< TInt, int64 > &WalksVV, 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 ( const PNEANet InNet,
const double &  ParamP,
const double &  ParamQ,
const int &  Dimensions,
const int &  WalkLen,
const int &  NumWalks,
const int &  WinSize,
const int &  Iter,
const bool &  Verbose,
TIntFltVH EmbeddingsHV 
)

Version for weighted graphs. Edges must have TFlt attribute "weight". No walk output flag. For backward compatibility.

Definition at line 96 of file n2v.cpp.

99  {
100  TVVec <TInt, int64> WalksVV;
101  bool OutputWalks = 0;
102  node2vec(InNet, ParamP, ParamQ, Dimensions, WalkLen, NumWalks, WinSize,
103  Iter, Verbose, OutputWalks, WalksVV, EmbeddingsHV);
104 }
Definition: ds.h:2223
void node2vec(PWNet &InNet, const double &ParamP, const double &ParamQ, const int &Dimensions, const int &WalkLen, const int &NumWalks, const int &WinSize, const int &Iter, const bool &Verbose, const bool &OutputWalks, TVVec< TInt, int64 > &WalksVV, 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