struct vertex *tree_firstchild(struct ctx *x, struct vertex *v) { c_iterator i; FOR_X_IN_CHILDREN(x, i, v) { if (!CHILD_HIDDEN(v, i) && !CHILD_IMPRED(v,i)) return C_X(i); } return 0; }
void SVMachine::showParams(){ std::cout << C_b << std::endl; std::cout << C_n << std::endl; for(int i=0; i<C_n; i++){ if(C_SupportVectors.at(i) > 0.0){ std::cout << C_SupportVectors.at(i) << std::endl; std::cout << C_y.at(i) << std::endl; std::cout << C_X(i,0); for(int j = 1; j < C_X.row(i).n_elem; j++){ std::cout << ";" << C_X(i,j); } std::cout << std::endl; } } }
//Está un poco fuzzy, habrá que estructurarlo y tal, pero correcto void SVMachine::quadraticSolution() { int n = C_trainingSet.size(); int m = 1; // Entiendo que es el numero de restricciones Program qp (CGAL::EQUAL); // Obtengo la X // Utils::scalation(C_trainingSet); // Escalado de parámetros std::cout << "n vale: " << n << std::endl; std::cout << "nFeatures vale: " << C_nFeatures << std::endl; C_X = arma::mat(n, C_nFeatures); for(int i = 0; i < n; i++){ for(int j = 0; j < C_nFeatures; j++){ C_X(i,j) = C_trainingSet[i].getInput()[j]; } } std::cout << "X vale: " << std::endl << C_X; // Obtengo la Y C_y = arma::mat(n, 1); std::cout << "n vale: " << n << std::endl; for(int i = 0; i < n; i++){ C_y(i) = C_trainingSet[i].getResult()[0]; } std::cout << "Y vale: " << std::endl << C_y; // Seteo la restriccion for(int i = 0; i < n; i++){ qp.set_a(i,0,ET(C_y.at(i))); qp.set_l(i,true,ET(0)); qp.set_u(i,false); qp.set_c(i,ET(-1)); // std::cout << "y(" << i << "): " << y.at(i) << std::endl; } qp.set_b(0,ET(0)); qp.set_r(0,CGAL::EQUAL); qp.set_c0(ET(0)); // Seteo la symmetric positive-semidefinite matrix for(int i = 0; i < n; i++){ for(int j = 0; j<=i; j++){ ET ip = C_kernel->K(C_X.row(i).t(),C_X.row(j).t()); // std::cout << "El kernel para " << i << "," << j << " nos dice que el innerproduct es: " << ip << std::endl; ET daux = ip*ET(C_y.at(i))*ET(C_y.at(j)); std::cout << "El producto de " << i << "," << j << ": " << daux << std::endl; qp.set_d(i,j,daux); // std::cout << "La matriz auxiliar vale:" << daux << std::endl; } } // solve the program, using ET as the exact type Solution s = CGAL::solve_quadratic_program(qp, ET()); // print basic constraint indices (we know that there is only one: 1) std::cout << "Y la solución es: " << s << std::endl; // arma::mat W = arma::mat(n,1); C_SupportVectors = arma::mat(n,1); ET sumaB(0.0); if(s.is_optimal()) { // we know that, don't we? int i = 0; for (Solution::Variable_value_iterator it = s.variable_values_begin(); it != s.variable_values_end(); ++it){ C_SupportVectors(i) = CGAL::to_double(*it); if(C_SupportVectors.at(i) != 0.0){ C_m = i; // Esto lo hago para obtener un sv que me resuelva la b } i++; } // Calculo la b for(int i=0; i<n; i++) if(C_SupportVectors.at(i) != 0.0) sumaB += ET(C_SupportVectors.at(i))*ET(C_y.at(i))*C_kernel->K(C_X.row(i).t(), C_X.row(C_m).t()); C_b = ET(C_y(C_m)) - sumaB; std::cout << "Y el valor de b es: "<< C_b << std::endl; } else std::cout << "No es optima, vete tu a saber por qué...\n"; // int pausa; std::cin >> pausa; }
void SVMachine::saveParams(){ //Ahora averiguaremos el nombre que debe tener el archivo de thetas std::string command = "mkdir -p "; std::vector<std::string> trainingFileParts(Utils::split(C_trainingFile,'-')); std::string root = trainingFileParts[0].substr(0,10); std::string machinefolder = "SVM/"; std::string value = trainingFileParts[0].substr(10,trainingFileParts[0].length()); std::string route = ""; route.append(root); route.append(machinefolder); route.append(value); route.append("/"); command.append(route); system(command.c_str()); std::cout << "El comando ha sido: "<< std::endl << command << std::endl; std::string prefix = ""; prefix.append(root); prefix.append(value); std::string name = C_trainingFile.substr(prefix.length()+1,C_trainingFile.length()); route.append(name); C_fileName = route; std::cout << "EL archivo creado es: " << C_fileName << std::endl; //Y para terminar, escribimos las thetas en un archivo std::string line; std::ofstream paramsFile(C_fileName.c_str()); if(paramsFile.is_open()){ paramsFile << C_b << std::endl; int n = 0; for(int i=0; i<C_trainingSet.size(); i++){ if(C_SupportVectors.at(i) > 0.0){ n++; } } paramsFile << n << std::endl; for(int i=0; i<C_trainingSet.size(); i++){ if(C_SupportVectors.at(i) > 0.0){ paramsFile << C_SupportVectors.at(i) << std::endl; paramsFile << C_y.at(i) << std::endl; paramsFile << C_X(i,0); for(int j = 1; j < C_X.row(i).n_elem; j++){ paramsFile << ";" << C_X(i,j); } paramsFile << std::endl; } } paramsFile.close(); std::cout << "Ya he escrito" << std::endl; } else{ std::cout << "Unable to open file" << std::endl; } }
void SVMachine::loadParams(){ // std::cout << "I'm loading Thetas with the NNMachine" << std::endl; //Ahora averiguaremos el nombre que debe tener el archivo de thetas std::vector<std::string> trainingFileParts(Utils::split(C_inputFile,'-')); std::string root = trainingFileParts[0].substr(0,10); std::string machinefolder = "SVM/"; std::string value = trainingFileParts[0].substr(10,trainingFileParts[0].length()); std::string route = ""; route.append(root); route.append(machinefolder); route.append(value); route.append("/"); std::string prefix = ""; prefix.append(root); prefix.append(value); std::string name = C_inputFile.substr(prefix.length()+1,C_inputFile.length()); route.append(name); C_fileName = "params.txt"; // std::cout << "EL archivo que vamos a leer es: " << C_fileName << std::endl; //Y para terminar, escribimos las thetas en un archivo std::string line; std::ifstream paramsFile(C_fileName.c_str()); if(paramsFile.is_open()){ std::getline(paramsFile,line); C_b = ET(atof(line.c_str())); std::getline(paramsFile,line); C_n = atoi(line.c_str()); init(); std::getline(paramsFile,line); for(int i=0; i<C_n; i++){ if(C_SupportVectors.at(i) > 0.0){ C_SupportVectors(i) = atof(line.c_str()); std::getline(paramsFile,line); C_y(i) = atof(line.c_str()); std::getline(paramsFile,line); std::vector<double> aux = Utils::vStovD(Utils::split(line,';')); for(int j = 0; j < aux.size(); j++){ C_X(i,j) = aux[j]; } std::getline(paramsFile,line); } } paramsFile.close(); std::cout << "Ya he cargado" << std::endl; } else{ std::cout << "Unable to open file" << std::endl; } }