| 21 | } |
| 22 | |
| 23 | double SGDTrainer::train(net::NeuralNet *network, const std::vector< std::vector<double> > &input, const std::vector< std::vector<double> > &correctOutput) { |
| 24 | double totalError = 0; |
| 25 | int iterations = 0; |
| 26 | resetNetworkVectors(network); |
| 27 | |
| 28 | do { |
| 29 | for(unsigned int a = 0; a < input.size(); a++) { |
| 30 | trainOnDataPoint(network, input[a], correctOutput[a]); |
| 31 | } |
| 32 | |
| 33 | totalError = 0; |
| 34 | for(unsigned int trialIndex = 0; trialIndex < input.size(); trialIndex++) { |
| 35 | std::vector<double> output = network->getOutput(input[trialIndex]); |
| 36 | for(unsigned int outputIndex = 0; outputIndex < output.size(); outputIndex++) { |
| 37 | totalError += pow(correctOutput[trialIndex][outputIndex] - output[outputIndex], 2); |
| 38 | } |
| 39 | } |
| 40 | iterations++; |
| 41 | } while(totalError > targetErrorLevel && iterations < maximumEpochs); |
| 42 | |
| 43 | //if(iterations >= maximumEpochs-1) std::cout << "SGDTrainer hit max epochs with an error level of " << totalError << ".\n"; |
| 44 | |
| 45 | finalWeights = network->getWeights3D(); |
| 46 | |
| 47 | return totalError; |
| 48 | } |
| 49 | |
| 50 | double SGDTrainer::trainEpocs(double numberOfEpochs, net::NeuralNet *network, const std::vector< std::vector<double> > &input, const std::vector< std::vector<double> > &correctOutput) { |
| 51 | resetNetworkVectors(network); |
no test coverage detected