int main() { int nInput = 2; int nHide = 2; int nOutput = 1; BpNet bpNet(nInput, nHide, nOutput); int n = 4; double **ppInput, **ppOutput; ppInput = new double *[n]; ppOutput = new double *[n]; for (int i = 0; i < n; i++) { ppInput[i] = new double[2]; ppOutput[i] = new double[1]; } /*ppInput[0][0] = 0.8; ppInput[0][1] = 0.5; ppInput[0][2] = 0; ppInput[1][0] = 0.9; ppInput[1][1] = 0.7; ppInput[1][2] = 0.3; ppInput[2][0] = 1; ppInput[2][1] = 0.8; ppInput[2][2] = 0.5; ppInput[3][0] = 0; ppInput[3][1] = 0.2; ppInput[3][2] = 0.3; ppInput[4][0] = 0.2; ppInput[4][1] = 0.1; ppInput[4][2] = 1.3; ppInput[5][0] = 0.2; ppInput[5][1] = 0.7; ppInput[5][2] = 0.8; ppOutput[0][0] = 0; ppOutput[0][1] = 1; ppOutput[1][0] = 0; ppOutput[1][1] = 1; ppOutput[2][0] = 0; ppOutput[2][1] = 1; ppOutput[3][0] = 1; ppOutput[3][1] = 0; ppOutput[4][0] = 1; ppOutput[4][1] = 0; ppOutput[5][0] = 1; ppOutput[5][1] = 0; */ ppInput[0][0] = 0; ppInput[0][1] = 0; ppInput[1][0] = 0; ppInput[1][1] = 1; ppInput[2][0] = 1; ppInput[2][1] = 0; ppInput[3][0] = 1; ppInput[3][1] = 1; ppOutput[0][0] = 0; ppOutput[1][0] = 1; ppOutput[2][0] = 1; ppOutput[3][0] = 0; bpNet.Train(n, ppInput, ppOutput); double pTest[1]; double pRs[1]; while (1) { printf("test:\n"); scanf("%lf%lf", &pTest[0], &pTest[1]); bpNet.Classify(pTest, pRs); printf("%lf\n", pRs[0]); } return 0; }
bool trial(const backpropSettings& bSettings, const boost::array< boost::array<float, inputWidth>, numTrials >& cTrials, const boost::array< boost::array<float, outputWidth>, numTrials >& cResults) { size_t midWidth = (inputWidth + outputWidth); while ((midWidth & 15) != 0) { ++midWidth; }; boost::array<size_t, 3> layers = {{inputWidth , midWidth , outputWidth}}; std::cout << "dimensions= " << inputWidth << "," << midWidth << "," << outputWidth << "\nnumTrials= " << cTrials.size() << "\n"; //boost::array<size_t, 2> layers = {{16 , 2}}; neuralNet cNet(layers.begin(), layers.end()); backpropNet bpNet(cNet, bSettings); bpNet.randomizeWeights(); double _MSE = 1.0; double MSE = 0.0; double numTrials = 0.0; boost::timer cTimer; for (size_t iEpoch = 0; iEpoch < 3000; ++iEpoch) { // determine an order that we intend to visit the trials: std::vector<size_t> order(cTrials.size()); for (size_t iTurn = 0; iTurn != order.size(); ++iTurn) { order[iTurn] = iTurn; } std::random_shuffle(order.begin(), order.end()); // jitter: if ((iEpoch % 1000) == 0) { bpNet.jitterNetwork(); } // perform trials: for (size_t iNTrial = 0; iNTrial != order.size(); ++iNTrial) { size_t iTrial = order[iNTrial]; bpNet.getNeuralNet().feedForward(cTrials[iTrial].begin()); MSE += bpNet.backPropagate(cResults[iTrial].begin()); assert(MSE == MSE); numTrials += 1.0; bpNet.updateWeights(); } // debug: if ((iEpoch+1) % 100 == 0) { //rankedNet.updateWeights(); _MSE = MSE/numTrials; std::cout << std::setw(4) << iEpoch << " " << _MSE << "\n"; MSE = 0.0; numTrials = 0.0; } } std::cout << "test " << ((_MSE<0.05)?"passed":"FAILED") << "! Elapsed time = " << cTimer.elapsed() << "\n"; return _MSE<0.05; };