本文整理汇总了C++中graphchi_vertex::id方法的典型用法代码示例。如果您正苦于以下问题:C++ graphchi_vertex::id方法的具体用法?C++ graphchi_vertex::id怎么用?C++ graphchi_vertex::id使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graphchi_vertex
的用法示例。
在下文中一共展示了graphchi_vertex::id方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: update
/**
* Vertex update function - computes the least square step
*/
void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) {
vertex_data & vdata = latent_factors_inmem[vertex.id()];
mat XtX = mat::Zero(D, D);
vec Xty = vec::Zero(D);
bool compute_rmse = (vertex.num_outedges() > 0);
// Compute XtX and Xty (NOTE: unweighted)
for(int e=0; e < vertex.num_edges(); e++) {
float observation = vertex.edge(e)->get_data();
vertex_data & nbr_latent = latent_factors_inmem[vertex.edge(e)->vertex_id()];
Xty += nbr_latent.pvec * observation;
XtX += nbr_latent.pvec * nbr_latent.pvec.transpose();
if (compute_rmse) {
double prediction;
rmse_vec[omp_get_thread_num()] += sparse_als_predict(vdata, nbr_latent, observation, prediction);
}
}
double regularization = lambda;
if (regnormal)
lambda *= vertex.num_edges();
for(int i=0; i < D; i++) XtX(i,i) += regularization;
bool isuser = vertex.id() < (uint)M;
if (algorithm == SPARSE_BOTH_FACTORS || (algorithm == SPARSE_USR_FACTOR && isuser) ||
(algorithm == SPARSE_ITM_FACTOR && !isuser)){
double sparsity_level = 1.0;
if (isuser)
sparsity_level -= user_sparsity;
else sparsity_level -= movie_sparsity;
vdata.pvec = CoSaMP(XtX, Xty, (int)ceil(sparsity_level*(double)D), 10, 1e-4, D);
}
else vdata.pvec = XtX.selfadjointView<Eigen::Upper>().ldlt().solve(Xty);
}
示例2: grab_adj
/**
* Grab pivot's adjacency list into memory.
*/
int grab_adj(graphchi_vertex<uint32_t, uint32_t> &v) {
if(is_pivot(v.id())) {
int ncount = v.num_edges();
// Count how many neighbors have larger id than v
v.sort_edges_indirect();
int actcount = 0;
vid_t lastvid = 0;
for(int i=0; i<ncount; i++) {
if (v.edge(i)->vertexid > v.id() && v.edge(i)->vertexid != lastvid)
actcount++; // Need to store only ids larger than me
lastvid = v.edge(i)->vertex_id();
}
// Allocate the in-memory adjacency list, using the
// knowledge of the number of edges.
dense_adj dadj = dense_adj(actcount, (vid_t*) calloc(sizeof(vid_t), actcount));
actcount = 0;
lastvid = 0;
for(int i=0; i<ncount; i++) {
if (v.edge(i)->vertexid > v.id() && v.edge(i)->vertexid != lastvid) { // Need to store only ids larger than me
dadj.adjlist[actcount++] = v.edge(i)->vertex_id();
}
lastvid = v.edge(i)->vertex_id();
}
assert(dadj.count == actcount);
adjs[v.id() - pivot_st] = dadj;
assert(v.id() - pivot_st < adjs.size());
__sync_add_and_fetch(&grabbed_edges, actcount);
return actcount;
}
return 0;
}
示例3: update
void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) {
if(gcontext.iteration == 0){
VertexDataType vertexdata = vertex.get_data();
if(!vertexdata.confirmed || !vertexdata.reconfirmed)
return ;
assert(vertex.num_inedges() * vertex.num_outedges() <= product);
for(int i=0; i<vertex.num_outedges(); i++){
bidirectional_label edgedata = vertex.outedge(i)->get_data();
if(edgedata.is_equal()){
/*
if(edgedata.smaller_one != 0)
std::cout<<edgedata.smaller_one<<" \t"<<edgedata.larger_one<<"\t root="<<root<<std::endl;
*/
if(root == edgedata.my_label(vertex.id(), vertex.outedge(i)->vertexid)){
lock.lock();
fprintf(fpout, "%u\t%u\n", vertex.id(), vertex.outedge(i)->vertexid);
lock.unlock();
continue;
}
}
/*
lock.lock();
fprintf(fpout1, "%u\t%u\n", vertex.id(), vertex.outedge(i)->vertexid);
lock.unlock();
*/
}
}
}
示例4: update
/**
* Vertex update function - computes the least square step
*/
void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) {
vertex_data & vdata = latent_factors_inmem[vertex.id()];
bool isuser = vertex.id() < M;
mat XtX = mat::Zero(D, D);
vec Xty = vec::Zero(D);
bool compute_rmse = (vertex.num_outedges() > 0);
// Compute XtX and Xty (NOTE: unweighted)
for(int e=0; e < vertex.num_edges(); e++) {
const edge_data & edge = vertex.edge(e)->get_data();
float observation = edge.weight;
vertex_data & nbr_latent = latent_factors_inmem[vertex.edge(e)->vertex_id()];
Xty += nbr_latent.pvec * observation;
XtX.triangularView<Eigen::Upper>() += nbr_latent.pvec * nbr_latent.pvec.transpose();
if (compute_rmse) {
double prediction;
rmse_vec[omp_get_thread_num()] += pmf_predict(vdata, nbr_latent, observation, prediction, (void*)&edge.avgprd);
vertex.edge(e)->set_data(edge);
}
}
double regularization = lambda;
if (regnormal)
lambda *= vertex.num_edges();
for(int i=0; i < D; i++) XtX(i,i) += regularization;
// Solve the least squares problem with eigen using Cholesky decomposition
mat iAi_;
bool ret =inv((isuser? A_U : A_V) + alpha * XtX, iAi_);
assert(ret);
vec mui_ = iAi_*((isuser? (A_U*mu_U) : (A_V*mu_V)) + alpha * Xty);
vdata.pvec = mvnrndex(mui_, iAi_, D, 0);
assert(vdata.pvec.size() == D);
}
示例5: update
/**
* Vertex update function.
*/
void update(graphchi_vertex<VertexDataType, EdgeDataType > &vertex, graphchi_context &gcontext) {
if (gcontext.iteration == 0) {
for(int i=0; i < vertex.num_outedges(); i++) {
chivector<vid_t> * evector = vertex.outedge(i)->get_vector();
evector->clear();
assert(evector->size() == 0);
evector->add(vertex.id());
assert(evector->size() == 1);
assert(evector->get(0) == vertex.id());
}
} else {
for(int i=0; i < vertex.num_inedges(); i++) {
graphchi_edge<EdgeDataType> * edge = vertex.inedge(i);
chivector<vid_t> * evector = edge->get_vector();
assert(evector->size() >= gcontext.iteration);
for(int j=0; j < evector->size(); j++) {
vid_t expected = edge->vertex_id() + j;
vid_t has = evector->get(j);
if (has != expected) {
std::cout << "Mismatch: " << has << " != " << expected << std::endl;
}
assert(has == expected);
}
}
for(int i=0; i < vertex.num_outedges(); i++) {
vertex.outedge(i)->get_vector()->add(vertex.id() + gcontext.iteration);
}
}
vertex.set_data(gcontext.iteration + 1);
}
示例6: update
/**
* Vertex update function.
* On first iteration ,each vertex chooses a label = the vertex id.
* On subsequent iterations, each vertex chooses the minimum of the neighbor's
* label (and itself).
*/
void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) {
/* On subsequent iterations, find the minimum label of my neighbors */
if (!edge_count){
vid_t curmin = vertex_values[vertex.id()];
if (gcontext.iteration == 0 && vertex.num_edges() > 0){
mymutex.lock(); actual_vertices++; mymutex.unlock();
}
for(int i=0; i < vertex.num_edges(); i++) {
vid_t nblabel = neighbor_value(vertex.edge(i));
curmin = std::min(nblabel, curmin);
}
if (vertex_values[vertex.id()] > curmin) {
changes++;
set_data(vertex, curmin);
}
}
else {
vid_t curmin = vertex_values[vertex.id()];
for(int i=0; i < vertex.num_edges(); i++) {
vid_t nblabel = neighbor_value(vertex.edge(i));
curmin = std::min(nblabel, curmin);
if (vertex.edge(i)->vertex_id() > vertex.id()){
mymutex.lock();
state[curmin]++;
mymutex.unlock();
}
}
}
}
示例7: update
/**
* Vertex update function.
*/
void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) {
//go over all samples (rows)
if ( vertex.num_outedges() > 0){
assert(vertex.id() < M);
vertex_data & row = latent_factors_inmem[vertex.id()];
assert(row.y == -1 || row.y == 1);
if (debug)
std::cout<<"Entered item " << vertex.id() << " y: " << row.y << std::endl;
row.sigma = beta*beta;
row.xT_mu = 0;
//go over all features
for(int e=0; e < vertex.num_outedges(); e++) {
uint feature_id = vertex.edge(e)->vertex_id();
edge_data edge = vertex.edge(e)->get_data();
assert(sigma_ij[feature_id] > 0);
assert(edge.x_ij == 1);
/* compute equation (6) */
row.sigma += edge.x_ij * sigma_ij[feature_id];
/* compute the sum xT*w as needed in equations (7) and (8) */
row.xT_mu += edge.x_ij * mu_ij[feature_id];
}
double prediction;
double ret = ctr_predict(row, row, row.y, prediction);
double predicted_target = prediction < 0 ? -1: 1;
if ((predicted_target == -1 && row.y == 1) || (predicted_target == 1 && row.y == -1))
err_vec[omp_get_thread_num()] += 1.0;
if (debug)
std::cout<<"Prediction was: " << prediction << " real value: " << row.y << std::endl;
liklihood_vec[omp_get_thread_num()] += ret;
assert(row.sigma > 0);
//go over all features
for(int e=0; e < vertex.num_outedges(); e++) {
edge_data edge = vertex.edge(e)->get_data();
uint feature_id = vertex.edge(e)->vertex_id();
assert(row.sigma > 0);
double product = row.y * row.xT_mu / sqrt(row.sigma);
mu_ij[feature_id] += (row.y * edge.x_ij * sigma_ij[feature_id] / sqrt(row.sigma)) * v(product);
//if (debug)
// std::cout<<"Added to edge: "<< vertex.edge(e)->vertex_id() << " product: " << product << " v(product): " << v(product) << " value: " <<(row.y * edge.x_ij * edge.sigma_ij * edge.sigma_ij / sqrt(row.sigma)) * v(product) << std::endl;
double factor = 1.0 - (edge.x_ij * sigma_ij[feature_id] / row.sigma)*w(product);
//if (debug)
// std::cout<<"Added to edge: "<< vertex.edge(e)->vertex_id() << " product: " << product << " w(product): " << w(product) << " factor: " << (1.0 - (edge.x_ij * edge.sigma_ij / row.sigma)*w(product)) << " sigma_ij " << edge.sigma_ij << " product: " << edge.sigma_ij * factor << std::endl;
assert(factor > 0);
sigma_ij[feature_id] *= factor;
assert(sigma_ij[feature_id] > 0);
}
}
}
示例8: update
/**
* Vertex update function - computes the least square step
*/
void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) {
if (vertex.id() >= M)
return;
vertex_data & vdata = latent_factors_inmem[vertex.id()];
int howmany = N*knn_sample_percent;
assert(howmany > 0 );
vec distances = vec::Zero(howmany);
ivec indices = ivec(howmany);
for (int i=0; i< howmany; i++){
indices[i]= -2;
}
std::vector<bool> curratings;
curratings.resize(N);
for(int e=0; e < vertex.num_edges(); e++) {
//no need to calculate this rating since it is given in the training data reference
curratings[vertex.edge(e)->vertex_id() - M] = true;
}
if (knn_sample_percent == 1.0){
for (uint i=M; i< M+N; i++){
if (curratings[i-M])
continue;
vertex_data & other = latent_factors_inmem[i];
double dist;
als_predict(vdata, other, 0, dist);
indices[i-M] = i-M;
distances[i-M] = dist;
}
}
else for (int i=0; i<howmany; i++){
int random_other = ::randi(M, M+N-1);
vertex_data & other = latent_factors_inmem[random_other];
double dist;
als_predict(vdata, other, 0, dist);
indices[i-M] = i-M;
distances[i-M] = dist;
}
vec out_dist(num_ratings);
ivec indices_sorted = reverse_sort_index2(distances, indices, out_dist, num_ratings);
assert(indices_sorted.size() <= num_ratings);
assert(out_dist.size() <= num_ratings);
vdata.ids = indices_sorted;
vdata.ratings = out_dist;
if (debug)
printf("Closest is: %d with distance %g\n", (int)vdata.ids[0], vdata.ratings[0]);
if (vertex.id() % 1000 == 0)
printf("Computing recommendaitons for user %d at time: %g\n", vertex.id()+1, mytimer.current_time());
}
示例9: update
/**
* Vertex update function.
*/
void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) {
if (vertex.get_data().confirmed) {
return;
}
VertexDataType vertexdata = vertex.get_data();
bool propagate = false;
if (gcontext.iteration == 0) {
/* "Leader" of the SCC */
if (vertexdata.color == vertex.id()) {
propagate = true;
vertex.remove_alloutedges();
}
} else {
/* Loop over in-edges and see if there is a match */
bool match = false;
for(int i=0; i < vertex.num_outedges(); i++) {
if (!vertex.outedge(i)->get_data().deleted()) {
if (vertex.outedge(i)->get_data().neighbor_label(vertex.id(), vertex.outedge(i)->vertexid) == vertexdata.color) {
match = true;
break;
}
}
}
if (match) {
propagate = true;
vertex.remove_alloutedges();
vertex.set_data(SCCinfo(vertexdata.color, true));
} else {
vertex.set_data(SCCinfo(vertex.id(), false));
}
}
if (propagate) {
for(int i=0; i < vertex.num_inedges(); i++) {
bidirectional_label edgedata = vertex.inedge(i)->get_data();
if (!edgedata.deleted()) {
edgedata.my_label(vertex.id(), vertex.inedge(i)->vertexid) = vertexdata.color;
vertex.inedge(i)->set_data(edgedata);
gcontext.scheduler->add_task(vertex.inedge(i)->vertexid, true);
}
}
}
}
示例10: update
/**
* Vertex update function.
*/
void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) {
if (vertex.id() < (uint)mi.start || vertex.id() >= (uint)mi.end)
return;
vertex_data& user = latent_factors_inmem[vertex.id()];
bool rows = vertex.id() < (uint)info.get_start_node(false);
if (info.is_square())
rows = mi.A_transpose;
(void) rows; // unused
assert(mi.r_offset >=0);
//store previous value for convergence detection
if (mi.prev_offset >= 0)
user.pvec[mi.prev_offset ] = user.pvec[mi.r_offset];
double val = 0;
assert(mi.x_offset >=0 || mi.y_offset>=0);
/*** COMPUTE r = c*A*x ********/
if (mi.A_offset && mi.x_offset >= 0){
for(int e=0; e < vertex.num_edges(); e++) {
const edge_data & edge = vertex.edge(e)->get_data();
const vertex_data & movie = latent_factors_inmem[vertex.edge(e)->vertex_id()];
val += (edge.weight * movie.pvec[mi.x_offset]);
}
if (info.is_square() && mi.use_diag)// add the diagonal term
val += (/*mi.c**/ (user.A_ii+ regularization) * user.pvec[mi.x_offset]);
val *= mi.c;
}
/***** COMPUTE r = c*I*x *****/
else if (!mi.A_offset && mi.x_offset >= 0){
val = mi.c*user.pvec[mi.x_offset];
}
/**** COMPUTE r+= d*y (optional) ***/
if (mi.y_offset>= 0){
val += mi.d*user.pvec[mi.y_offset];
}
/***** compute r = (... ) / div */
if (mi.div_offset >= 0){
val /= user.pvec[mi.div_offset];
}
assert(mi.r_offset>=0 && mi.r_offset < user.pvec.size());
user.pvec[mi.r_offset] = val;
} //end update
示例11: update
/**
* Vertex update function.
*/
void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) {
//go over all user nodes
if ( vertex.num_outedges() > 0){
vertex_data & user = latent_factors_inmem[vertex.id()];
//go over all ratings
for(int e=0; e < vertex.num_edges(); e++) {
float observation = vertex.edge(e)->get_data();
vertex_data & movie = latent_factors_inmem[vertex.edge(e)->vertex_id()];
double estScore;
rmse_vec[omp_get_thread_num()] += sgd_predict(user, movie, observation, estScore);
double err = observation - estScore;
if (std::isnan(err) || std::isinf(err))
logstream(LOG_FATAL)<<"SGD got into numerical error. Please tune step size using --sgd_gamma and sgd_lambda" << std::endl;
//NOTE: the following code is not thread safe, since potentially several
//user nodes may updates this item gradient vector concurrently. However in practice it
//did not matter in terms of accuracy on a multicore machine.
//if you like to defend the code, you can define a global variable
//mutex mymutex;
//
//and then do: mymutex.lock()
movie.pvec += sgd_gamma*(err*user.pvec - sgd_lambda*movie.pvec);
//and here add: mymutex.unlock();
user.pvec += sgd_gamma*(err*movie.pvec - sgd_lambda*user.pvec);
}
}
}
示例12: load_edges_into_memory
/**
* Grab pivot's adjacency list into memory.
*/
int load_edges_into_memory(graphchi_vertex<uint32_t, edge_data> &v) {
assert(is_pivot(v.id()));
assert(is_user(v.id()));
int num_edges = v.num_edges();
dense_adj dadj;
for(int i=0; i<num_edges; i++)
set_new( dadj.edges, v.edge(i)->vertex_id(), v.edge(i)->get_data().up_weight);
//dadj.ratings = zeros(N);
dadj.vid = v.id();
adjs[v.id() - pivot_st] = dadj;
assert(v.id() - pivot_st < adjs.size());
__sync_add_and_fetch(&grabbed_edges, num_edges /*edges_to_larger_id*/);
return num_edges;
}
示例13: update
/**
* compute validaton RMSE for a single user
*/
void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) {
if (user_nodes && vertex.id() >= M)
return;
else if (!user_nodes && vertex.id() < M)
return;
vertex_data & vdata = latent_factors_inmem[vertex.id()];
for(int e=0; e < vertex.num_outedges(); e++) {
const EdgeDataType & observation = vertex.edge(e)->get_data();
vertex_data & nbr_latent = latent_factors_inmem[vertex.edge(e)->vertex_id()];
double prediction;
double rmse = (*pprediction_func)(vdata, nbr_latent, observation, prediction, NULL);
// assert(rmse <= pow(maxval - minval, 2)); <ice>
assert(validation_rmse_vec.size() > omp_get_thread_num());
validation_rmse_vec[omp_get_thread_num()] += rmse;
}
}
示例14: update
/**
* Vertex update function - computes the least square step
*/
void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext) {
if (gcontext.iteration == 0){
if (vertex.num_outedges() == 0 && vertex.id() < M)
logstream(LOG_FATAL)<<"NMF algorithm can not work when the row " << vertex.id() << " of the matrix contains all zeros" << std::endl;
for(int e=0; e < vertex.num_edges(); e++) {
float observation = vertex.edge(e)->get_data();
if (observation < 0 ){
logstream(LOG_FATAL)<<"Found a negative entry in matirx row " << vertex.id() << " with value: " << observation << std::endl;
}
}
return;
}
bool isuser = (vertex.id() < M);
if ((iter % 2 == 1 && !isuser) ||
(iter % 2 == 0 && isuser))
return;
vec ret = zeros(D);
vertex_data & vdata = latent_factors_inmem[vertex.id()];
for(int e=0; e < vertex.num_edges(); e++) {
float observation = vertex.edge(e)->get_data();
vertex_data & nbr_latent = latent_factors_inmem[vertex.edge(e)->vertex_id()];
double prediction;
rmse_vec[omp_get_thread_num()] += nmf_predict(vdata, nbr_latent, observation, prediction);
if (prediction == 0)
logstream(LOG_FATAL)<<"Got into numerical error! Please submit a bug report." << std::endl;
ret += nbr_latent.pvec * (observation / prediction);
}
vec px;
if (isuser)
px = sum_of_item_latent_features;
else
px = sum_of_user_latent_feautres;
for (int i=0; i<D; i++){
assert(px[i] != 0);
vdata.pvec[i] *= ret[i] / px[i];
if (vdata.pvec[i] < epsilon)
vdata.pvec[i] = epsilon;
}
}
示例15: update
/**
* Vertex update function.
* On first iteration ,each vertex chooses a label = the vertex id.
* On subsequent iterations, each vertex chooses the minimum of the neighbor's
* label (and itself).
*/
void update(graphchi_vertex<VertexDataType, EdgeDataType> &vertex, graphchi_context &gcontext)
{
/* This program requires selective scheduling. */
assert(gcontext.scheduler != NULL);
if(gcontext.iteration == 0)
{
set_data(vertex, vertex.id());
/* Schedule neighbor for update */
gcontext.scheduler->add_task(vertex.id());
return;
}
else
{
vid_t curmin = vertex_values[vertex.id()];
for(int i=0; i < vertex.num_edges(); i++)
{
vid_t nblabel = neighbor_value(vertex.edge(i));
curmin = std::min(nblabel, curmin);
}
if ( curmin < vertex.get_data() )
{
for(int i=0; i < vertex.num_edges(); i++)
{
if (curmin < neighbor_value(vertex.edge(i)))
{
/* Schedule neighbor for update */
gcontext.scheduler->add_task(vertex.edge(i)->vertex_id());
}
}
set_data(vertex, curmin);
}
}
/* On subsequent iterations, find the minimum label of my neighbors */
/* If my label changes, schedule neighbors */
}