MCPcopy Create free account
hub / github.com/apache/singa / TEST

Function TEST

test/singa/test_sgd.cc:26–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24#include "singa/singa_config.h"
25
26TEST(SGD, ApplyWithoutMomentum) {
27 singa::SGD sgd;
28 const float v[4] = {0.1f, 0.2f, 0.3f, 0.4f};
29 const float g[4] = {0.1f, 0.1f, 0.1f, 0.1f};
30
31 singa::Tensor value(singa::Shape{4}), grad(singa::Shape{4});
32 value.CopyDataFromHostPtr(v, 4);
33 grad.CopyDataFromHostPtr(g, 4);
34
35 float lr = 0.1f;
36 sgd.Apply(0, lr, "xx", grad, value);
37
38 singa::Tensor v1 = value.Clone();
39 const float* newv1 = v1.data<float>();
40 for (int i = 0; i < 4; i++) {
41 EXPECT_FLOAT_EQ(newv1[i], v[i] - g[i] * lr);
42 }
43
44 lr /= 2;
45 grad.CopyDataFromHostPtr(g, 4);
46 sgd.Apply(1, lr, "xx", grad, value);
47 singa::Tensor v2 = value.Clone();
48 const float* newv2 = v2.data<float>();
49 for (int i = 0; i < 4; i++) {
50 EXPECT_FLOAT_EQ(newv2[i], newv1[i] - g[i] * lr);
51 }
52}
53
54TEST(SGD, ApplyWithMomentum) {
55 singa::SGD sgd;

Callers

nothing calls this directly

Calls 5

ToHostMethod · 0.80
CopyDataFromHostPtrMethod · 0.45
ApplyMethod · 0.45
CloneMethod · 0.45
SetMomentumGeneratorMethod · 0.45

Tested by

no test coverage detected