// // SGD.hpp // MNN // // Created by MNN on 2019/11/22. // Copyright © 2018, Alibaba Group Holding Limited // #ifndef SGD_hpp #define SGD_hpp #include #include #include #include "ParameterOptimizer.hpp" namespace MNN { namespace Train { class MNN_PUBLIC SGD : public ParameterOptimizer { public: SGD(std::shared_ptr module); virtual ~ SGD() = default; virtual std::map onGetNextParameter(Express::VARP loss) override; virtual std::pair, std::vector> onMakeParameterUpdateGraphByGrad(const std::vector& parameterGrads) override; Express::VARP regularizeParameters(Express::VARP param, Express::VARP grad); virtual Express::VARP onComputeUpdateValue(Express::VARP param, Express::VARP grad); void setLearningRate(float rate); float getMomentum(); void setMomentum(float momentum); float getWeightDecay(); void setWeightDecay(float decay); RegularizationMethod getRegularizationMethod(); void setRegularizationMethod(RegularizationMethod method); float currentLearningRate(); void setGradBlockName(std::vector block) { mGradBlockExprName = block; } protected: float mLearningRate = 0.001f; float mMomentum = 0; float mWeightDecay = 0; RegularizationMethod mRegularizationMethod = L2; std::map mHistory; // For Cache const Express::Expr* mLoss = nullptr; int mLossFromIndex = 0; std::vector mGradBlockExprName; }; } // namespace Train } // namespace MNN #endif // SGD_hpp