NTU-Coursera ml: HomeWork 1 Q15-20
Question15
The training data format is as follows:
The input has four dimensions, and the output is {-1, + 1 }. There are a total of 400 data records.
The question requires that the weight vector element be initialized to 0, and then "Naive Cycle" is used to traverse the training set. When the iteration is stopped, the weight vector is updated several times.
The so-called "Naive Cycle" means that after an error is found on a data entry x (I) and the weight vector is updated, the next time the data is read from x (I + 1, instead of returning to the first data x (0.
# Include
# Include
# Include
Using namespace std; # define DEMENSION 5 // data dimension double weights [DEMENSION]; // weight vector int step = 0; // number of iterations int length = 0; // number of data entries int index = 0; // current data entry index bool isFinished = false; // iteration termination status char * file = "training_data.txt "; struct record {double input [DEMENSION]; // input int output; // output}; vector
TrainingSet; // training data int sign (double x) {if (x <0) return-1; else if (x> 0) return 1; else return-1 ;} // add two vectors and update the first vector void add (double * v1, double * v2, int demension) {for (int I = 0; I
> CurRecord. input [I];} dataFile> curRecord. output; trainingSet. push_back (curRecord);} dataFile. close (); length = trainingSet. size ();} void PLA () {int start = index; double curInput [DEMENSION]; // locate the index of the next error record while (trainingSet [index]. output = sign (multiply (weights, trainingSet [index]. input, DEMENSION) {if (index = length-1) {index = 0;} else {index ++;} if (index = start) {isFinished = true; break;} // No error found, iteration ended} if (isFinished) {cout <"Calculation Result: step =" <
File opening failed "<
The test results are as follows:
The program is run multiple times, and the iterations are all 45 times.
Question16
Vc/C0rvC1rzGy + O/samples/O21Mioz/LBv7XExr2 ++ dDe1f20zsr9oaM8YnIgLz48L3A + PHA + PHByZSBjbGFzcz0 = "brush: java;"> # include
# Include
# Include
# Include
Using namespace std; # define DEMENSION 5 // data dimension int step = 0; // number of iterations int index = 0; // The current data entry index bool isFinished = false; // iteration termination status char * file = "training_data.txt"; struct record {double input [DEMENSION]; // input int output; // output}; int sign (double x) {// same as Q15} void add (double * v1, double * v2, int demension) {// multiply two vectors with Q15, returns the Inner Product double multiply (double * v1, double * v2, int demension) {// same as Q15} // The vector is multiplied by the real number. The result is returned through * result, void multiply (double * result, double * v, double num, int demension) {// same as Q15} // creates a random sorting void setRandomOrder (vector
& TrainingSet, vector
& RandIndexes) {srand (unsigned) time (NULL); int length = trainingSet. size (); vector
AssignedIndexes (length, false); for (int I = 0; I
& TrainingSet) {while (! DataFile. eof () {record curRecord; curRecord. input [0] = 1; for (int I = 1; I
> CurRecord. input [I];} dataFile> curRecord. output; trainingSet. push_back (curRecord);} dataFile. close ();} void PLA (vector
& TrainingSet, vector
& RandIndexes, double * weights) {int length = trainingSet. size (); int start = index; double curInput [DEMENSION]; // locate the index of the next error record while (trainingSet [randIndexes [index]. output = sign (multiply (weights, trainingSet [randIndexes [index]. input, DEMENSION) {if (index = length-1) {index = 0;} else {index ++;} if (index = start) {isFinished = true; break;} // No error found, iteration ended} if (isFinished) {return;} else {step ++; // update: weights = weights + curOutput * curInput multiply (curInput, trainingSet [randIndexes [index]. input, trainingSet [randIndexes [index]. output, DEMENSION); add (weights, curInput, DEMENSION); if (index = length-1) {index = 0;} else {index ++;} PLA (trainingSet, randIndexes, weights);} return;} void main () {int totalSteps = 0; for (int I = 0; I <2000; ++ I) {double weights [DEMENSION]; // Weight vector
TrainingSet; // training data vector
RandIndexes; // Random index list for data access ifstream dataFile (file); step = 0; index = 0; isFinished = false; if (dataFile. is_open () {getData (dataFile, trainingSet); setRandomOrder (trainingSet, randIndexes);} else {cerr <"ERROR ---> file opening failed" <
# Include
# Include
# Include
# Include
Using namespace std; # define DEMENSION 5 // data dimension int index = 0; // The current data entry index int step = 0; // current number of times the weight vector is updated char * file = "training_data.txt"; char * file_test = "test_data.txt"; struct record {double input [DEMENSION]; // input int output; // output}; int sign (double x) {// same as Q16} // add two vectors, update the first vector void add (double * v1, double * v2, int demension) {// same as Q16} // returns the Inner Product double multiply (double * v1, double * v2, int demension) by multiplying two vectors) {// same as Q16} // returns the result by multiplying the vector and the real number. The void multiply (double * result, double * v, double num, int demension) {// same as Q16} // creates a random sorting void setRandomOrder (vector
& TrainingSet, vector
& RandIndexes) {// same as Q16} // read data void getData (ifstream & dataFile, vector
& Data) {// same as Q16} // error statistics and Pocket vector update void errCountAndPocketUpdate (vector
& TrainingSet, vector
& RandIndexes, double * weights, double * pocketWeights, double & trainingErrRate, int dataLength) {int errCount = 0; double curTrainingErrRate = 1.0; for (int I = 0; I
& TrainingSet, vector
& RandIndexes, double * weights, double * pocketWeights, double & trainingErrRate) {int length = trainingSet. size (); double curInput [DEMENSION]; errCountAndPocketUpdate (trainingSet, randIndexes, weights, pocketWeights, trainingErrRate, length ); // locate the index of the next error record while (trainingSet [randIndexes [index]. output = sign (multiply (weights, trainingSet [randIndexes [index]. input, DEMENSION) {if (index = length-1) {index = 0;} else {index ++ ;}} if (step <50) {step ++; // update: weights = weights + curOutput * curInput multiply (curInput, trainingSet [randIndexes [index]. input, trainingSet [randIndexes [index]. output, DEMENSION); add (weights, curInput, DEMENSION); if (index = length-1) {index = 0;} else {index ++;} Pocket (trainingSet, randIndexes, weights, pocketWeights, trainingErrRate);} else {return ;}// counts the error rate of W (pocket) on the test dataset. double getTestErrRate (vector
& TestSet, double * pocketWeights, int dataLength) {int errCount = 0; for (int I = 0; I
TrainingSet; // training data vector
TestSet; // test data vector
RandIndexes; // Random Index list for data access ifstream dataFile (file); ifstream testDataFile (file_test); double trainingErrRate = 1.0; // error rate in the training set [0.0, 1.0] double testErrRate = 1.0; // error rate in the test set [0.0, 1.0] step = 0; index = 0; if (dataFile. is_open () & testDataFile. is_open () {getData (dataFile, trainingSet); getData (testDataFile, testSet); setRandomOrder (trainingSet, randIndexes);} else {cerr <"ERROR ---> file opening failed" <
For more discussions and exchanges on Machine Learning, please stay tuned to this blog and Sina Weibo songzi_tea.