void loglike (int N, int W, int D, int T, double alpha, double beta, int *w, int *d, int **Nwt, int **Ndt, int *Nt, int *Nd) // { int i, j, t; double llike; static int init = 0; static double **prob_w_given_t; static double **prob_t_given_d; static double *Nd_; double Nt_; if (init==0) { init = 1; prob_w_given_t = dmat(W,T); prob_t_given_d = dmat(D,T); Nd_ = dvec(D); for (j = 0; j < D; j++) Nd_[j] = Nd[j] + T*alpha; } for (t = 0; t < T; t++) { Nt_ = Nt[t] + W*beta; for (i = 0; i < W; i++) prob_w_given_t[i][t] = (Nwt[i][t]+beta) / Nt_; for (j = 0; j < D; j++) prob_t_given_d[j][t] = (Ndt[j][t]+alpha)/ Nd_[j]; } llike = 0; for (i = 0; i < N; i++) llike += log(ddot(T, prob_w_given_t[w[i]], prob_t_given_d[d[i]])); printf(">>> llike = %.6e ", llike); printf("pplex = %.4f\n", exp(-llike/N)); }
void sample_chain_2layer (int N, int W, int T, int S, double alpha, double beta, double gamma, int *w, int *d, int *z, int *y, int **Nwt, int **zy, int **Ndt, int *Nt, int *ytot) // { int i, t, s; double totprob, U, cumprob, term1, term2, term3; double wbeta = W*beta; double tgamma = T*gamma; double **prob = dmat(T,S); for (i = 0; i < N; i++) { t = z[i]; s = y[i]; Nt[t]--; ytot[s]--; Nwt[w[i]][t]--; zy[t][s]--; Ndt[d[i]][s]--; totprob = 0; for (t = 0; t < T; t++) { for (s = 0; s < S; s++) { term1 = (Nwt[w[i]][t] + beta) / (Nt[t] + wbeta); term2 = (zy[t][s] + gamma) / (ytot[s] + tgamma); term3 = (Ndt[d[i]][s] + alpha); prob[t][s] = term1*term2*term3; totprob += prob[t][s]; } } U = drand48()*totprob; cumprob = prob[0][0]; t = 0; s = 0; while (U>cumprob) { t++; if (t >= T) { s++; t=0; } cumprob += prob[t][s]; } z[i] = t; y[i] = s; Nt[t]++; ytot[s]++; Nwt[w[i]][t]++; zy[t][s]++; Ndt[d[s]][t]++; } free_dmat(prob); }
RcppExport SEXP rthxpos(SEXP m) { Rcpp::NumericMatrix tmpm = Rcpp::NumericMatrix(m); int nr = tmpm.nrow(); int nc = tmpm.ncol(); thrust::device_vector<double> dmat(tmpm.begin(),tmpm.end()); // make space for the transpose thrust::device_vector<double> dxp(nr*nc); // iterator to march through the matrix elements thrust::counting_iterator<int> seqb(0); thrust::counting_iterator<int> seqe = seqb + nr*nc; // for each i in seq, copy the matrix elt to its spot in the // transpose thrust::for_each(seqb,seqe, copyelt2xp(dmat.begin(),dxp.begin(),nr,nc)); // prepare the R output, and return it Rcpp::NumericVector routmat(nc*nr); thrust::copy(dxp.begin(),dxp.end(),routmat.begin()); return routmat; }
unsigned int Compute2DCoordsMimicDistmat( RDKit::ROMol &mol, python::object distMat, bool canonOrient, bool clearConfs, double weightDistMat, unsigned int nFlipsPerSample, unsigned int nSamples, int sampleSeed, bool permuteDeg4Nodes, double bondLength = -1.0) { PyObject *distMatPtr = distMat.ptr(); if (!PyArray_Check(distMatPtr)) { throw_value_error("Argument isn't an array"); } PyArrayObject *dmatrix = reinterpret_cast<PyArrayObject *>(distMatPtr); unsigned int nitems = PyArray_DIM(dmatrix, 0); unsigned int na = mol.getNumAtoms(); if (nitems != na * (na - 1) / 2) { throw_value_error( "The array size does not match the number of atoms in the molecule"); } double *inData = reinterpret_cast<double *>(PyArray_DATA(dmatrix)); double *cData = new double[nitems]; memcpy(static_cast<void *>(cData), static_cast<const void *>(inData), nitems * sizeof(double)); DOUBLE_SMART_PTR dmat(cData); double oBondLen = RDDepict::BOND_LEN; if (bondLength > 0) { RDDepict::BOND_LEN = bondLength; } unsigned int res; res = RDDepict::compute2DCoordsMimicDistMat( mol, &dmat, canonOrient, clearConfs, weightDistMat, nFlipsPerSample, nSamples, sampleSeed, permuteDeg4Nodes); if (bondLength > 0) { RDDepict::BOND_LEN = oBondLen; } return res; }
void StereoProcessor::processDisparity(const cv::Mat& left_rect, const cv::Mat& right_rect, const image_geometry::StereoCameraModel& model, stereo_msgs::DisparityImage& disparity) const { // Fixed-point disparity is 16 times the true value: d = d_fp / 16.0 = x_l - x_r. static const int DPP = 16; // disparities per pixel static const double inv_dpp = 1.0 / DPP; // Block matcher produces 16-bit signed (fixed point) disparity image block_matcher_(left_rect, right_rect, disparity16_); // Fill in DisparityImage image data, converting to 32-bit float sensor_msgs::Image& dimage = disparity.image; dimage.height = disparity16_.rows; dimage.width = disparity16_.cols; dimage.encoding = sensor_msgs::image_encodings::TYPE_32FC1; dimage.step = dimage.width * sizeof(float); dimage.data.resize(dimage.step * dimage.height); cv::Mat_<float> dmat(dimage.height, dimage.width, (float*)&dimage.data[0], dimage.step); // We convert from fixed-point to float disparity and also adjust for any x-offset between // the principal points: d = d_fp*inv_dpp - (cx_l - cx_r) disparity16_.convertTo(dmat, dmat.type(), inv_dpp, -(model.left().cx() - model.right().cx())); ROS_ASSERT(dmat.data == &dimage.data[0]); /// @todo is_bigendian? :) // Stereo parameters disparity.f = model.right().fx(); disparity.T = model.baseline(); /// @todo Window of (potentially) valid disparities // Disparity search range disparity.min_disparity = getMinDisparity(); disparity.max_disparity = getMinDisparity() + getDisparityRange() - 1; disparity.delta_d = inv_dpp; }
/* * print out the topic topk=10 words. report the PMI score. */ double report_pmi(char *topfile, /* name of topics file */ char *pmifile, /* name of PMI file */ int T, /* total topics */ int W, /* total words */ int E, /* number of epochs */ int topk, double *tp) { int lineno = 0; int i,k, thee; /* * mapping from local index to actual word index */ uint32_t *wind = u32vec(topk*T*E); int n_wind = 0; /* * boolean vector ... is word used */ uint32_t *wuse = u32vec(W/32+1); /* * PMI's by local index */ uint32_t *topic = u32vec(topk); float *coherency = fvec(E); double **pmi; float ave = 0; char *line; size_t n_line; FILE *fr; if ( !wind || !wuse ) yap_quit("Out of memory in report_pmi()\n"); /* * read in file of top word indices in topic */ fr = fopen(topfile,"r"); if ( !fr ) yap_sysquit("Topic file '%s' not read\n", topfile); line = NULL; n_line = 0; lineno = 0; while ( getline(&line, &n_line, fr)>0 ) { char *buf = line; unsigned j; int e = 0; lineno ++; buf += strspn(buf," \t\n"); // skip space if ( (E==1 && sscanf(buf, "%d: ", &k)<1) || (E>1 && sscanf(buf, "%d,%d: ", &e, &k)<2) ) yap_quit("Cannot read topic in topic line %d from file '%s'\n", lineno, topfile); if ( k<0 || k>=T ) continue; if ( e<0 || e>=E ) continue; for (i = 0; i<topk && *buf; i++) { buf = strpbrk(buf," \t\n"); // skip to next space if ( sscanf(buf, " %u", &j) <1 ) { if ( verbose>2 ) yap_message("Cannot read word %d in topic line %d from file '%s'\n", i+1, lineno, topfile); break; } if ( j>=W) { yap_quit("Bad word %d in topic line %d from file '%s'\n", i+1, lineno, topfile); } buf += strspn(buf," \t\n"); // skip space /* * check if word exists, and set up its index */ if ( wuse[j/32U] & (1U<<(j%32U)) ) { // yes, so search for it int ii; for (ii=0; ii<n_wind; ii++) if ( wind[ii]==j ) break; if ( ii>=n_wind ) yap_quit("Lookup of word %d failed at line %d in report_pmi()\n", (int)j, lineno); } else { // no, so add it wuse[j/32U] |= (1U<<(j%32U)); wind[n_wind] = j; n_wind++; } } free(line); line = NULL; n_line = 0; } fclose(fr); pmi = dmat(n_wind,n_wind); /* * build hash table now since we know size */ hashsize = n_wind*2; hashtab = malloc(sizeof(*hashtab)*hashsize); if ( !pmi || !hashtab ) yap_quit("Out of memory in report_pmi()\n"); for (i=0; i<hashsize; i++) hashtab[i] = 0; for (i=0; i<n_wind; i++) addw(wind[i],i); /* * load up PMI file, only keeping words mentioned in hash table */ { unsigned t1, t2; double value; int zcat = 0; fr = fopen(pmifile,"r"); if ( !fr ) { /* * try to zcat it */ char *cmd = malloc(strlen(pmifile)+20); sprintf(cmd,"%s.gz", pmifile); fr = fopen(cmd,"r"); if ( !fr ) yap_sysquit("Cannot open pmifile '%s' in report_pmi()\n", pmifile); fclose(fr); sprintf(cmd,"gunzip -c %s", pmifile); fr = popen(cmd,"r"); if ( !fr ) yap_sysquit("Cannot open or zcat pmifile '%s' in report_pmi()\n", pmifile); zcat = 1; free(cmd); } while (fscanf(fr, "%u %u %lg", &t1, &t2, &value)==3 ) { if ( t1>=W || t2>= W ) yap_quit("Illegal word index in report_pmi()\n"); if ( t1!= t2 && ( wuse[t1/32U] & (1U<<(t1%32U)) ) && ( wuse[t2/32U] & (1U<<(t2%32U))) ) { int i1, i2; i1 = findw(t1,wind); i2 = findw(t2,wind); if ( i1==UINT32_MAX || i2==UINT32_MAX ) yap_quit("Could not locate word index in report_pmi()\n"); pmi[i1][i2]=value; pmi[i2][i1]=value; } } if ( zcat ) pclose(fr); else fclose(fr); } /* * compute PMI score for each topic */ fr = fopen(topfile,"r"); if ( !fr ) yap_sysquit("Topic file '%s' not read\n", topfile); line = NULL; n_line = 0; thee = 0; lineno = 0; if ( E>1 ) yap_message("PMI %d:: ", 0); else yap_message("PMI :: "); while ( getline(&line, &n_line, fr)>0 ) { /* * repeat logic above to read topic file again */ char *buf = line; unsigned j; int cnt = 0; int e = 0; double coh = 0; buf += strspn(buf," \t\n"); // skip space if ( (E==1 && sscanf(buf, "%d: ", &k)<1) || (E>1 && sscanf(buf, "%d,%d: ", &e, &k)<2) ) yap_quit("Cannot read topic in topic line %d from file '%s'\n", lineno, topfile); if ( k<0 || k>=T ) continue; if ( e<0 || e>=E ) continue; if ( e!=thee ) { thee = e; yap_message("\nPMI %d:: ", e); } for (i = 0; i<topk && *buf; i++) { buf = strpbrk(buf," \t\n"); // skip to next space if ( sscanf(buf, " %u", &j) <1 ) { yap_message("Cannot read word %d in topic line %d from file '%s'\n", i+1, lineno, topfile); break; } if ( j>=W) { yap_quit("Bad word %d in topic line %d from file '%s'\n", i+1, lineno, topfile); } buf += strspn(buf," \t\n"); // skip space topic[i] = findw(j,wind); } if ( i<topk ) topic[i] = W; /* * topics now read */ for (i=0; i<topk && topic[i]<W; i++) { for (j=i+1; j<topk && topic[j]<W; j++) { coh += pmi[topic[i]][topic[j]]; cnt ++; } } if ( cnt>0 ) coh /= cnt; coherency[e] += coh * tp[k]; yap_message(" %d:%.3lf", k, coh); } fclose(fr); yap_message("\nPMI ="); if ( E==1 ) { yap_message(" %.3lf\n", coherency[0]); ave = coherency[0]; } else { int e; for (e=0; e<E; e++) { ave += coherency[e]; yap_message(" %.3lf", coherency[e]); } ave /= E; yap_message(" -> %.3lf\n", ave); } free(wind); free(coherency); free(wuse); free(topic); free(pmi[0]); free(pmi); free(hashtab); hashtab = NULL; hashsize = 0; return ave; }
/*========================================== * main *========================================== */ int main(int argc, char* argv[]) { int T; // number of topics int W; // number of unique words int D; // number of docs int N; // number of words in corpus int i, iter, seed; int *w, *d, *z, *order; double **Nwt, **Ndt, *Nt; double alpha, beta; if (argc == 1) { fprintf(stderr, "usage: %s T iter seed\n", argv[0]); exit(-1); } T = atoi(argv[1]); assert(T>0); iter = atoi(argv[2]); assert(iter>0); seed = atoi(argv[3]); assert(seed>0); N = countN("docword.txt"); w = ivec(N); d = ivec(N); z = ivec(N); read_dw("docword.txt", d, w, &D, &W); Nwt = dmat(W,T); Ndt = dmat(D,T); Nt = dvec(T); alpha = 0.05 * N / (D * T); beta = 0.01; printf("seed = %d\n", seed); printf("N = %d\n", N); printf("W = %d\n", W); printf("D = %d\n", D); printf("T = %d\n", T); printf("iter = %d\n", iter); printf("alpha = %f\n", alpha); printf("beta = %f\n", beta); srand48(seed); randomassignment_d(N,T,w,d,z,Nwt,Ndt,Nt); order = randperm(N); add_smooth_d(D,T,Ndt,alpha); add_smooth_d(W,T,Nwt,beta); add_smooth1d( T,Nt, W*beta); for (i = 0; i < iter; i++) { sample_chain_d(N,W,T,w,d,z,Nwt,Ndt,Nt,order); printf("iter %d \n", i); } printf("In-Sample Perplexity = %.2f\n",pplex_d(N,W,T,w,d,Nwt,Ndt)); add_smooth_d(D,T,Ndt,-alpha); add_smooth_d(W,T,Nwt,-beta); add_smooth1d( T,Nt, -W*beta); write_sparse_d(W,T,Nwt,"Nwt.txt"); write_sparse_d(D,T,Ndt,"Ndt.txt"); write_ivec(N,z,"z.txt"); return 0; }
CPM_BENCH() { CPM_TWO_PASS_NS_P( pmp_policy, "pmp_h(c=2) (s) [pmp][s]", [](size_t d){ return std::make_tuple(smat(d, d), smat(d,d)); }, [](smat& a, smat& r){ r = etl::p_max_pool_h<2,2>(a); }, [](size_t d){ return 2 * d * d * 2 * 2; } ); #ifdef ETL_EXTENDED_BENCH CPM_TWO_PASS_NS_P( pmp_policy_3, "pmp_h_3(c=2) (s) [pmp][s]", [](size_t d){ return std::make_tuple(smat3(50UL, d, d), smat3(50UL, d,d)); }, [](smat3& a, smat3& r){ r = etl::p_max_pool_h<2,2>(a); }, [](size_t d){ return 50 * 2 * d * d * 2 * 2; } ); CPM_TWO_PASS_NS_P( pmp_policy_3, "pmp_h_4(c=2) (s) [pmp][s]", [](size_t d){ return std::make_tuple(smat4(50UL, 50UL, d, d), smat4(50UL, 50UL, d,d)); }, [](smat4& a, smat4& r){ r = etl::p_max_pool_h<2,2>(a); }, [](size_t d){ return 50 * 50 * 2 * d * d * 2 * 2; } ); CPM_TWO_PASS_NS_P( pmp_policy, "dyn_pmp_h(c=2) (s) [pmp][s]", [](size_t d){ return std::make_tuple(smat(d, d), smat(d, d)); }, [](smat& a, smat& r){ r = etl::p_max_pool_h(a, 2, 2); }, [](size_t d){ return 2 * d * d * 2 * 2; } ); #endif CPM_TWO_PASS_NS_P( pmp_policy, "pmp_p(c=2) (s) [pmp][s]", [](size_t d){ return std::make_tuple(smat(d, d), smat(d/2,d/2)); }, [](smat& a, smat& r){ r = etl::p_max_pool_p<2,2>(a); }, [](size_t d){ return 2 * d * d * 2 * 2; } ); CPM_TWO_PASS_NS_P( pmp_policy, "pmp_h(c=4) (s) [pmp][s]", [](size_t d){ return std::make_tuple(smat(d, d), smat(d,d)); }, [](smat& a, smat& r){ r = etl::p_max_pool_h<4,4>(a); }, [](size_t d){ return 2 * d * d * 4 * 4; } ); CPM_TWO_PASS_NS_P( pmp_policy, "pmp_p(c=4) (s) [pmp][s]", [](size_t d){ return std::make_tuple(smat(d, d), smat(d/4,d/4)); }, [](smat& a, smat& r){ r = etl::p_max_pool_p<4,4>(a); }, [](size_t d){ return 2 * d * d * 4 * 4; } ); CPM_TWO_PASS_NS_P( pmp_policy, "pmp_h(c=2) (d) [pmp][s]", [](size_t d){ return std::make_tuple(dmat(d, d), dmat(d,d)); }, [](dmat& a, dmat& r){ r = etl::p_max_pool_h<2,2>(a); }, [](size_t d){ return 2 * d * d * 2 * 2; } ); CPM_TWO_PASS_NS_P( pmp_policy, "pmp_p(c=2) (d) [pmp][s]", [](size_t d){ return std::make_tuple(dmat(d, d), dmat(d/2,d/2)); }, [](dmat& a, dmat& r){ r = etl::p_max_pool_p<2,2>(a); }, [](size_t d){ return 2 * d * d * 2 * 2; } ); CPM_TWO_PASS_NS_P( pmp_policy, "pmp_h(c=4) (d) [pmp][s]", [](size_t d){ return std::make_tuple(dmat(d, d), dmat(d,d)); }, [](dmat& a, dmat& r){ r = etl::p_max_pool_h<4,4>(a); }, [](size_t d){ return 2 * d * d * 4 * 4; } ); CPM_TWO_PASS_NS_P( pmp_policy, "pmp_p(c=4) (d) [pmp][s]", [](size_t d){ return std::make_tuple(dmat(d, d), dmat(d/4,d/4)); }, [](dmat& a, dmat& r){ r = etl::p_max_pool_p<4,4>(a); }, [](size_t d){ return 2 * d * d * 4 * 4; } ); }