Line data Source code
1 : #include "NeuralNetwork.h" 2 : #include "LayerHidden.h" 3 : #include "LayerInput.h" 4 : #include "LayerOutput.h" 5 : #include "SimpleLogger.h" 6 : #include "exception/NeuralNetworkException.h" 7 : 8 : using namespace sipai; 9 : 10 61 : cv::Mat NeuralNetwork::forwardPropagation(const cv::Mat &inputValues) { 11 61 : if (layers.front()->layerType != LayerType::LayerInput) { 12 0 : throw NeuralNetworkException("Invalid front layer type"); 13 : } 14 61 : if (layers.back()->layerType != LayerType::LayerOutput) { 15 0 : throw NeuralNetworkException("Invalid back layer type"); 16 : } 17 61 : ((LayerInput *)layers.front())->setInputValues(inputValues); 18 245 : for (auto &layer : layers) { 19 184 : layer->forwardPropagation(); 20 : } 21 61 : return ((LayerOutput *)layers.back())->getOutputValues(); 22 : } 23 : 24 42 : void NeuralNetwork::backwardPropagation(const cv::Mat &expectedValues, 25 : const float &error_min, 26 : const float &error_max) { 27 42 : if (layers.back()->layerType != LayerType::LayerOutput) { 28 0 : throw NeuralNetworkException("Invalid back layer type"); 29 : } 30 42 : ((LayerOutput *)layers.back())->computeErrors(expectedValues); 31 168 : for (auto it = layers.rbegin(); it != layers.rend(); ++it) { 32 126 : (*it)->backwardPropagation(error_min, error_max); 33 : } 34 42 : } 35 : 36 42 : void NeuralNetwork::updateWeights(float learning_rate) { 37 168 : for (auto &layer : layers) { 38 126 : layer->updateWeights(learning_rate); 39 : } 40 42 : }