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

Function TEST

test/singa/test_adagrad.cc:28–59  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26#include "singa/singa_config.h"
27
28TEST(AdaGrad, ApplyCPU) {
29 singa::AdaGrad adagrad;
30 float lr = 0.1f;
31 const float v[4] = {0.1f, 0.2f, 0.3f, 0.4f};
32 const float g[4] = {0.01f, 0.02f, 0.03f, 0.04f};
33
34 singa::Tensor value(singa::Shape{4}), grad(singa::Shape{4});
35 value.CopyDataFromHostPtr(v, 4);
36 grad.CopyDataFromHostPtr(g, 4);
37
38 singa::OptimizerConf conf;
39 adagrad.Setup(conf);
40 adagrad.Apply(0, lr, "xx", grad, value);
41
42 singa::Tensor v1 = value.Clone();
43 const float* newv1 = v1.data<float>();
44 float history[4];
45 for (int i = 0; i < 4; ++i) history[i] = g[i] * g[i];
46 for (int i = 0; i < 4; ++i)
47 EXPECT_NEAR(newv1[i], v[i] - lr * g[i] / sqrt(history[i] + conf.delta()),
48 1e-5);
49
50 grad.CopyDataFromHostPtr(g, 4);
51 adagrad.Apply(1, lr, "xx", grad, value);
52 singa::Tensor v2 = value.Clone();
53 const float* newv2 = v2.data<float>();
54 for (int i = 0; i < 4; ++i) history[i] += g[i] * g[i];
55
56 for (int i = 0; i < 4; ++i)
57 EXPECT_NEAR(newv2[i],
58 newv1[i] - lr * g[i] / sqrt(history[i] + conf.delta()), 1e-5);
59}
60
61#ifdef USE_CUDA
62TEST(AdaGrad, ApplyCUDA) {

Callers

nothing calls this directly

Calls 6

ToHostMethod · 0.80
sqrtFunction · 0.50
CopyDataFromHostPtrMethod · 0.45
SetupMethod · 0.45
ApplyMethod · 0.45
CloneMethod · 0.45

Tested by

no test coverage detected