Пример #1
0
int main(int argc, char* argv[]) {

	if(argc!=5) {

		std::cout << "You have the wrong argument count" << std::endl;
	}
	
	double C=atof(argv[1]);
	int nr_threads=atoi(argv[2]);
	char*  trainloc = argv[3];
	char*  testloc = argv[4];


	omp_set_num_threads(nr_threads);


	std::vector<int> y;
	std::vector< std::vector<double> > traindata;
	std::vector< std::vector<int> > feattrain;
	std::ifstream file;
	file.open(trainloc);
	std::string line;
	std::vector<std::string> vals;
	std::vector<std::string> vals2;
	std::vector<std::string> feat_val;
	std::vector<int> yt;
	std::vector< std::vector<double> > testdata;
	std::vector< std::vector<int> > feattest;
	std::vector<std::string> feat_valt;
	if(file.is_open()) {
	
		while(std::getline(file,line)) {
			feattrain.push_back(std::vector<int>());
			traindata.push_back(std::vector<double>());

			vals= split(line, ' ');
			y.push_back(std::stoi(vals[0]));
			for(int i=1;i<vals.size();i++) {
				feat_val = split(vals[i],':');
				(feattrain.back()).push_back(std::stoi(feat_val[0])-1);
				(traindata.back()).push_back(std::stof(feat_val[1]));

			}
				
		}
	file.close();
	std::ifstream file2;
	file2.open(testloc);
	std::string line2;
		while(std::getline(file2,line2)) {
			feattest.push_back(std::vector<int>());
			testdata.push_back(std::vector<double>());

			vals2= split(line2, ' ');
			yt.push_back(std::stoi(vals2[0]));
			for(int j=1;j<vals2.size();j++) {
				feat_valt = split(vals2[j],':');
				(feattest.back()).push_back(std::stoi(feat_valt[0])-1);
				(testdata.back()).push_back(std::stof(feat_valt[1]));

			}
				
		}
	file2.close();
	}
	else {
		std::cout << "File didn't open" << std::endl;
	}


	std::cout << "Data has been loaded" << std::endl;

	int sizet=yt.size();
	int size=y.size();
	std::vector<int> permutes(size);
	std::vector<int> used(size);

#pragma omp parallel for
	for(int i=0;i<size;i++) {
		permutes[i]=i;
	}


// permuting the vector and finding the max feature number	
	int maxn=0;
#pragma omp parallel for
	for(int t=0;t<size;t++) {
		int h=traindata[t].size();
#pragma omp parallel for
		for(int ins=0;ins<h;ins++) {
			if(feattrain[t][ins]>maxn){
				maxn=feattrain[t][ins];
			}	
		}
	}
	std::cout << "Max feature count: " << maxn << std::endl;

	std::vector<double> alpha(size), w(maxn+1);
	std::fill(alpha.begin(),alpha.end(),0);
	std::fill(w.begin(),w.end(),0);
	double delt=0;
	for(int outer=1;outer<=20;outer++){
		std::srand(std::time(0));
		std::random_shuffle(permutes.begin(),permutes.end());
		double itertime1 = omp_get_wtime();
		#pragma omp parallel for private(delt) shared(alpha,w,traindata,feattrain) schedule(dynamic) 	
		for(int iter=0;iter<size;iter++) {


			int i=permutes[iter];
			int tempsize=traindata[i].size();
			// x w inner product
			double inprodxw=0;
			double inprodxx=0;
			for(int xal=0;xal<tempsize;xal++){
				inprodxw+=traindata[i][xal]*w[feattrain[i][xal]];
				inprodxx+=traindata[i][xal]*traindata[i][xal];	
			}
			delt=(1-y[i]*inprodxw-((double)alpha[i]/(2*C)))/(y[i]*y[i]*inprodxx + ((double)1/(2*C)));
			double gz = std::max(delt,-alpha[i]);
			alpha[i]+=gz;

			for(int xal2=0;xal2<tempsize;xal2++){
				#pragma omp atomic
				w[feattrain[i][xal2]] = w[feattrain[i][xal2]] + y[i]*gz*traindata[i][xal2];
			}
}
		
		double itertime2 = omp_get_wtime() - itertime1;
		double norms = wnorm(y,alpha,traindata,maxn,w,feattrain);
		double primvals = primval(C,y,w,traindata,feattrain);
		double dualvals = dualval(C,alpha,w);


		double midaccuracy=prederror(w,yt,testdata,feattest);
		std::cout<< outer << "- "<<  "Accuracy: "<<midaccuracy <<", Wtime: " << itertime2<< ", Primal: "<<primvals<<", W norm: "<<norms<<", Dual: "<< dualvals<<std::endl;
	}
	std::cout<< "Finished" << std::endl;

	double accuracy=prederror(w,yt,testdata,feattest);
	std::cout<< accuracy << std::endl;
return 0;
}
Пример #2
0
static int install_white(SINGLE_QSP_ARG_DECL)
{
	int j,k;
	float wc[3];	/* white chromaticity */
	float uv[3];	/* unit vector */
	float *ptr;

	if( init_matrices(SINGLE_QSP_ARG) < 0 ) return(-1);

	/* we assume we already have the white point */
	if( ! know_white )
		error1("install_white:  white point not defined!?");


	for(j=0;j<3;j++) wc[j] = _white[j];
showvec(wc);
	rgb2rb(wc);	/* now we have the chromaticity of the white point */
advise("white transformed to opponent space");
showvec(wc);

	for(j=0;j<3;j++) uv[j] = wc[j];
	uv[0]+= 0.1;	/* take a step in the red cone direction */
	rb2rgb(uv);
	for(j=0;j<3;j++) uv[j] -= _white[j];

	/* now have an rgb vector for an red cone step */
	/* normalize this relative to the white point */
	/* this is to guarantee that amp. of +-1 won't overflow */
advise("red cone vector");
showvec(wc);

	wnorm(uv);

	/* now set the entries of the o2p matrix */
	for(j=0;j<3;j++) o2p_mat[j][0] = uv[j];

	for(j=0;j<3;j++) uv[j] = wc[j];
	uv[1]+= 0.1;	/* take a step in the blue cone  direction */
	rb2rgb(uv);
	for(j=0;j<3;j++) uv[j] -= _white[j];

	/* now have an rgb vector for a blue cone step */

	wnorm(uv);

	for(j=0;j<3;j++) o2p_mat[j][1] = uv[j];

	for(j=0;j<3;j++) uv[j] = _white[j];
	wnorm(uv);
	for(j=0;j<3;j++) o2p_mat[j][2] = uv[j];

	ptr = (float *)OBJ_DATA_PTR(o2p_dp);
	for(j=0;j<3;j++)
		for(k=0;k<3;k++)
			*ptr++ = o2p_mat[j][k];
	dp_copy(p2o_dp,o2p_dp);
	dt_invert(p2o_dp);
	ptr = (float *)OBJ_DATA_PTR(p2o_dp);
	for(j=0;j<3;j++)
		for(k=0;k<3;k++)
			p2o_mat[j][k] = *ptr++;

	return(0);
}