/** The gather function computes XtX and Xy */
 gather_type gather(icontext_type& context, const vertex_type& vertex, 
                    edge_type& edge) const {
   if(edge.data().role == edge_data::TRAIN) {
     const vertex_type other_vertex = get_other_vertex(edge, vertex);
     return gather_type(other_vertex.data().factor, edge.data().obs);
   } else return gather_type();
 } // end of gather function
Beispiel #2
0
 /**
  * \brief The scatter function just signal adjacent pages
  */
 void scatter(icontext_type& context, const vertex_type& vertex,
              edge_type& edge) const {
     const vertex_type other = get_other_vertex(edge, vertex);
     distance_type newd = vertex.data().dist + edge.data().dist;
     if (other.data().dist > newd) {
         const min_distance_type msg(newd);
         context.signal(other, msg);
     }
 } // end of scatter
Beispiel #3
0
 factor_type gather(icontext_type& context, const vertex_type& vertex, 
                    edge_type& edge) const {
   vertex_type other_vertex = get_other_vertex(edge, vertex);
   // VIOLATING THE ABSTRACTION!
   vertex_data& vdata = graph_type::vertex_type(vertex).data();
   // VIOLATING THE ABSTRACTION!
   vertex_data& other_vdata = other_vertex.data();
   factor_type& doc_topic_count = 
     is_doc(vertex) ? vdata.factor : other_vdata.factor;
   factor_type& word_topic_count = 
     is_word(vertex) ? vdata.factor : other_vdata.factor;
   ASSERT_EQ(doc_topic_count.size(), NTOPICS);
   ASSERT_EQ(word_topic_count.size(), NTOPICS);
   // run the actual gibbs sampling 
   factor_type& belief = edge.data().belief;
   const uint32_t count = edge.data().count;
   // Resample the topics
   double sum = 0, old_sum = 0;
   for(size_t t = 0; t < NTOPICS; ++t) {
     old_sum += belief[t];
     doc_topic_count[t] -= belief[t];
     word_topic_count[t] -= belief[t];
     GLOBAL_TOPIC_COUNT[t] -= belief[t];
     const double n_dt = 
       std::max(count_type(doc_topic_count[t]), count_type(0));
     ASSERT_GE(n_dt, 0);
     const double n_wt = 
       std::max(count_type(word_topic_count[t]), count_type(0)); 
     ASSERT_GE(n_wt, 0);
     const double n_t  = 
       std::max(count_type(GLOBAL_TOPIC_COUNT[t]), count_type(0)); 
     ASSERT_GE(n_t, 0);
     belief[t] = (ALPHA + n_dt) * (BETA + n_wt) / (BETA * NWORDS + n_t);
     sum += belief[t];
   } // End of loop over each token
   ASSERT_GT(sum, 0);
   if(old_sum == 0) {
     size_t asg = graphlab::random::multinomial(belief);
     for(size_t i = 0; i < NTOPICS; ++i) belief[i] = 0;
     belief[asg] = count;
     return belief;
   }
   for(size_t t = 0; t < NTOPICS; ++t) {
     belief[t] = count * (belief[t]/sum);
     doc_topic_count[t] += belief[t];
     word_topic_count[t] += belief[t];
     GLOBAL_TOPIC_COUNT[t] += belief[t];
   }
   return belief;
 } // end of gather
Beispiel #4
0
/*
* \brief Returns the sparse vector containing all the items rated by the user vertex.
*/
rated_type map_get_topk(graph_type::edge_type edge, graph_type::vertex_type other)
{
	// return the list of items rated by the user
	const rated_type& similar_items = other.data().rated_items;

	// To hold the similarity score
	rated_type similarity_score;

	// Get the id of the current item
	id_type current_id = get_other_vertex(edge, other).id();

	// Go through all the items rated by the other user, except for current item
	for(rated_type::const_iterator cit = similar_items.begin(); cit != similar_items.end(); cit++)
		if(cit->first != current_id)
		{
			// Get the score and add only if score is valid
			// Score is invalid if common users are less than MIN_ALLOWED_INTERSECTION			
			double score = adj_cosine_similarity(item_vector[cit->first], item_vector[current_id]);
			if(score != INVALID_SIMILARITY)
				similarity_score[cit->first] = score; 
		}

	return similarity_score;
}
Beispiel #5
0
 void scatter(icontext_type& context, const vertex_type& vertex, 
              edge_type& edge) const {
   const vertex_type other_vertex = get_other_vertex(edge, vertex);
   context.signal(other_vertex);
 } // end of scatter function