| 1356 | // ---------------------------------------------------------------------------------------- |
| 1357 | |
| 1358 | void test_positional_encodings() |
| 1359 | { |
| 1360 | print_spinner(); |
| 1361 | using net_type = tag1<positional_encodings<input<matrix<float>>>>; |
| 1362 | net_type net; |
| 1363 | |
| 1364 | const unsigned long sequence_dim = 4; |
| 1365 | const unsigned long embedding_dim = 6; |
| 1366 | const unsigned long n_samples = 1, n_channels = 1; |
| 1367 | matrix<float> input_data(sequence_dim, embedding_dim); |
| 1368 | input_data = 0.0f; |
| 1369 | |
| 1370 | resizable_tensor input_tensor(n_samples, n_channels, sequence_dim, embedding_dim); |
| 1371 | std::vector<matrix<float>> x(n_samples); |
| 1372 | x[0] = input_data; |
| 1373 | net.to_tensor(&x[0], &x[0] + n_samples, input_tensor); |
| 1374 | net.forward(input_tensor); |
| 1375 | |
| 1376 | matrix<float> expected_output(sequence_dim, embedding_dim); |
| 1377 | const float n = 10000.0f; |
| 1378 | for (long r = 0; r < sequence_dim; ++r) { |
| 1379 | for (long c = 0; c < embedding_dim; ++c) { |
| 1380 | float theta = static_cast<float>(r) / std::pow(n, static_cast<float>(c) / embedding_dim); |
| 1381 | expected_output(r, c) = (c % 2 == 0) ? std::sin(theta) : std::cos(theta); |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | auto& net_output = layer<tag1>(net).get_output(); |
| 1386 | DLIB_TEST(have_same_dimensions(net_output, input_tensor)); |
| 1387 | DLIB_TEST(max(abs(image_plane(net_output) - expected_output)) < 1e-5); |
| 1388 | } |
| 1389 | |
| 1390 | // ---------------------------------------------------------------------------------------- |
| 1391 |
no test coverage detected