Commit c5cd949e by Jan Wijffels

Renamed verse to verse_pagerank

parent 82b9da19
# Generated by using Rcpp::compileAttributes() -> do not edit by hand # Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
embeddings_verse <- function(dgrmatrix_p, dgrmatrix_j, n_epochs = 100000L) { embeddings_verse_pagerank <- function(dgrmatrix_p, dgrmatrix_j, dimension = 128L, epochs = 100000L, learning_rate = 0.0025, samples = 3L, alpha = 0.85, threads = 1L) {
.Call('_deepwalker_embeddings_verse', PACKAGE = 'deepwalker', dgrmatrix_p, dgrmatrix_j, n_epochs) .Call('_deepwalker_embeddings_verse_pagerank', PACKAGE = 'deepwalker', dgrmatrix_p, dgrmatrix_j, dimension, epochs, learning_rate, samples, alpha, threads)
} }
...@@ -2,7 +2,19 @@ ...@@ -2,7 +2,19 @@
#' @description This function calculates node embeddings in a graph by using #' @description This function calculates node embeddings in a graph by using
#' VERSE (Versatile Graph Embeddings from Similarity Measures) #' VERSE (Versatile Graph Embeddings from Similarity Measures)
#' @param x an object of class dgRMatrix #' @param x an object of class dgRMatrix
#' @return TODO #' @param dimension integer with the dimension of the embedding
#' @param epochs number of epochs
#' @param learning_rate the learning rate of the algorithm
#' @param samples integer
#' @param alpha the alpha parameter of the algorithm
#' @return
#' a list with elements
#' \enumerate{
#' \item{vertices: }{the number of vertices in the graph}
#' \item{edges: }{the number of edges (links) between the vertices in the graph}
#' \item{embedding: }{the embeddings of the vertices}
#' \item{edges_data and offsets_data: }{for debugging purposes now}
#' }
#' @export #' @export
#' @examples #' @examples
#' library(igraphdata) #' library(igraphdata)
...@@ -13,8 +25,28 @@ ...@@ -13,8 +25,28 @@
#' x <- as_adj(karate) #' x <- as_adj(karate)
#' x <- as(x, "RsparseMatrix") #' x <- as(x, "RsparseMatrix")
#' #'
#' embeddings <- deepwalker_verse(x) #' embeddings <- deepwalker_verse(x, dimension = 64, epochs = 1000)
deepwalker_verse <- function(x){ deepwalker_verse <- function(x,
emb <- embeddings_verse(x@p, x@j, n_epochs = 10) similarity = c("pagerank", "adjacency", "simrank"),
dimension = 128,
epochs = 100000,
learning_rate = 0.0025,
samples = 3,
alpha = 0.85){
stopifnot(inherits(x, "dgRMatrix"))
similarity <- match.arg(similarity)
if(similarity == "pagerank"){
emb <- embeddings_verse_pagerank(x@p, x@j,
dimension = dimension,
epochs = epochs,
learning_rate = learning_rate,
samples = samples,
alpha = alpha)
}else if(similarity == "adjacency"){
}else if(similarity == "simrank"){
}
emb emb
} }
\ No newline at end of file
...@@ -4,13 +4,31 @@ ...@@ -4,13 +4,31 @@
\alias{deepwalker_verse} \alias{deepwalker_verse}
\title{Versatile Graph Embeddings from Similarity Measures} \title{Versatile Graph Embeddings from Similarity Measures}
\usage{ \usage{
deepwalker_verse(x) deepwalker_verse(x, similarity = c("pagerank", "adjacency", "simrank"),
dimension = 128, epochs = 1e+05, learning_rate = 0.0025, samples = 3,
alpha = 0.85)
} }
\arguments{ \arguments{
\item{x}{an object of class dgRMatrix} \item{x}{an object of class dgRMatrix}
\item{dimension}{integer with the dimension of the embedding}
\item{epochs}{number of epochs}
\item{learning_rate}{the learning rate of the algorithm}
\item{samples}{integer}
\item{alpha}{the alpha parameter of the algorithm}
} }
\value{ \value{
TODO a list with elements
\enumerate{
\item{vertices: }{the number of vertices in the graph}
\item{edges: }{the number of edges (links) between the vertices in the graph}
\item{embedding: }{the embeddings of the vertices}
\item{edges_data and offsets_data: }{for debugging purposes now}
}
} }
\description{ \description{
This function calculates node embeddings in a graph by using This function calculates node embeddings in a graph by using
...@@ -25,5 +43,5 @@ data(karate, package = "igraphdata") ...@@ -25,5 +43,5 @@ data(karate, package = "igraphdata")
x <- as_adj(karate) x <- as_adj(karate)
x <- as(x, "RsparseMatrix") x <- as(x, "RsparseMatrix")
embeddings <- deepwalker_verse(x) embeddings <- deepwalker_verse(x, dimension = 64, epochs = 1000)
} }
...@@ -5,22 +5,27 @@ ...@@ -5,22 +5,27 @@
using namespace Rcpp; using namespace Rcpp;
// embeddings_verse // embeddings_verse_pagerank
SEXP embeddings_verse(Rcpp::IntegerVector dgrmatrix_p, Rcpp::IntegerVector dgrmatrix_j, int n_epochs); SEXP embeddings_verse_pagerank(Rcpp::IntegerVector dgrmatrix_p, Rcpp::IntegerVector dgrmatrix_j, int dimension, int epochs, float learning_rate, int samples, float alpha, int threads);
RcppExport SEXP _deepwalker_embeddings_verse(SEXP dgrmatrix_pSEXP, SEXP dgrmatrix_jSEXP, SEXP n_epochsSEXP) { RcppExport SEXP _deepwalker_embeddings_verse_pagerank(SEXP dgrmatrix_pSEXP, SEXP dgrmatrix_jSEXP, SEXP dimensionSEXP, SEXP epochsSEXP, SEXP learning_rateSEXP, SEXP samplesSEXP, SEXP alphaSEXP, SEXP threadsSEXP) {
BEGIN_RCPP BEGIN_RCPP
Rcpp::RObject rcpp_result_gen; Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen; Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::IntegerVector >::type dgrmatrix_p(dgrmatrix_pSEXP); Rcpp::traits::input_parameter< Rcpp::IntegerVector >::type dgrmatrix_p(dgrmatrix_pSEXP);
Rcpp::traits::input_parameter< Rcpp::IntegerVector >::type dgrmatrix_j(dgrmatrix_jSEXP); Rcpp::traits::input_parameter< Rcpp::IntegerVector >::type dgrmatrix_j(dgrmatrix_jSEXP);
Rcpp::traits::input_parameter< int >::type n_epochs(n_epochsSEXP); Rcpp::traits::input_parameter< int >::type dimension(dimensionSEXP);
rcpp_result_gen = Rcpp::wrap(embeddings_verse(dgrmatrix_p, dgrmatrix_j, n_epochs)); Rcpp::traits::input_parameter< int >::type epochs(epochsSEXP);
Rcpp::traits::input_parameter< float >::type learning_rate(learning_rateSEXP);
Rcpp::traits::input_parameter< int >::type samples(samplesSEXP);
Rcpp::traits::input_parameter< float >::type alpha(alphaSEXP);
Rcpp::traits::input_parameter< int >::type threads(threadsSEXP);
rcpp_result_gen = Rcpp::wrap(embeddings_verse_pagerank(dgrmatrix_p, dgrmatrix_j, dimension, epochs, learning_rate, samples, alpha, threads));
return rcpp_result_gen; return rcpp_result_gen;
END_RCPP END_RCPP
} }
static const R_CallMethodDef CallEntries[] = { static const R_CallMethodDef CallEntries[] = {
{"_deepwalker_embeddings_verse", (DL_FUNC) &_deepwalker_embeddings_verse, 3}, {"_deepwalker_embeddings_verse_pagerank", (DL_FUNC) &_deepwalker_embeddings_verse_pagerank, 8},
{NULL, NULL, 0} {NULL, NULL, 0}
}; };
......
TARGETS = verse verse-neigh verse-simrank
CXXFLAGS = -std=c++11 -march=native
ifeq ($(CXX), g++)
CXXFLAGS += -fopenmp -Ofast
else ifeq ($(CXX), icpc)
CXXFLAGS += -qopenmp -O3 -no-prec-div -ansi-alias -ip -static-intel
else ifeq ($(CXX), clang++)
CXXFLAGS += -fopenmp=libomp -O3 -Wno-shift-op-parentheses
else
# use g++ by default
CXX = g++
CXXFLAGS += -fopenmp -Ofast
endif
all: $(TARGETS)
$(TARGETS):
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $@.cpp
clean:
rm -rfv $(TARGETS)
.PHONY: all clean
\ No newline at end of file
...@@ -137,17 +137,6 @@ inline int sample_rw(int node) { ...@@ -137,17 +137,6 @@ inline int sample_rw(int node) {
return n2; return n2;
} }
int ArgPos(char *str, int argc, char **argv) {
for (int a = 1; a < argc; a++)
if (!strcmp(str, argv[a])) {
if (a == argc - 1) {
cout << "Argument missing for " << str << endl;
exit(1);
}
return a;
}
return -1;
}
inline void update(float *w_s, float *w_t, int label, const float bias) { inline void update(float *w_s, float *w_t, int label, const float bias) {
float score = -bias; float score = -bias;
...@@ -182,7 +171,7 @@ void Train() { ...@@ -182,7 +171,7 @@ void Train() {
break; break;
if (tid == 0) if (tid == 0)
if (!silent) if (!silent)
cout << fixed << "\r Progress " << std::setprecision(2) Rcpp::Rcout << fixed << "\r Progress " << std::setprecision(2)
<< step / (float)(total_steps + 1) * 100 << "%"; << step / (float)(total_steps + 1) * 100 << "%";
last_ncount = ncount; last_ncount = ncount;
} }
...@@ -203,16 +192,21 @@ void Train() { ...@@ -203,16 +192,21 @@ void Train() {
// [[Rcpp::export]] // [[Rcpp::export]]
SEXP embeddings_verse(Rcpp::IntegerVector dgrmatrix_p, Rcpp::IntegerVector dgrmatrix_j, SEXP embeddings_verse_pagerank(Rcpp::IntegerVector dgrmatrix_p, Rcpp::IntegerVector dgrmatrix_j,
int n_epochs = 100000) { int dimension = 128,
int epochs = 100000,
float learning_rate = 0.0025,
int samples = 3,
float alpha = 0.85,
int threads = 1) {
step = 0; step = 0;
silent = false; silent = false;
n_threads = 1; n_threads = threads;
global_lr = 0.0025f; global_lr = learning_rate;
//n_epochs = ; n_epochs = epochs;
n_hidden = 128; n_hidden = dimension;
n_samples = 3; n_samples = samples;
ppralpha = 0.85f; ppralpha = alpha;
// //
// Set random nr // Set random nr
...@@ -249,12 +243,12 @@ SEXP embeddings_verse(Rcpp::IntegerVector dgrmatrix_p, Rcpp::IntegerVector dgrma ...@@ -249,12 +243,12 @@ SEXP embeddings_verse(Rcpp::IntegerVector dgrmatrix_p, Rcpp::IntegerVector dgrma
for (int i = 0; i < (int)nv; i++) for (int i = 0; i < (int)nv; i++)
degrees[i] = offsets[i + 1] - offsets[i]; degrees[i] = offsets[i + 1] - offsets[i];
total_steps = n_epochs * (long long)nv; total_steps = n_epochs * (long long)nv;
cout << "Total steps (mil): " << total_steps / 1000000. << endl; Rcpp::Rcout << "Total steps (mil): " << total_steps / 1000000. << endl;
chrono::steady_clock::time_point begin = chrono::steady_clock::now(); chrono::steady_clock::time_point begin = chrono::steady_clock::now();
Train(); Train();
chrono::steady_clock::time_point end = chrono::steady_clock::now(); chrono::steady_clock::time_point end = chrono::steady_clock::now();
cout << endl Rcpp::Rcout << endl
<< "Calculations took " << "Calculations took "
<< chrono::duration_cast<std::chrono::duration<float>>(end - begin) << chrono::duration_cast<std::chrono::duration<float>>(end - begin)
.count() .count()
...@@ -277,8 +271,8 @@ SEXP embeddings_verse(Rcpp::IntegerVector dgrmatrix_p, Rcpp::IntegerVector dgrma ...@@ -277,8 +271,8 @@ SEXP embeddings_verse(Rcpp::IntegerVector dgrmatrix_p, Rcpp::IntegerVector dgrma
} }
*/ */
Rcpp::NumericMatrix embeddings(nv, n_hidden); Rcpp::NumericMatrix embeddings(nv, n_hidden);
for (int i = 0; i < nv; i++){ for (int i = 0; i < (int)nv; i++){
for (int j = 0; j < n_hidden; j++){ for (int j = 0; j < (int)n_hidden; j++){
embeddings(i, j) = w0[i*nv + j]; embeddings(i, j) = w0[i*nv + j];
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment