| 26 | #include "singa/singa_config.h" |
| 27 | |
| 28 | TEST(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 |
| 62 | TEST(AdaGrad, ApplyCUDA) { |