VectorXd batch_grad_desc(Vector4d beta_init, VectorXd x, VectorXd y, double step_size, int MaxIter, double Facttol){ int i; Vector4d beta, beta_grad; double cost, cost_; beta = beta_init; if (debug) { std::cout << beta << std::endl; cin.get(); } cost = compute_cost(beta, x, y); for (i = 0; i < MaxIter; i++) { cost_ = cost; beta_grad = compute_grad(beta, x, y); beta -= (beta_grad.array() * step_size).matrix(); cost = compute_cost(beta, x, y); std::cout << "The cost is: " << cost << std::endl; if (abs(cost-cost_)/max(max(1.0, cost_), cost) < Facttol) { if (debug) { } std::cout << "Gradient Descent Converges!" << std::endl; break; } } return beta; }
void gradient_check() { std::vector<int> const Labels{0, 1, 2, 0}; auto const UniqueLabels = get_unique_labels(Labels); auto const NumClass = UniqueLabels.size(); EigenMat const Train = EigenMat::Random(10, 2); weight_ = EigenMat::Random(NumClass, Train.rows()); grad_ = EigenMat::Zero(NumClass, Train.rows()); int const TrainCols = static_cast<int>(Train.cols()); EigenMat const GroundTruth = get_ground_truth(NumClass, TrainCols, UniqueLabels, Labels); gradient_checking gc; auto func = [&](EigenMat &theta)->double { return compute_cost(Train, theta, GroundTruth); }; EigenMat const WeightBuffer = weight_; EigenMat const Gradient = gc.compute_gradient(weight_, func); compute_cost(Train, WeightBuffer, GroundTruth); compute_gradient(Train, WeightBuffer, GroundTruth); std::cout<<std::boolalpha<<"gradient checking pass : "******"\n";//*/ }
void cluster_document::kmedoids(int clusters, std::vector<cluster_document> &document_list) { int length = document_list.size(); std::vector<int> medoid_ids = random_subset(clusters, length); recompute_clusters(medoid_ids, document_list); int steps = KMEDOIDS_STEPS; double cost = compute_cost(medoid_ids, document_list); while (steps-- > 0) { //std::cerr << steps << std::endl; int best_document = -1; double best_cost = cost; for (auto &document: document_list) { int old_medoid = medoid_ids[document.cluster_]; if (old_medoid == document.id_) { continue; } medoid_ids[document.cluster_] = document.id_; double new_cost = compute_cost(medoid_ids, document_list); if (new_cost < best_cost) { cost = best_cost = new_cost; best_document = document.id_; } medoid_ids[document.cluster_] = old_medoid; } if (best_document == -1) { break; } auto &document = document_list[best_document]; medoid_ids[document.cluster_] = document.id_; recompute_clusters(medoid_ids, document_list); } }
// assign value to r, representing some penalty for assigning one // color to all pixels in this subtree void compute_cost(node *r) { //initial small value, so that all nodes have >0 cost r->reduce_cost = r->pixel_count/1000.0; if (r->children_count==0) { return; } // mean color of all pixels in subtree double mean_r = r->reds / r->pixel_count; double mean_g = r->greens / r->pixel_count; double mean_b = r->blues / r->pixel_count; double mean_a = r->alphas / r->pixel_count; for (unsigned idx=0; idx < 16; ++idx) { if (r->children_[idx] != 0) { double dr,dg,db,da; compute_cost(r->children_[idx]); // include childrens penalty r->reduce_cost += r->children_[idx]->reduce_cost; // difference between mean value and subtree mean value dr = r->children_[idx]->reds / r->children_[idx]->pixel_count - mean_r; dg = r->children_[idx]->greens / r->children_[idx]->pixel_count - mean_g; db = r->children_[idx]->blues / r->children_[idx]->pixel_count - mean_b; da = r->children_[idx]->alphas / r->children_[idx]->pixel_count - mean_a; // penalty_x = d_x^2 * pixel_count * mean_alfa/255, where x=r,g,b,a // mean_alpha/255 because more opaque color = more noticable differences r->reduce_cost += (dr*dr + dg*dg + db*db + da*da) * r->children_[idx]->alphas / 255; } } }
static void construct_edges(carmen_roadmap_t *roadmap, int id) { carmen_roadmap_vertex_t *node_list; carmen_roadmap_edge_t *edges; int i, j; int length; double cost = 0; node_list = (carmen_roadmap_vertex_t *)(roadmap->nodes->list); assert(node_list[id].edges->length == 0); for (i = 0; i < roadmap->nodes->length; i++) { if (i == id) continue; if (hypot(node_list[i].x - node_list[id].x, node_list[i].y - node_list[id].y) < 50) { cost = 1e6; edges = (carmen_roadmap_edge_t *)(node_list[i].edges->list); length = node_list[i].edges->length; for (j = 0; j < length; j++) { if (edges[j].id == id) { cost = edges[j].cost; break; } } if (j == length) cost = compute_cost(node_list+i, node_list+id, roadmap->c_space); if (cost < 1e5) add_edge(node_list+i, node_list+id, cost); } } assert(node_list[id].edges->length > 0); }
int check_data(timing_data* data, key_data * key){ unsigned char key_guess[KEY_LENGTH]; int i; compute_cost(data, key); compute_rank_table(data, key); first_guess(data, key_guess); printf("\nInitial guess: "); for(i = 0; i < KEY_LENGTH; i++) printf(" %02x ", key_guess[i]); walk(data, key_guess); printf("Walk : "); if(check_key(key_guess, data, key) ) return 1; for(i = 0; i < KEY_LENGTH; i++) printf(" %02x ", key_guess[i]); if(check_key(key_guess, data, key) ) return 1; return 0; }
int verified_tolling(struct pathdb* db, VerifierSideIn* input, struct Out* output){ output->cost = compute_cost(db); //Enforcement - make sure all spotchecks are in there int failed_spotchecks = 0; int i; for(i = 0; i < MAX_SPOTCHECKS; i++){ //Find a match int found = 0; int j; //THIS IS SLOW. TODO: permutation network implementation. tuple_t* s = &(input->spotchecks[i]); for(j = 0; j < MAX_TUPLES; j++){ tuple_t* v = &(db->path[j]); if (close_match(s,v,input->time_threshold)){ found = 1; } } if (!found){ failed_spotchecks = 1; } } output->rejected = failed_spotchecks; if (failed_spotchecks){ output->cost = 0; //Prevent fishing attack } return 0; }
void gradient_descent(double X[][MAX_FEATURE_DIMENSION], int y[], double theta[], int feature_number, int sample_number, double alpha, int iterate_number, double J[]){ for (int i = 0; i < iterate_number; i++){ double temp[MAX_FEATURE_DIMENSION] = {0}; for (int j = 0; j <= feature_number; j++){ temp[j] = theta[j] - alpha * compute_gradient(X, y, theta, feature_number, j, sample_number); } for (int j = 0; j <= feature_number; j++){ theta[j] = temp[j]; } J[i] = compute_cost(X, y, theta, feature_number, sample_number); } }
// starting from root_, unfold nodes with biggest penalty // until all available colors are assigned to processed nodes void assign_node_colors() { compute_cost(root_.get()); int tries = 0; // at the begining, single color assigned to root_ colors_ = 1; root_->count = root_->pixel_count; std::set<node*,node_rev_cmp> colored_leaves_heap; colored_leaves_heap.insert(root_.get()); while((!colored_leaves_heap.empty() && (colors_ < max_colors_) && (tries < 16))) { // select worst node to remove it from palette and replace with children node * cur_node = *colored_leaves_heap.begin(); colored_leaves_heap.erase(colored_leaves_heap.begin()); if (((cur_node->children_count + colors_ - 1) > max_colors_)) { tries++; continue; // try few times, maybe next will have less children } tries = 0; // ignore leaves and also nodes with small mean error and not excessive number of pixels if (cur_node->pixel_count > 0 && (cur_node->children_count > 0) && (((cur_node->reduce_cost / cur_node->pixel_count + 1) * std::log(double(cur_node->pixel_count))) > 15) ) { colors_--; cur_node->count = 0; for (unsigned idx=0; idx < 16; ++idx) { if (cur_node->children_[idx] != 0) { node *n = cur_node->children_[idx]; n->count = n->pixel_count; colored_leaves_heap.insert(n); colors_++; } } } } }
int main(int argc, char** argv) { if (argc != 3) { cerr << "incorrect usage\n"; return -1; } string file1(argv[1]); string file2(argv[2]); ifstream fin1(file1); ifstream fin2(file2); Contour c1, c2, actual_c1, actual_c2; // We need to limit contour size to 100 or so. int x,y; while (fin1 >> x >> y) { c1.push_back(make_pair((double)x,(double)y)); } int inc = c1.size()/75; if (inc == 0) inc = 1; for (int i=0;i<c1.size();i+=inc) actual_c1.push_back(c1[i]); while (fin2 >> x >> y) { c2.push_back(make_pair((double)x,(double)y)); } inc = c2.size()/75; if (inc == 0) inc = 1; for (int i=0;i<c2.size();i+=inc) actual_c2.push_back(c2[i]); fin1.close(); fin2.close(); compute_cost(actual_c1, actual_c2); //cout << setprecision(17) << compute_cost(actual_c1,actual_c2) << endl; // cout << "closed: " << // setprecision(17) << compute_cost_closed(actual_c1, actual_c2) << endl; return 0; }
void softmax<T>::train(const Eigen::Ref<const EigenMat> &train, const std::vector<int> &labels) { #ifdef OCV_TEST_SOFTMAX gradient_check(); #endif auto const UniqueLabels = get_unique_labels(labels); auto const NumClass = UniqueLabels.size(); weight_ = EigenMat::Random(NumClass, train.rows()); grad_ = EigenMat::Zero(NumClass, train.rows()); auto const TrainCols = static_cast<int>(train.cols()); EigenMat const GroundTruth = get_ground_truth(static_cast<int>(NumClass), TrainCols, UniqueLabels, labels); std::random_device rd; std::default_random_engine re(rd()); int const Batch = (get_batch_size(TrainCols)); int const RandomSize = TrainCols != Batch ? TrainCols - Batch - 1 : 0; std::uniform_int_distribution<int> uni_int(0, RandomSize); for(size_t i = 0; i != params_.max_iter_; ++i){ auto const Cols = uni_int(re); auto const &TrainBlock = train.block(0, Cols, train.rows(), Batch); auto const >Block = GroundTruth.block(0, Cols, NumClass, Batch); auto const Cost = compute_cost(TrainBlock, weight_, GTBlock); if(std::abs(params_.cost_ - Cost) < params_.epsillon_ || Cost < 0){ break; } params_.cost_ = Cost; compute_gradient(TrainBlock, weight_, GTBlock); weight_.array() -= grad_.array() * params_.lrate_;//*/ } }
virtual double compute_cost( std::list<double3d_ptr> outputs, std::list<double3d_ptr> labels, std::list<bool3d_ptr> masks ) { return compute_cost(outputs, labels, masks, true, true); }
double HDP_MEDOIDS (vector<Instance*>& data, vector< vector<double> >& means, Lookups* tables, vector<double> lambdas, dist_func df, int FIX_DIM) { // STEP ZERO: validate input and initialization int N = tables->nWords; int D = tables->nDocs; vector< pair<int, int> > doc_lookup = *(tables->doc_lookup); double lambda_global = lambdas[0]; double lambda_local = lambdas[1]; vector< vector<double> > global_means (1, vector<double>(FIX_DIM, 0.0)); vector< vector<int> > k (D, vector<int>(1,0)); // global association vector<int> z (N, 0); // local assignment vector<int> global_asgn (N, 0); // global assignment // STEP ONE: a. set initial *global* medoid as global mean compute_assignment (global_asgn, k, z, tables); compute_means (data, global_asgn, FIX_DIM, global_means); double last_cost = compute_cost (data, global_means, k, z, lambdas, tables, df, FIX_DIM); double new_cost = last_cost; while (true) { // 4. for each point x_ij, for (int j = 0; j < D; j ++) { for (int i = doc_lookup[j].first; i < doc_lookup[j].second; i++) { int num_global_means = global_means.size(); vector<double> d_ij (num_global_means, 0.0); for (int p = 0; p < num_global_means; p ++) { Instance* temp_ins = vec2ins(global_means[p]); double euc_dist = df(data[i], temp_ins, FIX_DIM); d_ij[p] = euc_dist * euc_dist; delete temp_ins; } set<int> temp; for (int p = 0; p < num_global_means; p ++) temp.insert(p); int num_local_means = k[j].size(); for (int q = 0; q < num_local_means; q ++) temp.erase(k[j][q]); set<int>::iterator it; for (it=temp.begin(); it!=temp.end();++it) d_ij[*it] += lambda_local; int min_p = -1; double min_dij = INF; for (int p = 0; p < num_global_means; p ++) if (d_ij[p] < min_dij) { min_p = p; min_dij = d_ij[p]; } if (min_dij > lambda_global + lambda_local) { z[i] = num_local_means; k[j].push_back(num_global_means); vector<double> new_g(FIX_DIM, 0.0); for (int f = 0; f < data[i]->fea.size(); f++) new_g[data[i]->fea[f].first-1] = data[i]->fea[f].second; global_means.push_back(new_g); // cout << "global and local increment" << endl; } else { bool c_exist = false; for (int c = 0; c < num_local_means; c ++) if (k[j][c] == min_p) { z[i] = c; c_exist = true; break; } if (!c_exist) { z[i] = num_local_means; k[j].push_back(min_p); // cout << "local increment" << endl; } } } } /* cout << "half..........." << endl; cout << "#global created: " << global_means.size() << ", #global used: " << get_num_global_means(k); */ new_cost = compute_cost (data, global_means, k, z, lambdas, tables, df, FIX_DIM); // 5. for all local clusters, for (int j = 0; j < D; j ++) { int begin_i = doc_lookup[j].first; int end_i = doc_lookup[j].second; int doc_len = doc_lookup[j].second - doc_lookup[j].first; int num_local_means = k[j].size(); // all local clusters are distinct to each other /* set<int> temp; for (int y = 0; y < num_local_means; y++) temp.insert(k[j][y]); cout << temp.size() << " ==? " << num_local_means << endl; assert (temp.size() == num_local_means); */ // compute means of local clusters vector< vector<double> > local_means (num_local_means, vector<double>(FIX_DIM, 0.0)); vector<int> local_asgn (z.begin()+begin_i, z.begin()+end_i); vector<Instance*> local_data (data.begin()+begin_i,data.begin()+end_i); compute_means (local_data, local_asgn, FIX_DIM, local_means); assert (num_local_means == local_means.size()); // pre-compute instances for global means int num_global_means = global_means.size(); vector<Instance*> temp_global_means (num_global_means, NULL); for (int p = 0; p < num_global_means; p ++) temp_global_means[p] = vec2ins (global_means[p]); // pre-compute instances for local means vector<Instance*> temp_local_means (num_local_means, NULL); for (int c = 0; c < num_local_means; c ++) temp_local_means[c] = vec2ins (local_means[c]); for (int c = 0; c < num_local_means; c++) { // compute distance of local clusters to each global cluster num_global_means = global_means.size(); vector<double> d_jcp (num_global_means, 0.0); double sum_d_ijc = 0.0; for (int i = doc_lookup[j].first; i < doc_lookup[j].second; i ++) { if (z[i] != c) continue; double local_dist = df (data[i], temp_local_means[c], FIX_DIM); sum_d_ijc += local_dist * local_dist; for (int p = 0; p < num_global_means; p ++) { double dist = df (data[i], temp_global_means[p], FIX_DIM); d_jcp[p] += dist * dist; } } int min_p = -1; double min_d_jcp = INF; for (int p = 0; p < num_global_means; p ++) if (d_jcp[p] < min_d_jcp) { min_p = p; min_d_jcp = d_jcp[p]; } assert (min_p >= 0); // cout << min_d_jcp << " " << lambda_global << " " << sum_d_ijc << endl; if (min_d_jcp > lambda_global + sum_d_ijc) { global_means.push_back(local_means[c]); // push mu_jc temp_global_means.push_back(vec2ins (local_means[c])); k[j][c] = num_global_means; // cout << "global increment" << endl; } else { k[j][c] = min_p; } } for (int c = 0; c < num_local_means; c ++) delete temp_local_means[c]; num_global_means = global_means.size(); for (int p = 0; p < num_global_means; p ++) delete temp_global_means[p]; } // 6. for each global clusters, compute_assignment (global_asgn, k, z, tables); /* cout << "compute global means.." << endl; cout << "#global created: " << global_means.size() << ", #global used: " << get_num_global_means(k); */ compute_means (data, global_asgn, FIX_DIM, global_means); // 7. convergence? new_cost = compute_cost (data, global_means, k, z, lambdas, tables, df, FIX_DIM); if ( new_cost < objmin ) objmin = new_cost; objmin_trace << omp_get_wtime()-start_time << " " << objmin << endl; if (new_cost == last_cost) break; if (new_cost < last_cost) { last_cost = new_cost; } else { cerr << "failure" << endl; return INF; assert(false); } } means = global_means; return last_cost; }// entry main function
void compute_estimate() { this->cost = compute_cost(); }