// default constructor svm::svm() : supervisedInstanceClassifier(), syseps(std::numeric_limits<double>::epsilon()) { _lti_debug("Creating SVM\n"); // create an instance of the parameters with the default values parameters defaultParameters; _lti_debug("Creating SVM 2\n"); // set the default parameters setParameters(defaultParameters); _lti_debug("Creating SVM 3\n"); // set pointers kernels.clear(); _lti_debug("Creating SVM 4\n"); trainData=0; target=0; nClasses=0; _lti_debug("Creating SVM 5\n"); // default output object outTemplate=outputTemplate(); _lti_debug("Creating SVM 6\n"); _lti_debug("Done creating SVM\n"); }
void shClassifier::defineOutputTemplate() { ivector tids(rIdMap.size()); unsigned int i=0; for (i=0; i<rIdMap.size(); i++) { tids.at(i)=rIdMap[i]; } outTemplate=outputTemplate(tids); }
void svm::defineOutputTemplate() { ivector tids(rIdMap.size()); unsigned int i=0; for (i=0; i<rIdMap.size(); i++) { tids.at(i)=rIdMap[i]; } outTemplate=outputTemplate(tids); // output tmp(nClasses); // output::namesMap names; // char buf[32]; // char fmt[32]; // int dec=0; // int i=0; // for (int k=1; k<nClasses; k*=10) { // dec++; // } // sprintf(fmt,"%%0%dd",dec); // for (i=0; i<nClasses; i++) { // sprintf(buf,fmt,rIdMap[i]); // names[i]=buf; // } // tmp.setClassNames(names); // std::vector<outputElementProb>& ov=tmp.getOutputVector(); // for (i=0; i<nClasses; i++) { // objectProb objProb; // _lti_debug("Assigning element " << i << " to " << rIdMap[i] << std::endl); // objProb.id = rIdMap[i]; // objProb.prob = 1; // ov[i].objects.clear(); // ov[i].objects.push_back(objProb); // } // setOutputObject(tmp); }
static int doargs(int argc, char* argv[]) { int i; if (argv[0] != NULL && *argv[0] != 0) { progname = argv[0]; } for (i = 1; i < argc; i++) { if (*argv[i] != '-') { /* end of options; keep it */ break; } else if (IS("--")) { /* end of options; skip it */ ++i; break; } else if (IS("-template")) { /* output OPCODES_TXT template */ outputTemplate(stdout); exit(EXIT_SUCCESS); } else if (IS("-lua")) { /* output allopcodes.lua */ fwrite(allopcodes_lua, sizeof(unsigned char), allopcodes_lua_len, stdout); exit(EXIT_SUCCESS); } else if (IS("-o")) { /* output file */ output = argv[++i]; if (output == NULL || *output == 0) { usage("'-o' needs argument", NULL); } } else if (IS("-gs")) { /* compare <input.luac> with x86-standard allopcodes.luac to generate a opcodes.txt */ cmp_gen_luac = 1; } else if (IS("-gf")) { /* compare <input.luac> with a fresh compiled allopcodes.luac to generate a opcodes.txt */ cmp_gen_lua = 1; } else if (IS("-v")) { /* show version */ printf("LuaOpSwap " VERSION_STRING " for " LUA_VERSION "\n"); exit(EXIT_SUCCESS); } else { /* unknown option */ usage("unrecognized option '%s'", argv[i]); } } return i; }
// implements the Fuzzy C Means algorithm bool fuzzyCMeans::train(const dmatrix& data) { bool ok=true; int t=0; // create the distance functor according to the paramter norm distanceFunctor<double>* distFunc = 0; switch (getParameters().norm) { case parameters::L1: distFunc = new l1Distance<double>; break; case parameters::L2: distFunc = new l2Distance<double>; break; default: break; } int nbOfClusters=getParameters().nbOfClusters; int nbOfPoints=data.rows(); if(nbOfClusters>nbOfPoints) { setStatusString("more Clusters than points"); ok = false; } double q=getParameters().fuzzifier; if (q<=1) { setStatusString("q has to be bigger than 1"); ok = false; } // select some points of the given data to initialise the centroids selectRandomPoints(data,nbOfClusters,centroids); // initialize variables centroids.resize(nbOfClusters,data.columns(),0.0); dmatrix memberships(nbOfPoints, nbOfClusters, 0.0); double terminationCriterion=0; double newDistance; dvector newCenter(data.columns()); dvector currentPoint(data.columns()); dmatrix newCentroids(nbOfClusters,data.columns(),0.0); double sumOfMemberships=0; double membership=0; double dist1; double dist2; int i,j,k,m; do { // calculate new memberships memberships.fill(0.0); // clear old memberships for (i=0; i<nbOfPoints; i++) { for (j=0; j<nbOfClusters; j++) { newDistance=0; dist1=distFunc->apply(data.getRow(i), centroids.getRow(j)); for (k=0; k<nbOfClusters; k++) { dist2=distFunc->apply(data.getRow(i), centroids.getRow(k)); // if distance is 0, normal calculation of membership is not possible. if (dist2!=0) { newDistance+=pow((dist1/dist2),(1/(q-1))); } } // if point and centroid are equal if (newDistance!=0) memberships.at(i,j)=1/newDistance; else { dvector row(memberships.columns(),0.0); memberships.setRow(i,row); memberships.at(i,j)=1; break; } } } t++; // counts the iterations // calculate new centroids based on modified memberships for (m=0; m<nbOfClusters; m++) { newCenter.fill(0.0); sumOfMemberships=0; for (i=0; i<nbOfPoints; i++) { currentPoint=data.getRow(i); membership=pow(memberships.at(i,m),q); sumOfMemberships+=membership; currentPoint.multiply(membership); newCenter.add(currentPoint); } newCenter.divide(sumOfMemberships); newCentroids.setRow(m,newCenter); } terminationCriterion=distFunc->apply(centroids,newCentroids); centroids=newCentroids; } // the termination criterions while ( (terminationCriterion>getParameters().epsilon) && (t<getParameters().maxIterations)); int nbClusters = nbOfClusters; //Put the id information into the result object //Each cluster has the id of its position in the matrix ivector tids(nbClusters); for (i=0; i<nbClusters; i++) { tids.at(i)=i; } outTemplate=outputTemplate(tids); return ok; }
bool SOFM2D::train(const dmatrix& data) { // tracks the status of the training process. // if an error occurs set to false and use setStatusString() // however, training should continue, fixing the error as well as possible bool b=true; int i; const parameters& param=getParameters(); // find the actual size of the grid if (param.calculateSize) { b = calcSize(data); } else { sizeX=param.sizeX; sizeY=param.sizeY; } // check whether one of the dimensions has negative or zero size // and try to fix by using alternate way of setting sizes. if (sizeX<=0 || sizeY<=0) { b=false; std::string err="Negative or zero size of one dimension"; if (param.calculateSize) { if (param.area<=0) { err += "\narea is <= 0"; if (param.sizeX>0 && param.sizeY>0) { sizeX=param.sizeX; sizeY=param.sizeY; err += "\nusing sizeX and sizeY instead"; } } } else { if (param.sizeX<=0) { err += "\nsizeX <= 0"; } if (param.sizeY<=0) { err += "\nsizeY <= 0"; } if (param.area>0) { err += "\ncalculating size from area instead"; calcSize(data); err += getStatusString(); } } setStatusString(err.c_str()); } // set grid to size grid.resize(sizeY*sizeX, data.columns()); //set learn rates setLearnRates(data.rows()); if (validProgressObject()) { getProgressObject().reset(); std::string str("SOFM2D: Training using "); switch(param.metricType) { case parameters::L1: str += "L1 distance"; break; case parameters::L2: str += "L2 distance"; break; case parameters::Dot: str += "dot product"; break; default: str += "unnamed method"; } char buffer[256]; sprintf(buffer," size of map %i x %i", sizeY, sizeX); str += std::string(buffer); getProgressObject().setTitle(str); getProgressObject().setMaxSteps(param.stepsOrdering+param.stepsConvergence+2); } //initialize grid if (validProgressObject()) { getProgressObject().step("initializing map"); } b = initGrid(data); //training if (param.metricType == parameters::Dot) { trainDot(data); } else { trainDist(data); } if (validProgressObject()) { getProgressObject().step("training finished"); } int nbOutputs = sizeX*sizeY; //Put the id information into the result object //Each output value has the id of its position in the matrix ivector tids(nbOutputs); for (i=0; i<nbOutputs; i++) { tids.at(i)=i; } outTemplate=outputTemplate(tids); return b; }