int main() { const unsigned int SAMPLE_NUM = 1000; // 定义训练输入矩阵和输出矩阵 LPerceptronMatrix X(SAMPLE_NUM, 2); LPerceptronMatrix Y(SAMPLE_NUM, 1); for (unsigned int i = 0; i < SAMPLE_NUM; i++) { X[i][0] = (float)RandInt(-1000, 1000); X[i][1] = (float)RandInt(-1000, 1000); Y[i][0] = ((X[i][0] + X[i][1]) >= 0)? LPERCEPTRON_SUN : LPERCEPTRON_MOON; } LPerceptronProblem problem(X, Y); // 训练模型 LPerceptron perceptron; perceptron.TrainModel(problem); // 使用测试样本测试 LPerceptronMatrix testSample(1, 2); testSample[0][0] = -50.0f; testSample[0][1] = 0.0f; printf("Predict: %f\n", perceptron.Predict(testSample)); system("pause"); return 0; }
void ofApp::draw() { ofBackground(0); // get grid of 100 x 100 probabilities for (int i=0; i<100; i++) { for (int j=0; j<100; j++) { double x = ofMap(i, 0, 100, 0, ofGetWidth()); double y = ofMap(j, 0, 100, 0, ofGetHeight()); float probability = testSample(x, y); ofFill(); ofSetColor(ofClamp(ofMap(probability, 0, 0.00001, 0, 255), 0, 255)); ofRect(x, y, ofGetWidth()/100, ofGetHeight()/100); } } // draw random sample on grid ofSetColor(0, 0, 255); ofCircle(randSample[0], randSample[1], 10); ofDrawBitmapString("random sample", randSample[0]+8, randSample[1]); // get mixture parameters for (int i=0; i<numGaussians; i++) { vector<double> mean = gmm.getMean(i); vector<double> variance = gmm.getVariance(i); vector<double> std = gmm.getStandardDeviation(i); double prior = gmm.getPrior(i); string msg = "Gaussian #"+ofToString(i)+" : prior ("+ofToString(prior)+"), mean ("+ofToString(mean[0])+", "+ofToString(mean[1])+"), standard deviation ("+ofToString(std[0])+", "+ofToString(std[1])+")"; ofSetColor(0, 255, 0); ofDrawBitmapString(msg, 15, 20 + 20*i); } // get sample from mouse double x = (double) ofGetMouseX(); double y = (double) ofGetMouseY(); float probability = testSample(x, y); string msg = "P("+ofToString(x) + ", " + ofToString(y) + ") = " + ofToString(probability); ofDrawBitmapString(msg, x, y); // message about controls ofSetColor(255, 0, 0); ofDrawBitmapString("Press '1' for example of training GMM from data\nPress '2' for example of setting GMM explicitly\nPress spacebar to sample random point from GMM", 20, ofGetHeight()-50); }
inline NNTYPE NeuralNet::testCycle (Iterator itPatternBegin, Iterator itPatternEnd) { m_pSettings->startTestCycle (); NNTYPE error = 0.0; NNTYPE sumOfWeights = 0.0; for (Iterator itPattern = itPatternBegin; itPattern != itPatternEnd; ++itPattern) { Pattern& pattern = *itPattern; error += testSample (pattern.beginInput (), pattern.endInput (), pattern.beginOutput (), pattern.endOutput (), pattern.weight ()); sumOfWeights += pattern.weight (); } error /= sumOfWeights; m_pSettings->endTestCycle (); return error; }