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

Method run

tools/train/source/demo/nnGradTest.cpp:352–422  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

350class LoopGradTest : public DemoUnit {
351public:
352 virtual int run(int argc, const char* argv[]) override {
353 MNN_PRINT("Test grad for Loop Binary\n");
354 {
355 int w = 4;
356 int h = 5;
357 auto input = _Input({w, h}, NHWC);
358 auto target = _Input({w, h}, NHWC);
359 auto targetAdd = _Input({w, h}, NHWC);
360 auto inputPtr = input->writeMap<float>();
361 auto targetPtr = target->writeMap<float>();
362 auto targetPtrAdd = targetAdd->writeMap<float>();
363 for (int y=0; y<h; ++y) {
364 for (int x=0; x<w; ++x) {
365 auto value = ((float) (y * w) + (float)x) * 0.1f;
366 inputPtr[x + y * w] = value;
367 targetPtr[x + y * w] = value * (float) (y + 1) * 0.1f;
368 targetPtrAdd[x + y * w] = value + (float) (y + 1) * 0.1f;
369 }
370 }
371
372 auto weight = _TrainableParam(0.0f, {1, h}, NHWC);
373 auto weightAdd = _TrainableParam(0.0f, {1, h}, NHWC);
374 std::shared_ptr<Module> _m(Module::createEmpty({weight, weightAdd}));
375 std::shared_ptr<SGD> sgd(new SGD(_m));
376 sgd->setLearningRate(0.01f);
377 MNN_PRINT("Test grad for Binary Mul Loop\n");
378 for (int i = 0; i < 1000; ++i) {
379 auto compute = input * weight;
380 auto loss = _ReduceMean(_Square(_Subtract(compute, target)), {});
381 if (i % 100 == 0) {
382 MNN_PRINT("Loss = %f\n", loss->readMap<float>()[0]);
383 }
384 sgd->step(loss);
385 }
386 MNN_PRINT("Test grad for Binary Add Loop\n");
387 for (int i = 0; i < 1000; ++i) {
388 auto compute = input + weightAdd;
389 auto loss = _ReduceMean(_Square(_Subtract(compute, target)), {});
390 if (i % 100 == 0) {
391 MNN_PRINT("Loss = %f\n", loss->readMap<float>()[0]);
392 }
393 sgd->step(loss);
394 }
395 }
396 MNN_PRINT("Test grad for Gather\n");
397 {
398 // set input data
399 const float inpudata[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0,
400 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21, 0, 22.0, 23.0, 24.0};
401 std::vector<float> inputDataRaw(0.0f, sizeof(inpudata) / sizeof(float));
402 auto params = _TrainableParam(inputDataRaw.data(), {4, 3, 2}, NCHW, halide_type_of<float>());
403 const int indices_data[] = {1, 0, 1, 0};
404 const std::vector<float> expectedOutput = {7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0,
405 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
406 std::shared_ptr<Module> _m(Module::createEmpty({params}));
407 std::shared_ptr<SGD> sgd(new SGD(_m));
408 sgd->setLearningRate(0.01f);
409 for (int i = 0; i < 1000; ++i) {

Callers

nothing calls this directly

Calls 12

_InputFunction · 0.85
_TrainableParamFunction · 0.85
_ReduceMeanFunction · 0.85
_SubtractFunction · 0.85
_ConstFunction · 0.85
_GatherV2Function · 0.85
_ReshapeFunction · 0.85
setLearningRateMethod · 0.80
stepMethod · 0.80
_SquareFunction · 0.50
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected