/** 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
Esempio n. 2
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
Esempio n. 3
0
	gather_type gather(icontext_type& context, const vertex_type& vertex, edge_type& edge) const
	{
		cout << "gather(), edge=" << edge.source().id() << "->" << edge.target().id() << ", called from vid=" << vertex.id() << endl;
		
		gather_type gathered;
		
		// add id of other vertex of edge and add id->beta to map if message source
		if (edge.target().id()==vertex.id())
		{
			// incoming edge, outgoing message, only if target is non-observed
			if (!edge.source().data().is_observed)
			{
				gathered.message_targets.insert(edge.source().id());
				cout << "added " << edge.source().id() << " as message target" << endl;
			}
		}
		else
		{
			// outgoing edge, incoming message with beta
			gathered.message_source_betas[edge.target().id()]=edge.data().beta;
			cout << "added " << edge.target().id() << " as message source" << endl;	
		}
					
		cout << "gathered=" << gathered << endl;
		
		return gathered;
	}
Esempio n. 4
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
Esempio n. 5
0
    void scatter(icontext_type& context, const vertex_type& vertex,
                 edge_type& edge) const
    {
        const vertex_type other = edge.target();
        distance_type newd = vertex.data().dist + edge.data().dist;

        const min_distance_type msg(newd);
        context.signal(other, msg);
    }
Esempio n. 6
0
    /* Gather the weighted rank of the adjacent page   */
    double gather(icontext_type& context, const vertex_type& vertex,
        edge_type& edge) const {

      if (edge.data().role == edge_data::PREDICT)
         return 0;

      bool brows = vertex.id() < (uint)info.get_start_node(false);
      if (info.is_square()) 
        brows = !mi.A_transpose;
      if (mi.A_offset  && mi.x_offset >= 0){
        double val = edge.data().obs * (brows ? edge.target().data().pvec[mi.x_offset] :
            edge.source().data().pvec[mi.x_offset]);
        //printf("gather edge on vertex %d val %lg obs %lg\n", vertex.id(), val, edge.data().obs);
        return val;
      }
      //printf("edge on vertex %d val %lg\n", vertex.id(), 0.0);
      return 0;
    }
Esempio n. 7
0
	void scatter(icontext_type& context, const vertex_type& vertex, edge_type& edge) const {

		EData ed = edge.data();
		float weight = ed.weight;

		if (  !edge.source().data().actived && ((double)rand() / RAND_MAX) < weight){
//		if (  !edge.source().data().actived && 0.5 <= weight){

			context.signal(edge.source());
		}
	}
Esempio n. 8
0
 void scatter(icontext_type& context, const vertex_type& vertex, edge_type& edge) const {
     float weight = edge.data();
     if ( ((double)rand() / RAND_MAX) < weight){
         context.signalVid(edge.target().id());
     }
 }
Esempio n. 9
0
	void scatter(icontext_type& context, const vertex_type& vertex, edge_type& edge) const
	{
		if (vertex.data() + edge.data() < edge.target().data())
			context.signal(edge.target());
	}
Esempio n. 10
0
	gather_type gather(icontext_type& context, const vertex_type& vertex, edge_type& edge) const
	{
		float newval = edge.data() + edge.source().data();
		return gather_type(newval);
	}
