MCPcopy Create free account
hub / github.com/alibaba/MNN / onGetNextParameter

Method onGetNextParameter

tools/train/source/optimizer/SGD.cpp:79–118  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

77}
78
79std::map<Express::VARP, Express::VARP> SGD::onGetNextParameter(Express::VARP loss) {
80 auto grad = OpGrad::grad(loss, trainable(), mGradBlockExprName);
81 auto parameters = module()->parameters();
82 std::vector<VARP> prepareCompute;
83 for (auto iter : parameters) {
84 if (iter->expr().first->get() != nullptr) {
85 prepareCompute.emplace_back(iter);
86 }
87 }
88 for (auto& iter : grad) {
89 prepareCompute.emplace_back(iter.second);
90 }
91 Variable::prepareCompute(prepareCompute);
92 std::vector<VARP> replaceOp(prepareCompute.size());
93 for (int i=0; i<prepareCompute.size(); ++i) {
94 auto info = prepareCompute[i]->getInfo();
95 auto ptr = prepareCompute[i]->readMap<void>();
96 if (nullptr == ptr) {
97 MNN_ERROR("Compute error in SGD\n");
98 return {};
99 }
100 auto newVar = _Const(ptr, info->dim, info->order, info->type);
101 replaceOp[i]= newVar;
102 }
103 for (int i=0; i<prepareCompute.size(); ++i) {
104 Variable::replace(prepareCompute[i], replaceOp[i]);
105 }
106
107 for (auto& iter : grad) {
108 // apply regularization
109 auto addWeightDecayGrad = regularizeParameters(iter.first, iter.second);
110 addWeightDecayGrad.fix(Express::VARP::CONSTANT);
111 // apply momentum, etc.
112 auto updateValue = this->onComputeUpdateValue(iter.first, addWeightDecayGrad);
113 // apply update
114 auto newParameter = iter.first - updateValue;
115 iter.second = newParameter;
116 }
117 return grad;
118}
119
120std::pair<std::vector<Express::VARP>, std::vector<Express::VARP>> SGD::onMakeParameterUpdateGraphByGrad(const std::vector<ParameterOptGrad>& parameterGrads) {
121 std::map<MNN::Express::VARP, MNN::Express::VARP> varUpdateMap;

Callers 1

stepMethod · 0.80

Calls 9

onComputeUpdateValueMethod · 0.95
gradFunction · 0.85
_ConstFunction · 0.85
parametersMethod · 0.80
fixMethod · 0.80
moduleFunction · 0.70
getMethod · 0.45
sizeMethod · 0.45
getInfoMethod · 0.45

Tested by

no test coverage detected