Skip to content

Commit bdb4348

Browse files
committed
Merge branch 'master' of github.com:Joyeewen/gpu-svm
2 parents 67770fb + 453cdca commit bdb4348

File tree

5 files changed

+88
-48
lines changed

5 files changed

+88
-48
lines changed

mascot/commandLineParser.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ void Parser::ParseLine(int argc, char **argv, char *pcFileName, char *pcSavedFil
4545
param.weight_label = NULL;
4646
param.weight = NULL;
4747

48-
if(argc != 6)
49-
{
50-
HelpInfo();
51-
}
5248
// parse options
5349
for(i=1;i<argc;i++)
5450
{
@@ -63,7 +59,20 @@ void Parser::ParseLine(int argc, char **argv, char *pcFileName, char *pcSavedFil
6359
case 'c':
6460
param.C = atof(argv[i]);
6561
break;
66-
/*
62+
case 'b':
63+
param.probability = atoi(argv[i]);
64+
break;
65+
case 'f':
66+
nNumofFeature = atoi(argv[i]);
67+
if(nNumofFeature < 1)
68+
{
69+
HelpInfo();
70+
}
71+
break;
72+
case 'o':
73+
cross_validation = atoi(argv[i]);
74+
break;
75+
/*
6776
case 's':
6877
param.svm_type = atoi(argv[i]);
6978
break;
@@ -91,9 +100,6 @@ void Parser::ParseLine(int argc, char **argv, char *pcFileName, char *pcSavedFil
91100
case 'h':
92101
param.shrinking = atoi(argv[i]);
93102
break;
94-
case 'b':
95-
param.probability = atoi(argv[i]);
96-
break;
97103
case 'q':
98104
print_func = &print_null;
99105
i--;
@@ -115,13 +121,6 @@ void Parser::ParseLine(int argc, char **argv, char *pcFileName, char *pcSavedFil
115121
param.weight[param.nr_weight-1] = atof(argv[i]);
116122
break;
117123
*/
118-
case 'f':
119-
nNumofFeature = atoi(argv[i]);
120-
if(nNumofFeature < 1)
121-
{
122-
HelpInfo();
123-
}
124-
break;
125124
default:
126125
fprintf(stderr,"Unknown option: -%c\n", argv[i-1][1]);
127126
HelpInfo();

mascot/svmMain.cu

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ using std::endl;
2020

2121
int main(int argc, char **argv)
2222
{
23-
argc = 6;
24-
argv = new char*[argc];
25-
argv[1] = "-g";
26-
argv[2] = "0.382";
27-
argv[3] = "-c";
28-
argv[4] = "100";
29-
argv[argc - 1] = "dataset/iris.scale";
30-
// argv[argc - 1] = "dataset/a1a";
31-
/**/
3223
char fileName[1024];
3324
char savedFileName[1024];
3425
Parser parser;
@@ -42,18 +33,26 @@ int main(int argc, char **argv)
4233

4334
printf("CUDA initialized.\n");
4435

45-
/*if(parser.cross_validation == 1)*/
46-
/*{*/
47-
/*//perform cross validation*/
48-
/*cout << "performing cross-validation" << endl;*/
49-
/*crossValidation(parser.param, fileName);*/
50-
/*}*/
51-
/*else*/
36+
if(parser.cross_validation == 1)
5237
{
38+
//perform cross validation*/
39+
cout << "performing cross-validation" << endl;
40+
crossValidation(parser.param, fileName);
41+
}
42+
else if(parser.cross_validation == 0)
43+
{
5344
//perform svm training
5445
cout << "performing training" << endl;
55-
svmModel model = trainSVM(parser.param, fileName, parser.nNumofFeature);
56-
}
46+
svmModel model = trainSVM(parser.param, fileName, parser.nNumofFeature);
47+
}
48+
else if(parser.cross_validation == 2)
49+
{
50+
//perform svm evaluation
51+
cout << "performing evaluation" << endl;
52+
svmModel model = trainSVM(parser.param, fileName, parser.nNumofFeature);
53+
evaluateSVMClassifier(model, fileName, parser.nNumofFeature);
54+
55+
}
5756

58-
return 0;
57+
return 0;
5958
}

mascot/trainingFunction.cu

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ using std::endl;
3434

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

37-
3837
svmModel trainSVM(SVMParam &param, string strTrainingFileName, int nNumofFeature) {
3938

4039
vector<vector<float_point> > v_v_DocVector;
@@ -47,20 +46,7 @@ svmModel trainSVM(SVMParam &param, string strTrainingFileName, int nNumofFeature
4746
rawDataRead.ReadFromFile(strTrainingFileName, nNumofFeature, v_v_DocVector, v_nLabel);
4847
svmProblem problem(v_v_DocVector, v_nLabel);
4948
svmModel model;
50-
param.probability = 1;//train with probability
5149
model.fit(problem, param);
52-
vector<int> predictLabels = model.predict(v_v_DocVector, true);
53-
int numOfCorrect = 0;
54-
for (int i = 0; i < v_v_DocVector.size(); ++i) {
55-
if (predictLabels[i] == v_nLabel[i])
56-
numOfCorrect++;
57-
// for (int j = 0; j < problem.getNumOfClasses(); ++j) {
58-
// printf("%.2f,",prob[i][j]);
59-
// }
60-
// printf("\n");
61-
}
62-
printf("training accuracy = %.2f%%(%d/%d)\n", numOfCorrect / (float) v_v_DocVector.size()*100, numOfCorrect,
63-
(int) v_v_DocVector.size());
6450
return model;
6551
}
6652

@@ -185,3 +171,30 @@ svm_model trainBinarySVM(svmProblem &problem, const SVMParam &param) {
185171
model.nDimension = problem.getNumOfFeatures();
186172
return model;
187173
}
174+
175+
void evaluateSVMClassifier(svmModel &model, string strTrainingFileName, int nNumofFeature)
176+
{
177+
vector<vector<float_point> > v_v_DocVector;
178+
vector<int> v_nLabel;
179+
180+
CDataIOOps rawDataRead;
181+
int nNumofInstance = 0; //not used
182+
long long nNumofValue = 0; //not used
183+
BaseLibSVMReader::GetDataInfo(strTrainingFileName, nNumofFeature, nNumofInstance, nNumofValue);
184+
rawDataRead.ReadFromFile(strTrainingFileName, nNumofFeature, v_v_DocVector, v_nLabel);
185+
186+
//perform svm classification
187+
vector<int> predictLabels = model.predict(v_v_DocVector, true);
188+
int numOfCorrect = 0;
189+
for (int i = 0; i < v_v_DocVector.size(); ++i)
190+
{
191+
if (predictLabels[i] == v_nLabel[i])
192+
numOfCorrect++;
193+
// for (int j = 0; j < problem.getNumOfClasses(); ++j) {
194+
// printf("%.2f,",prob[i][j]);
195+
// }
196+
// printf("\n");
197+
}
198+
printf("training accuracy = %.2f%%(%d/%d)\n", numOfCorrect / (float) v_v_DocVector.size()*100,
199+
numOfCorrect, (int) v_v_DocVector.size());
200+
}

mascot/trainingFunction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ using std::string;
1616
svmModel trainSVM(SVMParam &param, string strTrainingFileName, int nNumofFeature);
1717

1818
svm_model trainBinarySVM(svmProblem &problem, const SVMParam &param);
19+
void evaluateSVMClassifier(svmModel &model, string strTrainingFileName, int nNumofFeature);
20+
1921
#endif /* TESTTRAINER_H_ */

run.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
3+
4+
###options
5+
#svm with probability output
6+
PROB="-b 0" #0 for no probability output; 1 for probability output.
7+
8+
#task type
9+
TASK="-o 0" #0 for training; 1 for cross validation; 2 for evaluation
10+
11+
#gamma for RBF kernel
12+
GAMMA="-g 0.382"
13+
14+
#penalty
15+
C="-c 100"
16+
17+
#number of features
18+
NUMFEATURE="-f 123"
19+
20+
#file name (must appear as the last argument)
21+
FILENAME="dataset/a1a" #"dataset/iris.scale"
22+
23+
#print out the command before execution
24+
set -x
25+
26+
#command
27+
./bin/mascot $PROB $TASK $GAMMA $C $NUMFEATURE $FILENAME

0 commit comments

Comments
 (0)