From f846e8fec2e9c8d16e24e12d6b6b7efc77d3cc83 Mon Sep 17 00:00:00 2001 From: liaogang Date: Mon, 20 Feb 2017 15:13:43 +0800 Subject: [PATCH] Add const for getLayerOutput --- paddle/gserver/gradientmachines/GradientMachine.h | 2 +- .../gradientmachines/MultiGradientMachine.cpp | 12 ++++++------ .../gserver/gradientmachines/MultiGradientMachine.h | 2 +- paddle/gserver/gradientmachines/NeuralNetwork.cpp | 2 +- paddle/gserver/gradientmachines/NeuralNetwork.h | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/paddle/gserver/gradientmachines/GradientMachine.h b/paddle/gserver/gradientmachines/GradientMachine.h index a814e771d12..5469c0d89f4 100644 --- a/paddle/gserver/gradientmachines/GradientMachine.h +++ b/paddle/gserver/gradientmachines/GradientMachine.h @@ -134,7 +134,7 @@ public: backward(callback); } - virtual MatrixPtr getLayerOutput(const std::string& layerName) { + virtual MatrixPtr getLayerOutput(const std::string& layerName) const { return nullptr; } diff --git a/paddle/gserver/gradientmachines/MultiGradientMachine.cpp b/paddle/gserver/gradientmachines/MultiGradientMachine.cpp index 7e60920376d..2d42e648302 100644 --- a/paddle/gserver/gradientmachines/MultiGradientMachine.cpp +++ b/paddle/gserver/gradientmachines/MultiGradientMachine.cpp @@ -282,7 +282,8 @@ void MultiGradientMachine::forwardBackward(const std::vector& inArgs, backwardImp(callback); } -MatrixPtr MultiGradientMachine::getLayerOutput(const std::string& layerName) { +MatrixPtr MultiGradientMachine::getLayerOutput( + const std::string& layerName) const { // each thread has the same neural network auto nn = threads_[0]->getGradientMachine(); size_t height = 0; @@ -301,11 +302,10 @@ MatrixPtr MultiGradientMachine::getLayerOutput(const std::string& layerName) { // copy one layer output from one trainer thread at each time size_t startRow = 0; - - for (size_t i = 0; i < threads_.size(); i++) { - auto tmpMatrix = layerOutput->subMatrix(startRow, mats[i]->getHeight()); - tmpMatrix->copyFrom(*mats[i]); - startRow += mats[i]->getHeight(); + for (auto& mat : mats) { + auto tmpMatrix = layerOutput->subMatrix(startRow, mat->getHeight()); + tmpMatrix->copyFrom(*mat); + startRow += mat->getHeight(); } return layerOutput; diff --git a/paddle/gserver/gradientmachines/MultiGradientMachine.h b/paddle/gserver/gradientmachines/MultiGradientMachine.h index 988d5098179..a1a2d417062 100644 --- a/paddle/gserver/gradientmachines/MultiGradientMachine.h +++ b/paddle/gserver/gradientmachines/MultiGradientMachine.h @@ -189,7 +189,7 @@ public: PassType passType, const UpdateCallback& callback); - virtual MatrixPtr getLayerOutput(const std::string& layerName); + virtual MatrixPtr getLayerOutput(const std::string& layerName) const; virtual void onPassEnd(); diff --git a/paddle/gserver/gradientmachines/NeuralNetwork.cpp b/paddle/gserver/gradientmachines/NeuralNetwork.cpp index 1f9ace4f67f..00887c81d47 100644 --- a/paddle/gserver/gradientmachines/NeuralNetwork.cpp +++ b/paddle/gserver/gradientmachines/NeuralNetwork.cpp @@ -293,7 +293,7 @@ void NeuralNetwork::backward(const UpdateCallback& callback) { } } -MatrixPtr NeuralNetwork::getLayerOutput(const std::string& layerName) { +MatrixPtr NeuralNetwork::getLayerOutput(const std::string& layerName) const { auto it = layerMap_.find(layerName); CHECK(it != layerMap_.end()) << "Cannot find layer: " << layerName; return it->second->getOutputValue(); diff --git a/paddle/gserver/gradientmachines/NeuralNetwork.h b/paddle/gserver/gradientmachines/NeuralNetwork.h index bf9ed09327f..6ecc251a409 100644 --- a/paddle/gserver/gradientmachines/NeuralNetwork.h +++ b/paddle/gserver/gradientmachines/NeuralNetwork.h @@ -87,7 +87,7 @@ public: virtual void backward(const UpdateCallback& callback = nullptr); - virtual MatrixPtr getLayerOutput(const std::string& layerName); + virtual MatrixPtr getLayerOutput(const std::string& layerName) const; const LayerPtr& getLayer(const std::string& layerName) const { auto it = layerMap_.find(layerName); -- GitLab