double predict(const std::unordered_map<double, std::vector<double> > &model, const std::unordered_set<double> &label_set, const std::vector<double> &feats) { // binary classification if (model.size() == 1) { std::unordered_set<double>::const_iterator lit = label_set.begin(); const double label1 = *(lit++); const double label2 = *lit; const std::unordered_map<double, std::vector<double> >::const_iterator mit = model.find(label1); assert(mit != model.end()); return hypothesis(mit->second, feats) > 0.5 ? label1 : label2; } double max_hyp = -1; double max_label = -1; for (std::unordered_map<double, std::vector<double> >::const_iterator it = model.begin(); it != model.end(); ++it) { double hyp = hypothesis(it->second, feats); if (hyp > max_hyp) { max_hyp = hyp; max_label = it->first; } } assert(max_hyp != -1); assert(max_label != -1); return max_label; }
double compute_gradient(double X[][MAX_FEATURE_DIMENSION], int y[], double theta[], int feature_number, int feature_pos, int sample_number){ double sum = 0; for (int i = 0; i < sample_number; i++){ double h = hypothesis(X[i], theta, feature_number); sum += (h - y[i]) * X[i][feature_pos]; } return sum/sample_number; }
double compute_cost(double X[][MAX_FEATURE_DIMENSION], int y[], double theta[], int feature_number,int sample_number){ double sum = 0; for (int i = 0; i < sample_number; i++){ double h = hypothesis(X[i], theta, feature_number); sum += -y[i] * log(h) - (1 - y[i]) * log(1 - h); } return sum/sample_number; }
double cost(std::vector<std::vector<double> >::const_iterator feats, const std::vector<double> &weights, const size_t batch_size, const size_t dimension, const double true_label) { double cost = 0; #pragma omp parallel for reduction(+ : cost) for (size_t i = 0; i < batch_size; ++i) { double label = (*(feats + i))[0] == true_label ? 1 : 0; // TODO: can this be simplified? if (dimension == 0) cost += (hypothesis(weights, *(feats + i)) - label); else cost += (hypothesis(weights, *(feats + i)) - label) * (*(feats + i))[dimension]; } return cost; }
float der_x1(float x0,float x1) { int i=0; float sum=0; for(i=0;i<m;i++) sum+=(hypothesis(trainingSet[i][0],x0,x1)-trainingSet[i][1])*trainingSet[i][0]; sum=sum/m; return sum; }
float der_x0(float x0,float x1) { int i=0; float sum=0; for(i=0;i<m;i++) sum+=hypothesis(trainingSet[i][0],x0,x1)-trainingSet[i][1]; sum=sum/m; printf("%f",sum); return sum; }
/** * @brief mhtTrackingModule::takeBestHyp Finds the best hypothesis, according to the total cost of connection. * @param hyps List of hypothesis * @param ok True if there is a best hypothesis * @return The best hypothesis */ mhtTrackingModule::hypothesis mhtTrackingModule::takeBestHyp(std::vector<hypothesis > hyps, bool ok) { int minCost = INF; int index = -1; ok = false; for(uint i=0; i<hyps.size(); i++) if(hyps.at(i).totalCost < minCost) { index = i; minCost = hyps.at(i).totalCost; } if(index>-1) { hypothesis h = hyps.at(index); hyps.erase(hyps.begin()+index); ok = true; return h; } return hypothesis(); }
void cost(){ int i; for(i=0;i<(int)total_samples;i++){ err[i]=hypothesis(x[i]) - y[i]; } }