Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Joyeewen/gpu-svm
Browse files Browse the repository at this point in the history
  • Loading branch information
shijiashuai committed Nov 8, 2016
2 parents 67770fb + 453cdca commit bdb4348
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 48 deletions.
29 changes: 14 additions & 15 deletions mascot/commandLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ void Parser::ParseLine(int argc, char **argv, char *pcFileName, char *pcSavedFil
param.weight_label = NULL;
param.weight = NULL;

if(argc != 6)
{
HelpInfo();
}
// parse options
for(i=1;i<argc;i++)
{
Expand All @@ -63,7 +59,20 @@ void Parser::ParseLine(int argc, char **argv, char *pcFileName, char *pcSavedFil
case 'c':
param.C = atof(argv[i]);
break;
/*
case 'b':
param.probability = atoi(argv[i]);
break;
case 'f':
nNumofFeature = atoi(argv[i]);
if(nNumofFeature < 1)
{
HelpInfo();
}
break;
case 'o':
cross_validation = atoi(argv[i]);
break;
/*
case 's':
param.svm_type = atoi(argv[i]);
break;
Expand Down Expand Up @@ -91,9 +100,6 @@ void Parser::ParseLine(int argc, char **argv, char *pcFileName, char *pcSavedFil
case 'h':
param.shrinking = atoi(argv[i]);
break;
case 'b':
param.probability = atoi(argv[i]);
break;
case 'q':
print_func = &print_null;
i--;
Expand All @@ -115,13 +121,6 @@ void Parser::ParseLine(int argc, char **argv, char *pcFileName, char *pcSavedFil
param.weight[param.nr_weight-1] = atof(argv[i]);
break;
*/
case 'f':
nNumofFeature = atoi(argv[i]);
if(nNumofFeature < 1)
{
HelpInfo();
}
break;
default:
fprintf(stderr,"Unknown option: -%c\n", argv[i-1][1]);
HelpInfo();
Expand Down
37 changes: 18 additions & 19 deletions mascot/svmMain.cu
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ using std::endl;

int main(int argc, char **argv)
{
argc = 6;
argv = new char*[argc];
argv[1] = "-g";
argv[2] = "0.382";
argv[3] = "-c";
argv[4] = "100";
argv[argc - 1] = "dataset/iris.scale";
// argv[argc - 1] = "dataset/a1a";
/**/
char fileName[1024];
char savedFileName[1024];
Parser parser;
Expand All @@ -42,18 +33,26 @@ int main(int argc, char **argv)

printf("CUDA initialized.\n");

/*if(parser.cross_validation == 1)*/
/*{*/
/*//perform cross validation*/
/*cout << "performing cross-validation" << endl;*/
/*crossValidation(parser.param, fileName);*/
/*}*/
/*else*/
if(parser.cross_validation == 1)
{
//perform cross validation*/
cout << "performing cross-validation" << endl;
crossValidation(parser.param, fileName);
}
else if(parser.cross_validation == 0)
{
//perform svm training
cout << "performing training" << endl;
svmModel model = trainSVM(parser.param, fileName, parser.nNumofFeature);
}
svmModel model = trainSVM(parser.param, fileName, parser.nNumofFeature);
}
else if(parser.cross_validation == 2)
{
//perform svm evaluation
cout << "performing evaluation" << endl;
svmModel model = trainSVM(parser.param, fileName, parser.nNumofFeature);
evaluateSVMClassifier(model, fileName, parser.nNumofFeature);

}

return 0;
return 0;
}
41 changes: 27 additions & 14 deletions mascot/trainingFunction.cu
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ using std::endl;

void trainingByGPU(vector<vector<float_point> > &v_v_DocVector, data_info &SDataInfo, SVMParam &param);


svmModel trainSVM(SVMParam &param, string strTrainingFileName, int nNumofFeature) {

vector<vector<float_point> > v_v_DocVector;
Expand All @@ -47,20 +46,7 @@ svmModel trainSVM(SVMParam &param, string strTrainingFileName, int nNumofFeature
rawDataRead.ReadFromFile(strTrainingFileName, nNumofFeature, v_v_DocVector, v_nLabel);
svmProblem problem(v_v_DocVector, v_nLabel);
svmModel model;
param.probability = 1;//train with probability
model.fit(problem, param);
vector<int> predictLabels = model.predict(v_v_DocVector, true);
int numOfCorrect = 0;
for (int i = 0; i < v_v_DocVector.size(); ++i) {
if (predictLabels[i] == v_nLabel[i])
numOfCorrect++;
// for (int j = 0; j < problem.getNumOfClasses(); ++j) {
// printf("%.2f,",prob[i][j]);
// }
// printf("\n");
}
printf("training accuracy = %.2f%%(%d/%d)\n", numOfCorrect / (float) v_v_DocVector.size()*100, numOfCorrect,
(int) v_v_DocVector.size());
return model;
}

Expand Down Expand Up @@ -185,3 +171,30 @@ svm_model trainBinarySVM(svmProblem &problem, const SVMParam &param) {
model.nDimension = problem.getNumOfFeatures();
return model;
}

void evaluateSVMClassifier(svmModel &model, string strTrainingFileName, int nNumofFeature)
{
vector<vector<float_point> > v_v_DocVector;
vector<int> v_nLabel;

CDataIOOps rawDataRead;
int nNumofInstance = 0; //not used
long long nNumofValue = 0; //not used
BaseLibSVMReader::GetDataInfo(strTrainingFileName, nNumofFeature, nNumofInstance, nNumofValue);
rawDataRead.ReadFromFile(strTrainingFileName, nNumofFeature, v_v_DocVector, v_nLabel);

//perform svm classification
vector<int> predictLabels = model.predict(v_v_DocVector, true);
int numOfCorrect = 0;
for (int i = 0; i < v_v_DocVector.size(); ++i)
{
if (predictLabels[i] == v_nLabel[i])
numOfCorrect++;
// for (int j = 0; j < problem.getNumOfClasses(); ++j) {
// printf("%.2f,",prob[i][j]);
// }
// printf("\n");
}
printf("training accuracy = %.2f%%(%d/%d)\n", numOfCorrect / (float) v_v_DocVector.size()*100,
numOfCorrect, (int) v_v_DocVector.size());
}
2 changes: 2 additions & 0 deletions mascot/trainingFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ using std::string;
svmModel trainSVM(SVMParam &param, string strTrainingFileName, int nNumofFeature);

svm_model trainBinarySVM(svmProblem &problem, const SVMParam &param);
void evaluateSVMClassifier(svmModel &model, string strTrainingFileName, int nNumofFeature);

#endif /* TESTTRAINER_H_ */
27 changes: 27 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash


###options
#svm with probability output
PROB="-b 0" #0 for no probability output; 1 for probability output.

#task type
TASK="-o 0" #0 for training; 1 for cross validation; 2 for evaluation

#gamma for RBF kernel
GAMMA="-g 0.382"

#penalty
C="-c 100"

#number of features
NUMFEATURE="-f 123"

#file name (must appear as the last argument)
FILENAME="dataset/a1a" #"dataset/iris.scale"

#print out the command before execution
set -x

#command
./bin/mascot $PROB $TASK $GAMMA $C $NUMFEATURE $FILENAME

0 comments on commit bdb4348

Please sign in to comment.