Esempio n. 11
0
	void scatter(icontext_type& context, const vertex_type& vertex, edge_type& edge) const
	{
		cout << "scatter(), edge=" << edge.source().id() << "->" << edge.target().id() << ", called from vid=" << vertex.id() << endl;
		cout << "computing message from vid=" << vertex.id() << " to vid=" << edge.source().id() << endl;

		vertex_id_type message_target=edge.source().id();
		
		// find out whether full rank or incomplete Cholesky mode

		// distinguish case this node being observed or not
		VectorXd new_beta;
		if (edge.target().data().is_observed)
		{
			cout << "observed target" << endl;
			
			// extract system solutions and observation kernel vector, base on full rank or incomplete Cholesky
			if (edge.data().full_rank)
			{
				cout << "full rank case" << endl;
				
				MatrixXd L_s=edge.data().solution_matrices["L_s"];
				cout << "L_s:" << L_s << endl;
				MatrixXd L_t=edge.data().solution_matrices["L_t"];
				cout << "L_t:" << L_t << endl;
				VectorXd k=vertex.data().kernel_dict_obs.at(message_target);
				cout << "k:" << k << endl;
			
				// L_{s}^{-T}(L_{s}^{-1}(L_{t}^{-T}(L_{t}^{-1}k_{t}^{s}), from right to left, 4 solver calls
				new_beta=k;
				new_beta=L_t.triangularView<Lower>().solve(new_beta);
				new_beta=L_t.transpose().triangularView<Upper>().solve(new_beta);
				new_beta=L_s.triangularView<Lower>().solve(new_beta);
				new_beta=L_s.transpose().triangularView<Upper>().solve(new_beta);
			}
			else
			{
				cout << "incomplete Cholesky case" << endl;
				MatrixXd Q_s=edge.data().solution_matrices["Q_s"];
				cout << "Q_s:" << Q_s << endl;
				MatrixXd R_s=edge.data().solution_matrices["R_s"];
				cout << "R_s:" << R_s << endl;
				MatrixXd P_s=edge.data().solution_matrices["P_s"];
				cout << "P_s:" << P_s << endl;
				
				MatrixXd Q_t=edge.data().solution_matrices["Q_t"];
				cout << "Q_t:" << Q_t << endl;
				MatrixXd R_t=edge.data().solution_matrices["R_t"];
				cout << "R_t:" << R_t << endl;
				MatrixXd P_t=edge.data().solution_matrices["P_t"];
				cout << "P_t:" << P_t << endl;
				
				MatrixXd W=edge.data().solution_matrices["W"];
				cout << "W:" << W << endl;
				
				VectorXd k=vertex.data().kernel_dict_obs.at(message_target);
				cout << "k:" << k << endl;
				
				// R_{s}^{-1}(Q_{s}^{T}((P_{s}(W_{s}W_{t}^{T}))(R_{t}^{-1}(Q_{t}^{T}(P_{t}k_{\mathcal{I}_{t}}^{(s)})))
				new_beta=k;
				new_beta=P_t.transpose()*new_beta;
				new_beta=Q_t.transpose()*new_beta;
				new_beta=R_t.triangularView<Upper>().solve(new_beta);
				new_beta=W*new_beta;
				new_beta=P_s.transpose()*new_beta;
				new_beta=Q_s.transpose()*new_beta;
				new_beta=R_s.triangularView<Upper>().solve(new_beta);
			}
		}
		else
		{
			cout << "non-observed target" << endl;
			cout << "multiplied_incoming_messages: " << vertex.data().multiplied_incoming_messages << endl;
			
			// extract system solutions, depending on full rank or incomplete Cholesky
			if (edge.data().full_rank)
			{
				cout << "full rank case" << endl;
				MatrixXd L_s=edge.data().solution_matrices["L_s"];
				cout << "L_s:" << L_s << endl;
		
				VectorXd k;
				if (!vertex.data().multiplied_incoming_messages.size())
				{
					cout << "no incoming messages, using constant unit norm vector" << endl;
					k=VectorXd::Constant(L_s.cols(), 1.0/sqrt(L_s.cols()));
				}
				else
				{
					k=vertex.data().multiplied_incoming_messages.at(message_target);
				}
				cout << "k:" << k << endl;
				
				// (K_{s}+\lambda I){}^{-1}k_{ut}^{(s)}=L_{s}^{-T}(L_{s}^{-1}k_{ut}^{(s)}) from right to left, 2 solver calls
				new_beta=k;
				new_beta=L_s.triangularView<Lower>().solve(new_beta);
				new_beta=L_s.transpose().triangularView<Upper>().solve(new_beta);
			}
			else
			{
				cout << "incomplete Cholesky case" << endl;
				
				MatrixXd Q_s=edge.data().solution_matrices["Q_s"];
				cout << "Q_s:" << Q_s << endl;
				MatrixXd R_s=edge.data().solution_matrices["R_s"];
				cout << "R_s:" << R_s << endl;
				MatrixXd P_s=edge.data().solution_matrices["P_s"];
				cout << "P_s:" << P_s << endl;
				
				MatrixXd W=edge.data().solution_matrices["W"];
				cout << "W:" << W << endl;
				
				VectorXd k;
				if (!vertex.data().multiplied_incoming_messages.size())
				{
					cout << "no incoming messages, using constant unit norm vector" << endl;
					k=VectorXd::Constant(W.cols(), 1.0/sqrt(W.cols()));
				}
				else
				{
					k=vertex.data().multiplied_incoming_messages.at(message_target);
				}
				cout << "k:" << k << endl;
				
				// R_{s}^{-1}(Q_{s}^{T}(P_{s}^{T}k_{t}^{(s)}))
				new_beta=k;
				new_beta=W*new_beta;
				new_beta=P_s.transpose()*new_beta;
				new_beta=Q_s.transpose()*new_beta;
				new_beta=R_s.triangularView<Upper>().solve(new_beta);
			}
		}

		// normalise
		new_beta=new_beta/new_beta.norm();
		
		// check whether has changed or not yet existed
		double difference;
		if (!edge.data().beta.rows())
			difference=numeric_limits<double>::infinity();
		else
			difference=(new_beta-edge.data().beta).norm();

		cout << "beta norm difference is " << difference << endl;
		if (difference>BETA_EPSILON)
		{
			// store new message and signal depending node if beta has changed or has not yet existed
			edge.data().beta=new_beta;
			context.signal(edge.source());
			cout << "beta has changed, new_beta=" << new_beta << "\nhas norm=" << new_beta.norm() << ", signalling vid=" << edge.source().id() << endl;
		}
		else
		{
			cout << "converged!\n";
		}
		
		cout << "beta: " << edge.source().id() << "->" << edge.target().id() << ": " << edge.data().beta << endl;
	}