/** * Returns a random subsample of X of size n which has minimum (amongst * a set of NSAMPLES similar subsamples) maximum distance between points. */ ivec MaxMinDesign::subsample(mat X, int n) { if (n <= 0) cerr << "Invalid sample size in MaxMinDesign::subsample(...)" << endl; double maxMinDist = 0.0; ivec bestSubsample = randperm(X.rows()); for (int i=0; i<nsamples; i++) { // Generate random subsample ivec irand = randperm(X.rows()); ivec irandn = irand(0,n-1); mat S = X.get_rows(irandn); // Compute max distance in this subsample double minDist = distanceMin(S); // Keep subsample if has min max distance if (minDist > maxMinDist) { bestSubsample = irandn; maxMinDist = minDist; cout << "New min distance: " << maxMinDist << endl; } } return bestSubsample; }
/** * @brief Obtain the initial dictionary, which * its columns are normalized * * @param dictionary : will contain random patches from patches, * with its columns normalized; * @param patches : contains all patches in the noisy image. * * @return none. **/ void obtain_dict(matD_t &dictionary, matD_t const& patches) { //! Declarations vecU_t perm(patches.size()); //! Obtain random indices randperm(perm); //! Getting the initial random dictionary from patches iterU_t it_p = perm.begin(); for (matD_t::iterator it_d = dictionary.begin(); it_d < dictionary.end(); it_d++, it_p++) (*it_d) = patches[*it_p]; //! Normalize column double norm; for (matD_t::iterator it = dictionary.begin(); it < dictionary.end(); it++) { norm = 0.0l; for (iterD_t it_d = (*it).begin(); it_d < (*it).end(); it_d++) norm += (*it_d) * (*it_d); norm = 1 / sqrtl(norm); for (iterD_t it_d = (*it).begin(); it_d < (*it).end(); it_d++) (*it_d) *= norm; } }
/* * Generates a random directed weighted graph with N nodes and K edges. Weights * are chosen uniformly between wmin and wmax. No edges are placed on the main * diagonal. */ MATRIX_T* BCT_NAMESPACE::makerandCIJ_wd(int N, int K, FP_T wmin, FP_T wmax) { gsl_rng* rng = get_rng(); VECTOR_T* w = VECTOR_ID(alloc)(K); for (int i = 0; i < K; i++) { VECTOR_ID(set)(w, i, gsl_rng_uniform(rng) * (wmax - wmin) + wmin); } // ind = ~eye(N); MATRIX_T* eye_N = eye(N); MATRIX_T* ind = logical_not(eye_N); MATRIX_ID(free)(eye_N); // i = find(ind); VECTOR_T* i = find(ind); MATRIX_ID(free)(ind); // rp = randperm(length(i)); gsl_permutation* rp = randperm(length(i)); // irp = i(rp); VECTOR_T* irp = permute(rp, i); gsl_permutation_free(rp); VECTOR_ID(free)(i); // CIJ = zeros(N); MATRIX_T* CIJ = zeros(N); // CIJ(irp(1:K)) = w; VECTOR_ID(view) irp_subv = VECTOR_ID(subvector)(irp, 0, K); ordinal_index_assign(CIJ, &irp_subv.vector, w); VECTOR_ID(free)(w); VECTOR_ID(free)(irp); return CIJ; }
/* * Generates a random directed binary graph with N nodes and K edges. No edges * are placed on the main diagonal. */ MATRIX_T* BCT_NAMESPACE::makerandCIJ_bd(int N, int K) { // ind = ~eye(N); MATRIX_T* eye_N = eye(N); MATRIX_T* ind = logical_not(eye_N); MATRIX_ID(free)(eye_N); // i = find(ind); VECTOR_T* i = find(ind); MATRIX_ID(free)(ind); // rp = randperm(length(i)); gsl_permutation* rp = randperm(length(i)); // irp = i(rp); VECTOR_T* irp = permute(rp, i); gsl_permutation_free(rp); VECTOR_ID(free)(i); // CIJ = zeros(N); MATRIX_T* CIJ = zeros(N); // CIJ(irp(1:K)) = 1; VECTOR_ID(view) irp_subv = VECTOR_ID(subvector)(irp, 0, K); ordinal_index_assign(CIJ, &irp_subv.vector, 1.0); VECTOR_ID(free)(irp); return CIJ; }
void randperm(std::vector<int> &r, int n, void *d) { int *p; r.resize(n); p = &(r[0]); randperm(p, n, d); }
int main(void) { /*Matrix& A0 = *new DenseMatrix(new double[4] {1, 2, 3, 4}, 4, 1); disp(A0); Matrix& A1 = reshape(A0, new int[2] {2, 2}); disp(A1);*/ int m = 8; int r = m / 4; Matrix& L = randn(m, r); Matrix& R = randn(m, r); Matrix& A_star = mtimes(L, R.transpose()); Matrix& E_star0 = zeros(size(A_star)); int* indices = randperm(m * m); int nz = m * m / 20; int* nz_indices = new int[nz]; for (int i = 0; i < nz; i++) { nz_indices[i] = indices[i] - 1; } Matrix& E_vec = vec(E_star0); Matrix& Temp = (minus(rand(nz, 1), 0.5).times(100)); // disp(Temp); setSubMatrix(E_vec, nz_indices, nz, new int[1] {0}, 1, Temp); // disp(E_vec); Matrix& E_star = reshape(E_vec, size(E_star0)); // disp(E_star); // Input Matrix& D = A_star.plus(E_star); double lambda = 1 * pow(m, -0.5); RobustPCA& robustPCA = *new RobustPCA(lambda); robustPCA.feedData(D); tic(); robustPCA.run(); fprintf("Elapsed time: %.2f seconds.\n", toc()); // Output Matrix& A_hat = robustPCA.GetLowRankEstimation(); Matrix& E_hat = robustPCA.GetErrorMatrix(); fprintf("A*:\n"); disp(A_star, 4); fprintf("A^:\n"); disp(A_hat, 4); fprintf("E*:\n"); disp(E_star, 4); fprintf("E^:\n"); disp(E_hat, 4); fprintf("rank(A*): %d\n", rank(A_star)); fprintf("rank(A^): %d\n", rank(A_hat)); fprintf("||A* - A^||_F: %.4f\n", norm(A_star.minus(A_hat), "fro")); fprintf("||E* - E^||_F: %.4f\n", norm(E_star.minus(E_hat), "fro")); return EXIT_SUCCESS; }
point TargetControl::genTarget(point currentPos, bool directionMatters) { double theta; point targetPos; if(!directionMatters) //Direction not constrained this trial { do { theta=randb(0,360); targetPos=currentPos+point(cos(theta),sin(theta))*spawnDist; } while(targetPos.mag()>innerRadius); return targetPos; } //If direction predetermined, use it if (nextDirection>=0) {targetPos=currentPos+point(cos(nextDirection),sin(nextDirection))*spawnDist; nextDirection=-1; return targetPos;} //Make sure we haven't bottomed out int sum=0; for(int k=0;k<directions;k++) { sum+=((timesUsed[k]>=minUses)?0:1); } if (sum==0) minUses++; //Find the best direction double min=std::numeric_limits<double>::infinity(); int index=-1; double mag; point bestPos; for(int k=0;k<directions;k++) { targetPos=currentPos+point(cos(direction[k]),sin(direction[k]))*spawnDist; mag=targetPos.mag(); if((mag<min)&&(timesUsed[k]<minUses)&&(mag<innerRadius)) {min=mag; index=k; bestPos=targetPos;} } if(index != -1) { timesUsed[index]++; return bestPos; } //If no direction can produce a legal target pick randomly among the least used and set up for it int m=INT_MAX; int i; int * shuffle = new int[directions]; shuffle[0]=directions; randperm(shuffle); for(int k=0;k<directions;k++) if(timesUsed[shuffle[k]]<m) {m=timesUsed[shuffle[k]]; i=shuffle[k];} timesUsed[i]++; nextDirection=direction[i]; theta=atan2(currentPos.Y()+spawnDist*sin(direction[i]),currentPos.X()+spawnDist*cos(direction[i])); targetPos=currentPos - point(cos(theta),sin(theta))*spawnDist; return (targetPos.mag()<=maxRadius)?targetPos:point(0,0); }
int main(int argc, char *argv[]) { // initialize random seed based on current time srand((unsigned) time(NULL)); // Question 1 double x[] = {3,5,4,3,6,7,-1,2,-3,-4,2,-5,3,2,-1}; double y[] = {2,7,4,7,6,9,-1,3,-2,-1,3,-1,2,4,2}; double d = mean(y,15) - mean(x,15); int bootcount = 0; int i, j; int nboot = 1e6; double xy[30]; double di; for (i=0; i<30; i++) { if (i<15) xy[i] = x[i]; else xy[i] = y[i-15]; } int seq[30]; double xi[15], yi[15]; for (i=0; i<nboot; i++) { randperm(seq, 30); for (j=0; j<30; j++) { if (j<15) xi[j] = xy[seq[j]]; else yi[j-15] = xy[seq[j]]; } di = mean(yi,15) - mean(xi,15); if (di >= d) bootcount = bootcount + 1; } double pboot = (double)bootcount / (double)nboot; printf("pboot = %7.5f\n", pboot); return 0; }
//! Initialize the oracle void init() { assert(params.valid()); // Initialize the random number generator rng.seed(static_cast<unsigned>(params.random_seed)); uniform_prob = boost::uniform_real<double>(0,1); // Construct the graph and make CPTs if (num_finite() > 2 * params.num_parents) { for (size_t j = 2 * params.num_parents; j < num_finite(); ++j) { // Choose NUM_PARENTS random parents finite_domain parents; std::vector<size_t> r(randperm(j, rng)); for (size_t k = 0; k < params.num_parents; ++k) parents.insert(finite_seq[r[k]]); parents.insert(finite_seq[j]); tablef f(parents.plus(finite_seq[j])); // RIGHT HERE NOW: MAKE FACTOR bn.add_factor(parents, finite_seq[j]); } } }
int main(int argc, char* argv[]) { double *w; /* weight vector */ long m, i; double C, epsilon; LEARN_PARM learn_parm; KERNEL_PARM kernel_parm; char trainfile[1024]; char modelfile[1024]; int MAX_ITER; /* new struct variables */ SVECTOR **fycache, *diff, *fy; EXAMPLE *ex; SAMPLE alldata; SAMPLE sample; SAMPLE val; STRUCT_LEARN_PARM sparm; STRUCTMODEL sm; double primal_obj; double stop_crit; char itermodelfile[2000]; /* self-paced learning variables */ double init_spl_weight; double spl_weight; double spl_factor; int *valid_examples; /* read input parameters */ my_read_input_parameters(argc, argv, trainfile, modelfile, &learn_parm, &kernel_parm, &sparm, &init_spl_weight, &spl_factor); epsilon = learn_parm.eps; C = learn_parm.svm_c; MAX_ITER = learn_parm.maxiter; /* read in examples */ alldata = read_struct_examples(trainfile,&sparm); int ntrain = (int) round(1.0*alldata.n); /* no validation set */ if(ntrain < alldata.n) { long *perm = randperm(alldata.n); sample = generate_train_set(alldata, perm, ntrain); val = generate_validation_set(alldata, perm, ntrain); free(perm); } else { sample = alldata; } ex = sample.examples; m = sample.n; /* initialization */ init_struct_model(alldata,&sm,&sparm,&learn_parm,&kernel_parm); w = create_nvector(sm.sizePsi); clear_nvector(w, sm.sizePsi); sm.w = w; /* establish link to w, as long as w does not change pointer */ /* some training information */ printf("C: %.8g\n", C); printf("spl weight: %.8g\n",init_spl_weight); printf("epsilon: %.8g\n", epsilon); printf("sample.n: %d\n", sample.n); printf("sm.sizePsi: %ld\n", sm.sizePsi); fflush(stdout); /* prepare feature vector cache for correct labels with imputed latent variables */ fycache = (SVECTOR**)malloc(m*sizeof(SVECTOR*)); for (i=0;i<m;i++) { fy = psi(ex[i].x, ex[i].y, &sm, &sparm); diff = add_list_ss(fy); free_svector(fy); fy = diff; fycache[i] = fy; } /* learn initial weight vector using all training examples */ valid_examples = (int *) malloc(m*sizeof(int)); /* errors for validation set */ double cur_loss, best_loss = DBL_MAX; int loss_iter; /* initializations */ spl_weight = init_spl_weight; /* solve biconvex self-paced learning problem */ primal_obj = alternate_convex_search(w, m, MAX_ITER, C, epsilon, fycache, ex, &sm, &sparm, valid_examples, spl_weight); printf("primal objective: %.4f\n", primal_obj); fflush(stdout); //alternate_convex_search(w, m, MAX_ITER, C, epsilon, fycache, ex, &sm, &sparm, valid_examples, spl_weight); int nValid = 0; for (i=0;i<m;i++) { if(valid_examples[i]) { nValid++; } } if(ntrain < alldata.n) { cur_loss = compute_current_loss(val,&sm,&sparm); printf("CURRENT LOSS: %f\n",cur_loss); } /* write structural model */ write_struct_model(modelfile, &sm, &sparm); // skip testing for the moment /* free memory */ free_struct_sample(alldata); if(ntrain < alldata.n) { free(sample.examples); free(val.examples); } free_struct_model(sm, &sparm); for(i=0;i<m;i++) { free_svector(fycache[i]); } free(fycache); free(valid_examples); return(0); }
//trains a neural net void FBNN::train(const FMatrix & train_x, const FMatrix & train_y, const Opts & opts, const FMatrix & valid_x, const FMatrix & valid_y, const FBNN_ptr pFBNN) { int ibatchNum = train_x.rows() / opts.batchsize + (train_x.rows() % opts.batchsize != 0); FMatrix L = zeros(opts.numpochs * ibatchNum, 1); m_oLp = std::make_shared<FMatrix>(L); Loss loss; // std::cout << "numpochs = " << opts.numpochs << std::endl; for(int i = 0; i < opts.numpochs; ++i) { std::cout << "start numpochs " << i << std::endl; int elapsedTime = count_elapse_second([&train_x,&train_y,&L,&opts,i,pFBNN,ibatchNum,this]{ std::vector<int> iRandVec; randperm(train_x.rows(),iRandVec); std::cout << "start batch: "; for(int j = 0; j < ibatchNum; ++j) { std::cout << " " << j; if(pFBNN)//pull { // TMutex::scoped_lock lock; // lock.acquire(pFBNN->W_RWMutex,false); // lock.release();//reader lock tbb boost::shared_lock<RWMutex> rlock(pFBNN->W_RWMutex); set_m_oWs(pFBNN->get_m_oWs()); if(m_fMomentum > 0) set_m_oVWs(pFBNN->get_m_oVWs()); rlock.unlock(); } int curBatchSize = opts.batchsize; if(j == ibatchNum - 1 && train_x.rows() % opts.batchsize != 0) curBatchSize = train_x.rows() % opts.batchsize; FMatrix batch_x(curBatchSize,train_x.columns()); for(int r = 0; r < curBatchSize; ++r)//randperm() row(batch_x,r) = row(train_x,iRandVec[j * opts.batchsize + r]); //Add noise to input (for use in denoising autoencoder) if(m_fInputZeroMaskedFraction != 0) batch_x = bitWiseMul(batch_x,(rand(curBatchSize,train_x.columns())>m_fInputZeroMaskedFraction)); FMatrix batch_y(curBatchSize,train_y.columns()); for(int r = 0; r < curBatchSize; ++r)//randperm() row(batch_y,r) = row(train_y,iRandVec[j * opts.batchsize + r]); L(i*ibatchNum+j,0) = nnff(batch_x,batch_y); nnbp(); nnapplygrads(); if(pFBNN)//push { // TMutex::scoped_lock lock; // lock.acquire(W_RWMutex); // lock.release();//writer lock tbb boost::unique_lock<RWMutex> wlock(pFBNN->W_RWMutex); pFBNN->set_m_odWs(m_odWs); pFBNN->nnapplygrads(); wlock.unlock(); } // std::cout << "end batch " << j << std::endl; } std::cout << std::endl; }); std::cout << "elapsed time: " << elapsedTime << "s" << std::endl; //loss calculate use nneval if(valid_x.rows() == 0 || valid_y.rows() == 0){ nneval(loss, train_x, train_y); std::cout << "Full-batch train mse = " << loss.train_error.back() << std::endl; } else{ nneval(loss, train_x, train_y, valid_x, valid_y); std::cout << "Full-batch train mse = " << loss.train_error.back() << " , val mse = " << loss.valid_error.back() << std::endl; } std::cout << "epoch " << i+1 << " / " << opts.numpochs << " took " << elapsedTime << " seconds." << std::endl; std::cout << "Mini-batch mean squared error on training set is " << columnMean(submatrix(L,i*ibatchNum,0UL,ibatchNum,L.columns())) << std::endl; m_iLearningRate *= m_fScalingLearningRate; // std::cout << "end numpochs " << i << std::endl; } }
int Solver::examine(int index_j) { double y2 = y[index_j]; double alph2 = alpha[index_j]; double E2 = error[index_j]; double r2 = E2 * y2; int index_i = 0; std::vector<int>::iterator iter; std::vector<int> nonBoundAlphaIdx; if ((r2 < -EPS && alph2 < C) || (r2 > EPS && alph2 > 0)) { //find number and indices of non-zero and non-C alphas nonBoundAlphaIdx.clear(); // reset index vector for (index_i = 0; index_i < length; index_i++) { if (alpha[index_i] < 0) { std::clog << "DEBUG:: alpha returned was < 0" << std::endl; } if (alpha[index_i] > EPS && alpha[index_i] < (C - EPS) && alpha[index_i] > (C + EPS)) { // push non-bound alpha index into vector cache //TODO: remove printf("-------- found non-zero/bound alpha -------------\n"); nonBoundAlphaIdx.push_back(index_i); } } // try to perform second choice heuristic to choose index_i int result = 0; if (nonBoundAlphaIdx.size() > 1) { index_i = 0; // reset index_i // choose multiplier to maximize the step taken; i.e. max(|E1 - E2|); double errorDiff = 0; for (iter = nonBoundAlphaIdx.begin(); iter != nonBoundAlphaIdx.end(); ++iter) { if (fabs(error[*iter] - E2) > errorDiff) { //TODO: remove printf("looking at error index %f \n",error[*iter]); index_i = *iter; errorDiff = fabs(error[*iter] - E2); } } //TODO: if () the errortemp doesnt stay 0? result = update(index_i, index_j); if (result == 1) { return 1; } } //loop over all non-zero and non-c alpha, starting at a random point random_shuffle(nonBoundAlphaIdx.begin(), nonBoundAlphaIdx.end()); for (iter = nonBoundAlphaIdx.begin(); iter != nonBoundAlphaIdx.end(); ++iter) { index_i = *iter; //TODO: remove printf("-------- random nonbound -------------"); printf("picked index %d\n", *iter); result = update(index_i, index_j); if (result == 1) { return 1; } } // else loop over all possible i1, starting at random point randperm(randi, length); for (int i = 0; i < length; i++) { index_i = randi[i]; result = update(index_i, index_j); if (result == 1) { return 1; } } } // if error > tolerance return 0; }
IGL_INLINE Eigen::PlainObjectBase<DerivedI> igl::randperm( const int n) { Eigen::PlainObjectBase<DerivedI> I; randperm(n,I); return I; }
index_t *alloc_randperm(index_t n, index_t seed) { index_t *p = alloc_idxtab(n); randperm(n, p, seed); return p; }
int permute_trans (permute_t *perm, state_info_t *state, perm_cb_f cb, void *ctx) { perm->call_ctx = ctx; perm->real_cb = cb; perm->state = state; perm->nstored = perm->start_group_index = 0; int count = 0; state_data_t data = state_info_pins_state (state); if (inhibit) { int N = dm_nrows (perm->inhibit_matrix); int class_count[N]; for (int i = 0; i < N; i++) { class_count[i] = 0; if (is_inhibited(perm, class_count, i)) continue; if (perm->class_label >= 0) { class_count[i] = GBgetTransitionsMatching (perm->model, perm->class_label, i, data, permute_one, perm); } else if (perm->class_matrix != NULL) { class_count[i] = GBgetTransitionsMarked (perm->model, perm->class_matrix, i, data, permute_one, perm); } else { Abort ("inhibit set, but no known classification found."); } count += class_count[i]; } } else { count = GBgetTransitionsAll (perm->model, data, permute_one, perm); } switch (perm->permutation) { case Perm_Otf: randperm (perm->pad, perm->nstored, state->ref + perm->shiftorder); for (size_t i = 0; i < perm->nstored; i++) perm_do (perm, perm->pad[i]); break; case Perm_Random: for (size_t i = 0; i < perm->nstored; i++) perm_do (perm, perm->rand[perm->nstored][i]); break; case Perm_Dynamic: sort_r (perm->tosort, perm->nstored, sizeof(int), dyn_cmp, perm); perm_do_all (perm); break; case Perm_RR: sort_r (perm->tosort, perm->nstored, sizeof(int), rr_cmp, perm); perm_do_all (perm); break; case Perm_SR: sort_r (perm->tosort, perm->nstored, sizeof(int), rand_cmp, perm); perm_do_all (perm); break; case Perm_Sort: sort_r (perm->tosort, perm->nstored, sizeof(int), sort_cmp, perm); perm_do_all (perm); break; case Perm_Shift: perm_do_all (perm); break; case Perm_Shift_All: for (size_t i = 0; i < perm->nstored; i++) { size_t j = (perm->start_group_index + i); j = j < perm->nstored ? j : 0; perm_do (perm, j); } break; case Perm_None: break; default: Abort ("Unknown permutation!"); } return count; }
void SGVector<T>::randperm() { randperm(vector, vlen); }
int main (void) { KLU_common Common ; cholmod_sparse *A, *A2 ; cholmod_dense *X, *B ; cholmod_common ch ; Int *Ap, *Ai, *Puser, *Quser, *Gunk ; double *Ax, *Bx, *Xx, *A2x ; double one [2], zero [2], xsave, maxerr ; Int n, i, j, nz, save, isreal, k, nan ; KLU_symbolic *Symbolic, *Symbolic2 ; KLU_numeric *Numeric ; one [0] = 1 ; one [1] = 0 ; zero [0] = 0 ; zero [1] = 0 ; printf ("klu test: -------------------------------------------------\n") ; OK (klu_defaults (&Common)) ; CHOLMOD_start (&ch) ; ch.print = 0 ; normal_memory_handler (&Common) ; /* ---------------------------------------------------------------------- */ /* read in a sparse matrix from stdin */ /* ---------------------------------------------------------------------- */ A = CHOLMOD_read_sparse (stdin, &ch) ; if (A->nrow != A->ncol || A->stype != 0) { fprintf (stderr, "error: only square unsymmetric matrices handled\n") ; CHOLMOD_free_sparse (&A, &ch) ; return (0) ; } if (!(A->xtype == CHOLMOD_REAL || A->xtype == CHOLMOD_COMPLEX)) { fprintf (stderr, "error: only real or complex matrices hanlded\n") ; CHOLMOD_free_sparse (&A, &ch) ; return (0) ; } n = A->nrow ; Ap = A->p ; Ai = A->i ; Ax = A->x ; nz = Ap [n] ; isreal = (A->xtype == CHOLMOD_REAL) ; /* ---------------------------------------------------------------------- */ /* construct random permutations */ /* ---------------------------------------------------------------------- */ Puser = randperm (n, n) ; Quser = randperm (n, n) ; /* ---------------------------------------------------------------------- */ /* select known solution to Ax=b */ /* ---------------------------------------------------------------------- */ X = CHOLMOD_allocate_dense (n, NRHS, n, A->xtype, &ch) ; Xx = X->x ; for (j = 0 ; j < NRHS ; j++) { for (i = 0 ; i < n ; i++) { if (isreal) { Xx [i] = 1 + ((double) i) / ((double) n) + j * 100; } else { Xx [2*i ] = 1 + ((double) i) / ((double) n) + j * 100 ; Xx [2*i+1] = - ((double) i+1) / ((double) n + j) ; if (j == NRHS-1) { Xx [2*i+1] = 0 ; /* zero imaginary part */ } else if (j == NRHS-2) { Xx [2*i] = 0 ; /* zero real part */ } } } Xx += isreal ? n : 2*n ; } /* B = A*X */ B = CHOLMOD_allocate_dense (n, NRHS, n, A->xtype, &ch) ; CHOLMOD_sdmult (A, 0, one, zero, X, B, &ch) ; Bx = B->x ; /* ---------------------------------------------------------------------- */ /* test KLU */ /* ---------------------------------------------------------------------- */ test_memory_handler (&Common) ; maxerr = do_solves (A, B, X, Puser, Quser, &Common, &ch, &nan) ; /* ---------------------------------------------------------------------- */ /* basic error checking */ /* ---------------------------------------------------------------------- */ FAIL (klu_defaults (NULL)) ; FAIL (klu_extract (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) ; FAIL (klu_extract (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &Common)) ; FAIL (klu_z_extract (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) ; FAIL (klu_z_extract (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &Common)) ; FAIL (klu_analyze (0, NULL, NULL, NULL)) ; FAIL (klu_analyze (0, NULL, NULL, &Common)) ; FAIL (klu_analyze_given (0, NULL, NULL, NULL, NULL, NULL)) ; FAIL (klu_analyze_given (0, NULL, NULL, NULL, NULL, &Common)) ; FAIL (klu_cholmod (0, NULL, NULL, NULL, NULL)) ; FAIL (klu_factor (NULL, NULL, NULL, NULL, NULL)) ; FAIL (klu_factor (NULL, NULL, NULL, NULL, &Common)) ; FAIL (klu_z_factor (NULL, NULL, NULL, NULL, NULL)) ; FAIL (klu_z_factor (NULL, NULL, NULL, NULL, &Common)) ; FAIL (klu_refactor (NULL, NULL, NULL, NULL, NULL, NULL)) ; FAIL (klu_refactor (NULL, NULL, NULL, NULL, NULL, &Common)) ; FAIL (klu_z_refactor (NULL, NULL, NULL, NULL, NULL, NULL)) ; FAIL (klu_z_refactor (NULL, NULL, NULL, NULL, NULL, &Common)) ; FAIL (klu_rgrowth (NULL, NULL, NULL, NULL, NULL, NULL)) ; FAIL (klu_rgrowth (NULL, NULL, NULL, NULL, NULL, &Common)) ; FAIL (klu_z_rgrowth (NULL, NULL, NULL, NULL, NULL, NULL)) ; FAIL (klu_z_rgrowth (NULL, NULL, NULL, NULL, NULL, &Common)) ; FAIL (klu_condest (NULL, NULL, NULL, NULL, NULL)) ; FAIL (klu_condest (NULL, NULL, NULL, NULL, &Common)) ; FAIL (klu_z_condest (NULL, NULL, NULL, NULL, NULL)) ; FAIL (klu_z_condest (NULL, NULL, NULL, NULL, &Common)) ; FAIL (klu_flops (NULL, NULL, NULL)) ; FAIL (klu_flops (NULL, NULL, &Common)) ; FAIL (klu_z_flops (NULL, NULL, NULL)) ; FAIL (klu_z_flops (NULL, NULL, &Common)) ; FAIL (klu_rcond (NULL, NULL, NULL)) ; FAIL (klu_rcond (NULL, NULL, &Common)) ; FAIL (klu_z_rcond (NULL, NULL, NULL)) ; FAIL (klu_z_rcond (NULL, NULL, &Common)) ; FAIL (klu_free_symbolic (NULL, NULL)) ; OK (klu_free_symbolic (NULL, &Common)) ; FAIL (klu_free_numeric (NULL, NULL)) ; OK (klu_free_numeric (NULL, &Common)) ; FAIL (klu_z_free_numeric (NULL, NULL)) ; OK (klu_z_free_numeric (NULL, &Common)) ; FAIL (klu_scale (0, 0, NULL, NULL, NULL, NULL, NULL, NULL)) ; FAIL (klu_scale (0, 0, NULL, NULL, NULL, NULL, NULL, &Common)) ; OK (klu_scale (-1, 0, NULL, NULL, NULL, NULL, NULL, &Common)) ; FAIL (klu_z_scale (0, 0, NULL, NULL, NULL, NULL, NULL, NULL)) ; FAIL (klu_z_scale (0, 0, NULL, NULL, NULL, NULL, NULL, &Common)) ; OK (klu_z_scale (-1, 0, NULL, NULL, NULL, NULL, NULL, &Common)) ; FAIL (klu_solve (NULL, NULL, 0, 0, NULL, NULL)) ; FAIL (klu_solve (NULL, NULL, 0, 0, NULL, &Common)) ; FAIL (klu_z_solve (NULL, NULL, 0, 0, NULL, NULL)) ; FAIL (klu_z_solve (NULL, NULL, 0, 0, NULL, &Common)) ; FAIL (klu_tsolve (NULL, NULL, 0, 0, NULL, NULL)) ; FAIL (klu_tsolve (NULL, NULL, 0, 0, NULL, &Common)) ; FAIL (klu_z_tsolve (NULL, NULL, 0, 0, NULL, 0, NULL)) ; FAIL (klu_z_tsolve (NULL, NULL, 0, 0, NULL, 0, &Common)) ; FAIL (klu_malloc (0, 0, NULL)) ; FAIL (klu_malloc (0, 0, &Common)) ; FAIL (klu_malloc (Int_MAX, 1, &Common)) ; FAIL (klu_realloc (0, 0, 0, NULL, NULL)) ; FAIL (klu_realloc (0, 0, 0, NULL, &Common)) ; FAIL (klu_realloc (Int_MAX, 1, 0, NULL, &Common)) ; Gunk = (Int *) klu_realloc (1, 0, sizeof (Int), NULL, &Common) ; OK (Gunk) ; OK (klu_realloc (Int_MAX, 1, sizeof (Int), Gunk, &Common)) ; OK (Common.status == KLU_TOO_LARGE) ; klu_free (Gunk, 1, sizeof (Int), &Common) ; /* ---------------------------------------------------------------------- */ /* mangle the matrix, and other error checking */ /* ---------------------------------------------------------------------- */ printf ("\nerror handling:\n") ; Symbolic = klu_analyze (n, Ap, Ai, &Common) ; OK (Symbolic) ; Xx = X->x ; if (nz > 0) { /* ------------------------------------------------------------------ */ /* row index out of bounds */ /* ------------------------------------------------------------------ */ save = Ai [0] ; Ai [0] = -1 ; FAIL (klu_analyze (n, Ap, Ai, &Common)) ; if (isreal) { FAIL (klu_scale (1, n, Ap, Ai, Ax, Xx, Puser, &Common)) ; } else { FAIL (klu_z_scale (1, n, Ap, Ai, Ax, Xx, Puser, &Common)) ; } Ai [0] = save ; /* ------------------------------------------------------------------ */ /* row index out of bounds */ /* ------------------------------------------------------------------ */ save = Ai [0] ; Ai [0] = Int_MAX ; FAIL (klu_analyze (n, Ap, Ai, &Common)) ; if (isreal) { FAIL (klu_scale (1, n, Ap, Ai, Ax, Xx, Puser, &Common)) ; } else { FAIL (klu_z_scale (1, n, Ap, Ai, Ax, Xx, Puser, &Common)) ; } Ai [0] = save ; /* ------------------------------------------------------------------ */ /* column pointers mangled */ /* ------------------------------------------------------------------ */ save = Ap [n] ; Ap [n] = -1 ; FAIL (klu_analyze (n, Ap, Ai, &Common)) ; if (isreal) { FAIL (klu_scale (1, n, Ap, Ai, Ax, Xx, Puser, &Common)) ; } else { FAIL (klu_z_scale (1, n, Ap, Ai, Ax, Xx, Puser, &Common)) ; } Ap [n] = save ; /* ------------------------------------------------------------------ */ /* column pointers mangled */ /* ------------------------------------------------------------------ */ save = Ap [n] ; Ap [n] = Ap [n-1] - 1 ; FAIL (klu_analyze (n, Ap, Ai, &Common)) ; if (isreal) { FAIL (klu_scale (1, n, Ap, Ai, Ax, Xx, Puser, &Common)) ; } else { FAIL (klu_z_scale (1, n, Ap, Ai, Ax, Xx, Puser, &Common)) ; } Ap [n] = save ; /* ------------------------------------------------------------------ */ /* duplicates */ /* ------------------------------------------------------------------ */ if (n > 1 && Ap [1] - Ap [0] > 1) { save = Ai [1] ; Ai [1] = Ai [0] ; FAIL (klu_analyze (n, Ap, Ai, &Common)) ; if (isreal) { FAIL (klu_scale (1, n, Ap, Ai, Ax, Xx, Puser, &Common)) ; } else { FAIL (klu_z_scale (1, n, Ap, Ai, Ax, Xx, Puser, &Common)) ; } Ai [1] = save ; } /* ------------------------------------------------------------------ */ /* invalid ordering */ /* ------------------------------------------------------------------ */ save = Common.ordering ; Common.ordering = 42 ; FAIL (klu_analyze (n, Ap, Ai, &Common)) ; Common.ordering = save ; /* ------------------------------------------------------------------ */ /* invalid ordering (klu_cholmod, with NULL user_ordering) */ /* ------------------------------------------------------------------ */ save = Common.ordering ; Common.user_order = NULL ; Common.ordering = 3 ; FAIL (klu_analyze (n, Ap, Ai, &Common)) ; Common.ordering = save ; } /* ---------------------------------------------------------------------- */ /* tests with valid symbolic factorization */ /* ---------------------------------------------------------------------- */ Common.halt_if_singular = FALSE ; Common.scale = 0 ; Numeric = NULL ; if (nz > 0) { /* ------------------------------------------------------------------ */ /* Int overflow */ /* ------------------------------------------------------------------ */ if (n == 100) { Common.ordering = 2 ; Symbolic2 = klu_analyze (n, Ap, Ai, &Common) ; OK (Symbolic2) ; Common.memgrow = Int_MAX ; if (isreal) { Numeric = klu_factor (Ap, Ai, Ax, Symbolic2, &Common) ; } else { Numeric = klu_z_factor (Ap, Ai, Ax, Symbolic2, &Common) ; } Common.memgrow = 1.2 ; Common.ordering = 0 ; klu_free_symbolic (&Symbolic2, &Common) ; klu_free_numeric (&Numeric, &Common) ; } /* ------------------------------------------------------------------ */ /* Int overflow again */ /* ------------------------------------------------------------------ */ Common.initmem = Int_MAX ; Common.initmem_amd = Int_MAX ; if (isreal) { Numeric = klu_factor (Ap, Ai, Ax, Symbolic, &Common) ; } else { Numeric = klu_z_factor (Ap, Ai, Ax, Symbolic, &Common) ; } Common.initmem = 10 ; Common.initmem_amd = 1.2 ; klu_free_numeric (&Numeric, &Common) ; /* ------------------------------------------------------------------ */ /* mangle the matrix */ /* ------------------------------------------------------------------ */ save = Ai [0] ; Ai [0] = -1 ; if (isreal) { Numeric = klu_factor (Ap, Ai, Ax, Symbolic, &Common) ; } else { Numeric = klu_z_factor (Ap, Ai, Ax, Symbolic, &Common) ; } FAIL (Numeric) ; Ai [0] = save ; /* ------------------------------------------------------------------ */ /* nan and inf handling */ /* ------------------------------------------------------------------ */ xsave = Ax [0] ; Ax [0] = one [0] / zero [0] ; if (isreal) { Numeric = klu_factor (Ap, Ai, Ax, Symbolic, &Common) ; klu_rcond (Symbolic, Numeric, &Common) ; klu_condest (Ap, Ax, Symbolic, Numeric, &Common) ; } else { Numeric = klu_z_factor (Ap, Ai, Ax, Symbolic, &Common) ; klu_z_rcond (Symbolic, Numeric, &Common) ; klu_z_condest (Ap, Ax, Symbolic, Numeric, &Common) ; } printf ("Nan case: rcond %g condest %g\n", Common.rcond, Common.condest) ; OK (Numeric) ; Ax [0] = xsave ; /* ------------------------------------------------------------------ */ /* mangle the matrix again */ /* ------------------------------------------------------------------ */ save = Ai [0] ; Ai [0] = -1 ; if (isreal) { FAIL (klu_refactor (Ap, Ai, Ax, Symbolic, Numeric, &Common)) ; } else { FAIL (klu_z_refactor (Ap, Ai, Ax, Symbolic, Numeric, &Common)) ; } Ai [0] = save ; /* ------------------------------------------------------------------ */ /* all zero */ /* ------------------------------------------------------------------ */ A2 = CHOLMOD_copy_sparse (A, &ch) ; A2x = A2->x ; for (k = 0 ; k < nz * (isreal ? 1:2) ; k++) { A2x [k] = 0 ; } for (Common.halt_if_singular = 0 ; Common.halt_if_singular <= 1 ; Common.halt_if_singular++) { for (Common.scale = -1 ; Common.scale <= 2 ; Common.scale++) { if (isreal) { klu_refactor (Ap, Ai, A2x, Symbolic, Numeric, &Common) ; klu_condest (Ap, A2x, Symbolic, Numeric, &Common) ; } else { klu_z_refactor (Ap, Ai, A2x, Symbolic, Numeric, &Common) ; klu_z_condest (Ap, A2x, Symbolic, Numeric, &Common) ; } OK (Common.status = KLU_SINGULAR) ; } } CHOLMOD_free_sparse (&A2, &ch) ; /* ------------------------------------------------------------------ */ /* all one, or all 1i for complex case */ /* ------------------------------------------------------------------ */ A2 = CHOLMOD_copy_sparse (A, &ch) ; A2x = A2->x ; for (k = 0 ; k < nz ; k++) { if (isreal) { A2x [k] = 1 ; } else { A2x [2*k ] = 0 ; A2x [2*k+1] = 1 ; } } Common.halt_if_singular = 0 ; Common.scale = 0 ; if (isreal) { klu_refactor (Ap, Ai, A2x, Symbolic, Numeric, &Common) ; klu_condest (Ap, A2x, Symbolic, Numeric, &Common) ; } else { klu_z_refactor (Ap, Ai, A2x, Symbolic, Numeric, &Common) ; klu_z_condest (Ap, A2x, Symbolic, Numeric, &Common) ; } OK (Common.status = KLU_SINGULAR) ; CHOLMOD_free_sparse (&A2, &ch) ; } klu_free_symbolic (&Symbolic, &Common) ; if (isreal) { klu_free_numeric (&Numeric, &Common) ; } else { klu_z_free_numeric (&Numeric, &Common) ; } /* ---------------------------------------------------------------------- */ /* free problem and quit */ /* ---------------------------------------------------------------------- */ CHOLMOD_free_dense (&X, &ch) ; CHOLMOD_free_dense (&B, &ch) ; CHOLMOD_free_sparse (&A, &ch) ; free (Puser) ; free (Quser) ; CHOLMOD_finish (&ch) ; fprintf (stderr, " maxerr %10.3e", maxerr) ; printf (" maxerr %10.3e", maxerr) ; if (maxerr < 1e-8) { fprintf (stderr, " test passed") ; printf (" test passed") ; } else { fprintf (stderr, " test FAILED") ; printf (" test FAILED") ; } if (nan) { fprintf (stderr, " *") ; printf (" *") ; } fprintf (stderr, "\n") ; printf ("\n-----------------------------------------------------------\n") ; return (0) ; }
void SMaxReg::train(const Mat &data, const Mat &label, int batchSize) { assert(data.type() == CV_32FC1 && label.type() == CV_32FC1); assert(batchSize >= 1); srand((uchar)time(NULL)); Mat labelMatrix; realLabel2Matrix(label, labelMatrix); // initialize weight and bias int numData = data.rows; int dimData = data.cols; if (weight.empty() == true) { weight = Mat::zeros(dimData, numClasses, data.type()); randu(weight, 0, 1); weight *= 0.001; } else { if (weight.rows != dimData || weight.cols != numClasses) { printf("Initial weight dimension wrong: weight.rows == dimData && weight.cols == numClasses!\n"); abort(); } } Mat velocity = Mat::zeros(weight.size(), weight.type()); vector<int> index(numData, 0); for (int i = 0; i < numData; ++i) { index[i] = i; } Mat batchData, batchLabel; // batch data and label Mat rsp, maxRsp; // feed-forward response Mat prob, sumProb, logProb; // softmax prediction probability Mat gradient, ww; // update bool isConverge = false; double prevCost = 0; double currCost = 0; // cost double mom = 0.5; // moment int t = 0; // iteration int numBatches = floor(numData / batchSize); for (int ei = 0; ei < epochs; ++ei) { randperm(index); batchData = Mat::zeros(batchSize, dimData, data.type()); batchLabel = Mat::zeros(batchSize, numClasses, labelMatrix.type()); for (int batchIdx = 0; batchIdx < numBatches; ++batchIdx) { // batch SGD t++; if (t == 20) mom = moment; getBatchData(data, labelMatrix, index, batchSize, batchIdx, batchData, batchLabel); // if (batchIdx == (numBatches - 1)) { // int batchRange = data.rows - batchIdx*batchSize; // batchData = batchData.rowRange(0, batchRange); // batchLabel = batchLabel.rowRange(0, batchRange); // } rsp = batchData * weight; reduce(rsp, maxRsp, 1, REDUCE_MAX, rsp.type()); rsp -= repeat(maxRsp, 1, numClasses); exp(rsp, prob); reduce(prob, sumProb, 1, REDUCE_SUM, prob.type()); prob = prob / repeat(sumProb, 1, numClasses); // compute gradient gradient = batchLabel - prob; gradient = batchData.t() * gradient; gradient = -gradient / batchData.rows; gradient += regularizer * weight; // update weight and bias velocity = mom * velocity + learningRate * gradient; weight -= velocity; // compute objective cost log(prob, logProb); logProb = batchLabel.mul(logProb); currCost = -(sum(logProb)[0] / batchData.rows); pow(weight, 2, ww); currCost += sum(ww)[0] * 0.5 * regularizer; printf("epoch %d: processing batch %d / %d cost %f\n", ei + 1, batchIdx + 1, numBatches, currCost); if (abs(currCost - prevCost) < epsilon && ei != 0) { printf("objective cost variation less than pre-defined %f\n", epsilon); isConverge = true; break; } prevCost = currCost; } batchData.release(); batchLabel.release(); if (isConverge == true) break; } if (isConverge == false) { printf("stopped by reaching maximum number of iterations\n"); } // free and destroy space index.clear(); labelMatrix.release(); batchData.release(); batchLabel.release(); rsp.release(); maxRsp.release(); prob.release(); sumProb.release(); logProb.release(); gradient.release(); ww.release(); velocity.release(); }
/*========================================== * 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; }
/* * Generates a random directed binary graph with the given in-degree and out- * degree sequences. Returns NULL if the algorithm failed to generate a graph * satisfying the given degree sequences. */ MATRIX_T* BCT_NAMESPACE::makerandCIJdegreesfixed(const VECTOR_T* in, const VECTOR_T* out) { gsl_rng* rng = get_rng(); // n = length(in); int n = length(in); // k = sum(in); int k = sum(in); // inInv = zeros(k,1); VECTOR_T* inInv = zeros_vector(k); // outInv = inInv; VECTOR_T* outInv = copy(inInv); // iIn = 1; iOut = 1; int iIn = 0; int iOut = 0; // for i = 1:n for (int i = 0; i < n; i++) { // inInv(iIn:iIn+in(i) - 1) = i; VECTOR_T* inInv_ind = sequence(iIn, iIn + (int)VECTOR_ID(get)(in, i) - 1); if (inInv_ind != NULL) { ordinal_index_assign(inInv, inInv_ind, (FP_T)i); VECTOR_ID(free)(inInv_ind); } // outInv(iOut:iOut+out(i) - 1) = i; VECTOR_T* outInv_ind = sequence(iOut, iOut + (int)VECTOR_ID(get)(out, i) - 1); if (outInv_ind != NULL) { ordinal_index_assign(outInv, outInv_ind, (FP_T)i); VECTOR_ID(free)(outInv_ind); } // iIn = iIn+in(i); iIn += (int)VECTOR_ID(get)(in, i); // iOut = iOut+out(i); iOut += (int)VECTOR_ID(get)(out, i); } // cij = eye(n); MATRIX_T* cij = eye(n); // edges = [outInv(1:k)'; inInv(randperm(k))']; VECTOR_T* outInv_ind = sequence(0, k - 1); VECTOR_T* edges_row_0 = ordinal_index(outInv, outInv_ind); VECTOR_ID(free)(outInv); VECTOR_ID(free)(outInv_ind); gsl_permutation* inInv_ind = randperm(k); VECTOR_T* edges_row_1 = permute(inInv_ind, inInv); gsl_permutation_free(inInv_ind); VECTOR_ID(free)(inInv); MATRIX_T* edges = concatenate_columns(edges_row_0, edges_row_1); VECTOR_ID(free)(edges_row_0); VECTOR_ID(free)(edges_row_1); bool flag = true; // for i = 1:k for (int i = 0; i < k && flag; i++) { // if cij(edges(1,i),edges(2,i)), if (fp_nonzero(MATRIX_ID(get)(cij, (int)MATRIX_ID(get)(edges, 0, i), (int)MATRIX_ID(get)(edges, 1, i)))) { // warningCounter = 1; int warningCounter = 1; // while (1) while (true) { // switchTo = ceil(k*rand); int switchTo = (int)std::ceil((k - 1) * gsl_rng_uniform(rng)); // if ~(cij(edges(1,i),edges(2,switchTo)) || cij(edges(1,switchTo),edges(2,i))), if (!(fp_nonzero(MATRIX_ID(get)(cij, (int)MATRIX_ID(get)(edges, 0, i), (int)MATRIX_ID(get)(edges, 1, switchTo))) || fp_nonzero(MATRIX_ID(get)(cij, (int)MATRIX_ID(get)(edges, 0, switchTo), (int)MATRIX_ID(get)(edges, 1, i))))) { // cij(edges(1,i),edges(2,switchTo)) = 1; MATRIX_ID(set)(cij, (int)MATRIX_ID(get)(edges, 0, i), (int)MATRIX_ID(get)(edges, 1, switchTo), 1.0); // if switchTo < i, if (switchTo < i) { // cij(edges(1,switchTo),edges(2,switchTo)) = 0; MATRIX_ID(set)(cij, (int)MATRIX_ID(get)(edges, 0, switchTo), (int)MATRIX_ID(get)(edges, 1, switchTo), 0.0); // cij(edges(1,switchTo),edges(2,i)) = 1; MATRIX_ID(set)(cij, (int)MATRIX_ID(get)(edges, 0, switchTo), (int)MATRIX_ID(get)(edges, 1, i), 1.0); } // temp = edges(2,i); FP_T temp = MATRIX_ID(get)(edges, 1, i); // edges(2,i) = edges(2,switchTo); MATRIX_ID(set)(edges, 1, i, MATRIX_ID(get)(edges, 1, switchTo)); // edges(2,switchTo) = temp; MATRIX_ID(set)(edges, 1, switchTo, temp); // break break; } // warningCounter = warningCounter+1; warningCounter++; // if warningCounter == 2*k^2 if (warningCounter == 2 * k * k) { // flag = 0; flag = false; // return; break; } } } else { // cij(edges(1,i),edges(2,i)) = 1; MATRIX_ID(set)(cij, (int)MATRIX_ID(get)(edges, 0, i), (int)MATRIX_ID(get)(edges, 1, i), 1.0); } } MATRIX_ID(free)(edges); // flag = 1; if (!flag) { MATRIX_ID(free)(cij); return NULL; } // cij = cij - eye(n); MATRIX_T* eye_n = eye(n); MATRIX_ID(sub)(cij, eye_n); MATRIX_ID(free)(eye_n); return cij; }
void updateZ(char OutputFile[40], float zm[MAXV][MAXC], float zma[MAXV][MAXC], float ma[MAXC], bool A[MAXV][MAXV], int Zloops, int C, int V, int Vtoupdate, float beta, float Threshold,bool DISP_ERRORS) {int *permC, *permV, loopz, loopc, cluster, loopvertexj, loopvertexk, vertexi,vertexk, vertexj, cl, i, j,k, errors, tmpi, tmpj; float meanfield0,meanfield1,logfn1,logfn0,loglik1,loglik0,tmp, copybeta, dellog; float Mold[MAXV][MAXV], zc[MAXV], Mk[MAXV]; static bool AA[MAXV][MAXV]; FILE *fpLinkFile, *fpOutputFile, *fpInitialFile; for (loopz=0;loopz<Zloops;loopz++) { for (vertexi=0;vertexi<V;vertexi++) {for (vertexj=0;vertexj<V;vertexj++) { Mold[vertexi][vertexj]=0.0; for (cl=0;cl<C;cl++){ Mold[vertexi][vertexj]+=zma[vertexi][cl]*zma[vertexj][cl];} } } permC=randperm(C); for (loopc=0;loopc<C;loopc++) {cluster=permC[loopc]; for (vertexi=0;vertexi<V;vertexi++) {zc[vertexi]=zma[vertexi][cluster];} permV=randperm(V); for (loopvertexk=0;loopvertexk<Vtoupdate;loopvertexk++) {vertexk=permV[loopvertexk]; for (vertexi=0;vertexi<V;vertexi++) {Mk[vertexi]=Mold[vertexi][vertexk]-zc[vertexi]*zc[vertexk]+zma[vertexi][cluster]*zma[vertexk][cluster];} logfn1=0.0;logfn0=0.0; for (vertexj=0;vertexj<V;vertexj++) {meanfield0=Mk[vertexj]-zma[vertexj][cluster]*zma[vertexk][cluster]-0.5; meanfield1=zma[vertexj][cluster]+meanfield0; if (A[vertexj][vertexk]==1) {logfn1+=logsigma(meanfield1,beta,0.0); logfn0+= logsigma(meanfield0,beta,0.0); } else {logfn1+= logsigma(-meanfield1,beta,0.0); logfn0+= logsigma(-meanfield0,beta,0.0); } } dellog=2*(logfn0-logfn1); zm[vertexk][cluster]=1.0/(1.0+exp(dellog)); zma[vertexk][cluster]=zm[vertexk][cluster]*ma[cluster]; } for (vertexi=0;vertexi<V;vertexi++) {for (vertexj=0;vertexj<V;vertexj++) {Mold[vertexi][vertexj]+=-zc[vertexi]*zc[vertexj]+zma[vertexi][cluster]*zma[vertexj][cluster]; } } } printf("\n C[%d] Zloop[%d] beta[%4.2f]",C,loopz,beta); /* compute the errors */ if (DISP_ERRORS==1){ errors=0; for (i=0;i<V;i++) { for (j=0;j<i+1;j++) {tmp=0; for (k=0;k<C;k++) {tmp+=zma[i][k]*zma[j][k];} if (tmp>Threshold){AA[i][j]=1;} else {AA[i][j]=0;} AA[j][i]=AA[i][j]; if (AA[i][j]!=A[i][j]){errors+=+1;} } } printf(", errors(threshold<Z><Z'>)=%d",errors);} /* compute the errors */ if (DISP_ERRORS==1){ errors=0; for (i=0;i<V;i++) { for (j=0;j<i+1;j++) {tmp=0; for (k=0;k<C;k++) {tmp+= (zm[i][k]>Threshold)*(zm[j][k]>Threshold);} if (tmp>0){AA[i][j]=1;} else {AA[i][j]=0;} AA[j][i]=AA[i][j]; if (AA[i][j]!=A[i][j]){errors+=+1;} } } printf(", errors(threshold<Z>threshold<Z'>)=%d",errors);} /* write out the cluster matrix (in transposed form): only those clusters that are on:*/ fpOutputFile = fopen(OutputFile, "w"); if(fpOutputFile==NULL){ printf("Error: can't open OutputFile %s\n",OutputFile);} for (cluster=0;cluster<C;cluster++){ if (ma[cluster]>SMALLVALUE){ for (i=0;i<V;i++){ fprintf(fpOutputFile, "%f ", zm[i][cluster]);}} fprintf(fpOutputFile,"\n");} fclose(fpOutputFile); //printf(" written (in transposed form) on %s\n",OutputFile); } }
int main(int argc, char *argv[]) { CommandLine cl; MathRandom<MathMersenneTwister> rng; Error error; ya_check_debug(); YA_MatD input_matrix; YA_MatD output_matrix; // Parse the command line HandleArgs(cl,argc,argv,&error); string outputfile=""; if (cl.argsize(' ')>0) { load(cl.argstring(' ',0),input_matrix); if (cl.argsize(' ')>1) outputfile=cl.argstring(' ',1); } else read(cin,input_matrix); // Select rows if (cl['r']) { output_matrix=input_matrix(YA_RowI(cl.argstring('r',0)),":"); input_matrix=output_matrix; } // Select cols if (cl['c']) { output_matrix=input_matrix(":",YA_RowI(cl.argstring('c',0))); input_matrix=output_matrix; } // Reorder rows using modulus else if (cl['z']) { ya_sizet mod=cl.argint('z',0); if (mod==0) error.generate_error(0,"vm_slice","Cannot specify a mod_num of 0."); if (input_matrix.rows()%mod!=0) { error.buffer() << "When using -z, the number of rows in the matrix " << "must be evenly divisible by the mod_num."; error.addbuf(0,"vm_slice"); } YA_VecI row_order(input_matrix.rows()); ya_sizet offset=input_matrix.rows()/mod; for (ya_sizet i=0; i<input_matrix.rows(); i++) { div_t index=div(int(i),int(mod)); row_order(i)=index.quot+index.rem*offset; } output_matrix=input_matrix(row_order,":"); } else output_matrix=input_matrix; ya_sizet file_format=YA_DEFAULT_IO; if (cl['t']) file_format=YA_PRETTY_IO; if (cl['b']) file_format=YA_BINARY_IO; // Random subset if (cl['s']) { double percent=cl.argdouble('s',0); if (percent>1) error.generate_error(0,"mat_convert", "Random percentage must be between 0 and 1"); YA_RowI rand_perm(randperm(output_matrix.rows(),rng)); output_matrix=copy(output_matrix(rand_perm,":")); ya_sizet cut_frac=ya_sizet(percent*output_matrix.rows()); if (cl.argstring('s',1)!="NO_OUTPUT") save(cl.argstring('s',1), output_matrix(vmcount(cut_frac,":",output_matrix.rows()-1),":"), file_format); output_matrix=copy(output_matrix(vmcount(cut_frac),":")); } if (cl['q']) ip_transpose(output_matrix); if (outputfile=="") write(cout,output_matrix,file_format); else save(outputfile,output_matrix,file_format); return 0; }
permute_t * permute_create (permutation_perm_t permutation, model_t model, alg_state_seen_f ssf, int worker_index, void *run_ctx) { permute_t *perm = RTalign (CACHE_LINE_SIZE, sizeof(permute_t)); perm->todos = RTalign (CACHE_LINE_SIZE, sizeof(permute_todo_t[K+TODO_MAX])); perm->tosort = RTalign (CACHE_LINE_SIZE, sizeof(int[K+TODO_MAX])); perm->shift = ((double)K)/W; perm->shiftorder = (1UL<<dbs_size) / W * worker_index; perm->start_group = perm->shift * worker_index; perm->model = model; perm->state_seen = ssf; perm->por_proviso = 1; perm->permutation = permutation; perm->run_ctx = run_ctx; perm->next = state_info_create (); if (Perm_Otf == perm->permutation) perm->pad = RTalign (CACHE_LINE_SIZE, sizeof(int[K+TODO_MAX])); if (Perm_Random == perm->permutation) { perm->rand = RTalignZero (CACHE_LINE_SIZE, sizeof(int*[K+TODO_MAX])); for (size_t i = 1; i < K+TODO_MAX; i++) { perm->rand[i] = RTalign (CACHE_LINE_SIZE, sizeof(int[ i ])); randperm (perm->rand[i], i, perm->shiftorder); } } if (Perm_RR == perm->permutation) { perm->rand = RTalignZero (CACHE_LINE_SIZE, sizeof(int*)); perm->rand[0] = RTalign (CACHE_LINE_SIZE, sizeof(int[1<<RR_ARRAY_SIZE])); srandom (time(NULL) + 9876432*worker_index); for (int i =0; i < (1<<RR_ARRAY_SIZE); i++) perm->rand[0][i] = random(); } if (Perm_SR == perm->permutation || Perm_Dynamic == perm->permutation) { perm->rand = RTalignZero (CACHE_LINE_SIZE, sizeof(int*)); perm->rand[0] = RTalign (CACHE_LINE_SIZE, sizeof(int[K+TODO_MAX])); randperm (perm->rand[0], K+TODO_MAX, (time(NULL) + 9876*worker_index)); } perm->labels = lts_type_get_edge_label_count (GBgetLTStype(model)); for (size_t i = 0; i < K+TODO_MAX; i++) { if (act_detect || files[1] || (PINS_BUCHI_TYPE == PINS_BUCHI_TYPE_TGBA)) { perm->todos[i].ti.labels = RTmalloc (sizeof(int*[perm->labels])); } else { perm->todos[i].ti.labels = NULL; } } perm->class_label = lts_type_find_edge_label (GBgetLTStype(model),LTSMIN_EDGE_TYPE_ACTION_CLASS); if (inhibit){ int id=GBgetMatrixID(model,"inhibit"); if (id>=0){ perm->inhibit_matrix = GBgetMatrix (model, id); Warning(infoLong,"inhibit matrix is:"); if (log_active(infoLong)) dm_print (stderr, perm->inhibit_matrix); perm->inhibited_by = (ci_list **)dm_cols_to_idx_table (perm->inhibit_matrix); } else { Warning(infoLong,"no inhibit matrix"); } id = GBgetMatrixID(model,LTSMIN_EDGE_TYPE_ACTION_CLASS); if (id>=0){ perm->class_matrix=GBgetMatrix(model,id); Warning(infoLong,"inhibit class matrix is:"); if (log_active(infoLong)) dm_print(stderr,perm->class_matrix); } else { Warning(infoLong,"no inhibit class matrix"); } if (perm->class_label>=0) { Warning(infoLong,"inhibit class label is %d",perm->class_label); } else { Warning(infoLong,"no inhibit class label"); } } if (PINS_POR) por_set_find_state (state_find, perm); return perm; }
int main() { //declare function variables int i,k,j,l; int n=0; int pass=0; double numArray[]; int minout[ARRAYLEN]; int maxout[ARRAYLEN]; double meanout[ARRAYLEN]; double ntimesn[ARRAYLEN]; double nfacout[ARRAYLEN]; double mean=0; double sizesquare=0; double nfactorial, nsq; //seed the generator srandom(RNG_SEED); //introduction message printf("Project 1, Created by: Alec Webb, Description: Solves the nqueens problem using permutations\n"); printf("---------------------------------------------------------------\n"); for(j=4; j<=ARRAYLEN; j++){ //start n loop int solutionFound=0; //used to determine first run or not int nCounter=0; //used to count number of runs for 10 solutions int pncount=0; //used to calulate current number of runs for max min functions int current=0; //for max min functions int minimum=0; //for max min comparisons int maximum=0; //for max min comparisons n=j; //set size of array nsq=j; //set size for calculations for(k=0; k<LOOPTIME; k++){ //find 10 solutions pass=0; //to continue after a first match is found pncount = nCounter; //for maxmin calc while(pass==0) { InitializeArray(numArray, n); //call initialize method randperm(numArray, n); //call randperm function pass = checkboard(numArray, n); //check the permutation nCounter++; //total# of runs } current = nCounter-pncount; //for max min,current run time if(pncount==0) //to account for a run that has not occured { //avoids min being set to 0 minimum = maximum = current; } else { minimum = min(minimum, current); //compare maximum = max(maximum, current); //compare } solutionFound++; //indicate first solution has been found if(solutionFound <=1) //handles the first and only first solution { displayboard(numArray, n); //print the solution found printf("---------------------------------------------------------------\n"); printf(" "); for(i=0; i<n; i++){ //print the numerical solution found printf("%-3.0lf", numArray[i]); } printf(": Solution for board size %d\n", n); printf("---------------------------------------------------------------\n"); } } //calculate and store relevent values mean = nCounter/10; //determine mean sizesquare = nsquared(nsq,n); //determine n^n nfactorial = factorial(n); //determine n! //stores the values for printing minout[j-4] = minimum; maxout[j-4] = maximum; meanout[j-4] = mean; ntimesn[j-4] = sizesquare; nfacout[j-4] = nfactorial; }//end n loop //the following prints out the results of the 10 runs for each n printf("size min max mean size**size size!\n"); printf("---------------------------------------------------------------\n"); for(l=0; l<NUMBEROFBOARDS; l++) { printf("%4d %8d %10d %12.1e %12.1e %12.1e\n", (l+4), minout[l], maxout[l], meanout[l], ntimesn[l], nfacout[l]); } return 0; }//end main
point TargetControl::genTarget(point currentPos, bool directionMatters) { double theta; point targetPos; if(!directionMatters) //Direction not constrained this trial { do { theta=randb(0,360); targetPos=currentPos+point(cos(theta),sin(theta))*spawnDist; } while(targetPos.mag()>innerRadius); return targetPos; } //If direction predetermined, use it if (nextDirection>=0) {targetPos=currentPos+point(cos(nextDirection),sin(nextDirection))*spawnDist; nextDirection=-1; return targetPos;} //Make sure we haven't bottomed out int sum=0; for(int k=0;k<directions;k++) { sum+=((timesUsed[k]>=minUses)?0:1); } if (sum==0) minUses++; //Find the best direction double min=std::numeric_limits<double>::infinity(); int index=-1; double mag; point bestPos; for(int k=0;k<directions;k++) { targetPos=currentPos+point(cos(direction[k]),sin(direction[k]))*spawnDist; mag=targetPos.mag(); if((mag<min)&&(timesUsed[k]<minUses)&&(mag<innerRadius)) {min=mag; index=k; bestPos=targetPos;} } if(index != -1) { timesUsed[index]++; return bestPos; } //If no direction can produce a legal target pick randomly among the least used and set up for it int m=INT_MAX; int i; int * shuffle = new int[directions]; randperm(shuffle,directions); for(int k=0;k<directions;k++) if(timesUsed[shuffle[k]]<m) {m=timesUsed[shuffle[k]]; i=shuffle[k];} timesUsed[i]++; nextDirection=direction[i]; theta=atan2(currentPos.Y()+spawnDist*sin(direction[i]),currentPos.X()+spawnDist*cos(direction[i]))-3.14159265; targetPos=currentPos - point(cos(theta),sin(theta))*spawnDist; if (targetPos.mag()>innerRadius) { point V=point(spawnDist*cos(direction[i]),spawnDist*sin(direction[i])); point center=currentPos+V; double distance=center.mag(); double xi=(pow(distance,2)+pow(innerRadius,2)-pow(spawnDist,2))/(2*distance); double yi=sqrt(pow(innerRadius,2)-pow(xi,2)); point i1=center*(xi/distance)+point(-center.Y(),center.X())*(yi/distance); point i2=center*(xi/distance)-point(-center.Y(),center.X())*(yi/distance); double angle1=atan2(i1.Y()-V.Y()-currentPos.Y(),i1.X()-V.X()-currentPos.X()); double angle2=atan2(i2.Y()-V.Y()-currentPos.Y(),i2.X()-V.X()-currentPos.X()); point p1p=currentPos+point(cos(angle1),sin(angle1))*spawnDist; point p1m=currentPos+point(cos(angle2),sin(angle2))*spawnDist; if (p1p.mag()>p1m.mag()) targetPos=p1m; else targetPos=p1p; } return (targetPos.mag()<=maxRadius)?targetPos:point(0,0); }
int main(int argc, char* argv[]) { // Generate data from a sine vec X = linspace(0, 2*pi, 200); vec Y = sin(X); // True function // Randomly select a subset of the data as observations, // and add white noise ivec Iobs = randperm(NUM_X).left(NUM_OBS); vec Xobs = X(Iobs); mat XobsMat = mat(Xobs); // PSGP requires a matrix of inputs (not a vector) vec Yobs = Y(Iobs) + sqrt(NOISE_VAR)*randn(NUM_OBS); // Noisy observations // Covariance function: Gaussian + Nugget double range = 0.5; // Initial range double sill = 1; // Initial sill double nugget = NOISE_VAR; // Nugget variance GaussianCF gaussianCovFunc(range, sill); WhiteNoiseCF nuggetCovFunc(nugget); SumCF covFunc; covFunc.add(gaussianCovFunc); covFunc.add(nuggetCovFunc); // Initialise psgp PSGP psgp(XobsMat, Yobs, covFunc, NUM_ACTIVE); // Use a Gaussian likelihood model with fixed variance GaussianLikelihood gaussLik(nugget); // Compute the posterior (first guess), which projects the whole observed data // onto a subset of optimal active points. psgp.computePosterior(gaussLik); // Estimate parameters using Scaled Conjugate Gradients SCGModelTrainer scg(psgp); scg.Train(10); // Recompute the posterior and active points for the new parameters psgp.resetPosterior(); psgp.computePosterior(gaussLik); // Make predictions - we use the Gaussian covariance function only // to make non-noisy prediction (no nugget) vec psgpmean = zeros(NUM_X); vec psgpvar = zeros(NUM_X); psgp.makePredictions(psgpmean, psgpvar, mat(X), gaussianCovFunc); // Plot results GraphPlotter gplot = GraphPlotter(); // Plot psgp mean and variance gplot.plotPoints(X, psgpmean, "psgp mean", LINE, BLUE); gplot.plotPoints(X, psgpmean + 2.0*sqrt(psgpvar), "error bar", LINE, CYAN); gplot.plotPoints(X, psgpmean - 2.0*sqrt(psgpvar), "error bar", LINE, CYAN); // Plot true function and observations gplot.plotPoints(X, Y, "true function", LINE, RED); gplot.plotPoints(Xobs, Yobs, "observations", CROSS, BLACK); // Plot active points vec activeX = (psgp.getActiveSetLocations()).get_col(0); vec activeY = Yobs(psgp.getActiveSetIndices()); gplot.plotPoints(activeX, activeY, "active points", CIRCLE, BLUE); return 0; }
int main(int argc, char *argv[]) { train_info_t train; sm_info_t sm; data_info_t data; //dtr: datatrain, dtt: datatest classify_t clssfy; char out[MAX_BUF]; parse_command_line(argc, argv, &train, &sm, &data, &clssfy, out); int train_numcase = train.mininumcase * train.nummix; double *H = (double*) malloc(train_numcase * sm.numhid * sizeof(double)); double *init = (double *) malloc(sm.numhid * sizeof(double)); int *V = (int*) malloc(train_numcase * sm.numvis * sizeof(int)); int *order = malloc(sizeof(int) * (train_numcase > data.numcase ? train_numcase : data.numcase)); if (!(H && V && order && init)) { fprintf(stderr, "malloc error!\n"); exit(0); } int nh; srand(time(NULL)); for (nh = 0; nh < sm.numhid; nh++) { init[nh] = (double)rand() / RAND_MAX > 0.5 ? 1.0 : 0.0; } int iter, curbatch = 0; int totalbatch = data.numcase / train.mininumcase; long minibatchlength = (long)train.mininumcase * data.channelcase * data.numchannel; double cost; //init_hid_nag_ip_struct(clssfy.lencase, clssfy.numclass - 1); for (iter = 0; iter < train.iteration; iter++) { if (curbatch >= totalbatch) curbatch = 0; if (curbatch == 0) { randperm(order, data.numcase); reorder(data.data, sizeof(uint8_t), order, data.numcase, data.numchannel * data.channelcase); reorder(data.labels, sizeof(int), order, data.numcase, 1); } uint8_t *unlbl = data.data + curbatch * minibatchlength; fprintf(stdout, "**** iter %d, loc %ld, current data pointer %p ****\n", iter, curbatch * minibatchlength, unlbl); data2V(V, sm.numclass, unlbl, train.mininumcase, data.channelcase, data.numchannel, sm.position, sm.numvis, train.mix, train.nummix); //_labels2clssfy(&clssfy, data.labels + curbatch * train.mininumcase, train.mininumcase, train.nummix); //classify_get_hid(sm.w, sm.bh, V, H, sm.numvis, sm.numhid, sm.numclass, train_numcase, &clssfy); sm_hid(&sm, V, H, train_numcase, init); int xx, yy; for (xx = 0; xx < 1; xx++) { for (yy=0; yy < sm.numhid; yy++) fprintf(stdout, "%d", (int)(H[xx * sm.numhid + yy])); fprintf(stdout, "\n"); } randperm(order, train_numcase); reorder(V, sizeof(int), order, train_numcase, sm.numvis); reorder(H, sizeof(double), order, train_numcase, sm.numhid); cost = sm_train(&sm, V, H, train_numcase, train.batchsize); curbatch++; } out_w(out, &sm); free_hid_nag_ip_struct(); free(init); free(V); free(H); free(order); return 0; }