📄 smo.cpp
字号:
// SMO.cpp : Defines the entry point for the console application.
#include "StdAfx.h"
#include "stdio.h"
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <float.h>
#include "initializeTraining.h"
#include "learn.h"
#include "result.h"
#include <string>
#define DEFAULT 4.5
#define EPS DBL_EPSILON
#define OPTSTRING "t:c:b:d:v:h"
char usage[]=
"\n\
General option:\n\
[-h] help (this description of command line args)\n\
[-b] Specify nature of feature value. 0 for nonbinary. 1 for binary\n\
Default is 0.\n\
\n\
Required options:\n\
[-t] type of kernel,0 for linear,1 for polynomial ,2 for RBF\n\
[-c] c parameter\n\
[-d] degree fo polynomial is needed if option [-t] is 1\n\
[-v] variance of RBF function is needed if option [-t] is 2\n\
\n";
void useMsg(char *progname){
fprintf( stderr,"\nUse:%s options\n",progname);
fprintf(stderr,"%s",usage);
}
int getopt (int argc,char** argv,char* pszValidOpts,char** ppszParam)
{
static int iArg = 1;
char chOpt;
char* psz = NULL;
char* pszParam = NULL;
if (iArg < argc)
{
psz = &(argv[iArg][0]);
if (*psz == '-' || *psz == '/')
{
// we have an option specifier
chOpt = argv[iArg][1];
if (isalnum(chOpt) || ispunct(chOpt))
{
// we have an option character
psz = strchr(pszValidOpts, chOpt);
if (psz != NULL)
{
// option is valid, we want to return chOpt
if (psz[1] == ':')
{
// option can have a parameter
psz = &(argv[iArg][2]);
if (*psz == '\0')
{
// must look at next argv for param
if (iArg+1 < argc)
{
psz = &(argv[iArg+1][0]);
if (*psz == '-' || *psz == '/')
{
// next argv is a new option, so param
// not given for current option
}
else
{
// next argv is the param
iArg++;
pszParam = psz;
}
}
else
{
// reached end of args looking for param
}
}
else
{
// param is attached to option
pszParam = psz;
}
}
else
{
// option is alone, has no parameter
}
}
else
{
// option specified is not in list of valid options
chOpt = '?';
pszParam = &(argv[iArg][0]);
}
}
else
{
// though option specifier was given, option character
// is not alpha or was was not specified
chOpt = '?';
pszParam = &(argv[iArg][0]);
}
}
else
{
// standalone arg given with no option specifier
chOpt = '?';
pszParam = &(argv[iArg][0]);
}
}
else
{
// end of argument list
chOpt = 0;
}
iArg++;
*ppszParam = pszParam;
return (chOpt);
}
int main(int argc, char* argv[])
{
char *progname =argv[0];
FILE *in,*out;
char opt;
int temp;
double startTime;
C=0;
kernelType =-1;
degree =0;
sigmaSqr =0;
binaryFeature =0;
//debug
/*for (int m=0;m<argc;m++){
printf("argc[%d]",m);
printf("%s\n",argv[m]);
}*/
//check command line options
char * optarg =NULL;
while( ((opt =getopt(argc,argv,OPTSTRING,&optarg)) !=0)) {
switch(opt){
case 't':{
temp = atoi(optarg);
if(temp==0)
kernelType=0;
else if(temp==1)
kernelType=1;
else if(temp ==2)
kernelType=2;
else {
fprintf(stderr,"kernel type is either 0,1 or 2\n");
exit(1);
}
break;
}
case 'c':
if( sscanf(optarg,"%f",&C)==0){
fprintf(stderr,"Expect a positive number for C.\n");
exit(1);
}
else
C =atof(optarg);
if(C<=0){
fprintf(stderr,"C has to be >0\n");
exit(1);
}
break;
case 'd':
if(sscanf(optarg,"%d",°ree)==0){
fprintf(stderr, "Expect degree to be a positive integer.\n");
exit(1);
}
else
degree = atoi(optarg);
if (degree<=0){
fprintf(stderr, "degree has to be a positive integer.\n");
exit(1);
}
break;
case 'v':
if(sscanf(optarg,"%f",&sigmaSqr)==0){
fprintf(stderr, "Expect a positive number for variance.\n");
exit(1);
}
else
sigmaSqr = atof(optarg);
if (sigmaSqr<=0){
fprintf(stderr, "variance has to be >0.\n");
exit(1);
}
rbfConstant =1/(2*sigmaSqr);
break;
case 'b':
if(sscanf(optarg,"%d",&binaryFeature)==0){
fprintf(stderr, "binaryFeature option is either 0 or 1.\n");
exit(1);
}
else
binaryFeature = atoi(optarg);
if (binaryFeature !=0 && binaryFeature !=1){
fprintf(stderr, "binaryFeature option is either 0 or 1.\n");
exit(1);
}
break;
case 'h':
useMsg( progname);
exit(1);
break;
/* default:
useMsg(progname);
exit(1);
break;*/
}
}
//check all necessary parameters are in
if( kernelType ==-1){
fprintf(stderr,"Kernel type has not been specified.\n");
exit(2);
}
else if (kernelType ==1 && degree ==0){
fprintf(stderr,"degree haas not been specified.\n");
exit(2);
}
else if(kernelType ==2 && sigmaSqr ==0){
fprintf(stderr, "Variance has not been specified.\n");
exit(2);
}
else if (C==0)
C =DEFAULT;
//check training file and model file
if((in =fopen(argv[argc-2],"r")) ==NULL){
fprintf( stderr,"Can't open %s \n",argv[argc-2]);
exit(2);
}
if ((out =fopen(argv[argc-1],"w"))==NULL){
fprintf( stderr,"Can't open %s \n",argv[argc-1]);
exit(2);
}
printf("smo_learn is preparing to learn...\n");
// char test =getc(in);
if( !readFile(in)){
fprintf( stderr,"Error in initializing. Program exits.\n");
exit(1);
}
else
fclose(in);
if( !initializeTraining()){
fprintf( stderr,"Error in initializing data sturcture.Program exits.\n");
exit(1);
}
printf("Start training ...\n");
startTime =clock()/CLOCKS_PER_SEC;
startLearn();
printf("Training is completed\n");
//print training statistics.
printf("CPU time is %f secs \n",clock()/CLOCKS_PER_SEC-startTime);
printf("Writing training results...\n");
writeModel(out);
fclose(out);
printf("Finish writing training results.\n");
printf("no of iteration is %f\n",iteration);
printf("threshold b is %f\n",getb());
if (kernelType ==0)
printf("norm of weight vector is %f\n",calculateNorm());
printf("no. of unBound multipliers is %d\n",unBoundSv);
printf("no. of Bounded multipliers if %d\n", boundSv);
///free memory
free(target);
free(lambda);
free(nonZeroFeature);
free(error);
free(nonBound);
free(weight);
free(unBoundIndex);
free(nonZeroLambda);
/* for( i=0;i<numExample;i++){
free(example[i]);
}*/ //debug assertion failure.
free(example);
free(errorCache);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -