cxx11_tensor_math.cpp 993 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2015 Benoit Steiner <benoit.steiner.goog@gmail.com>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "main.h"
  10. #include <Eigen/CXX11/Tensor>
  11. using Eigen::Tensor;
  12. using Eigen::RowMajor;
  13. static void test_tanh()
  14. {
  15. Tensor<float, 1> vec1(6);
  16. vec1.setRandom();
  17. Tensor<float, 1> vec2 = vec1.tanh();
  18. for (int i = 0; i < 6; ++i) {
  19. VERIFY_IS_APPROX(vec2(i), tanhf(vec1(i)));
  20. }
  21. }
  22. static void test_sigmoid()
  23. {
  24. Tensor<float, 1> vec1(6);
  25. vec1.setRandom();
  26. Tensor<float, 1> vec2 = vec1.sigmoid();
  27. for (int i = 0; i < 6; ++i) {
  28. VERIFY_IS_APPROX(vec2(i), 1.0f / (1.0f + std::exp(-vec1(i))));
  29. }
  30. }
  31. EIGEN_DECLARE_TEST(cxx11_tensor_math)
  32. {
  33. CALL_SUBTEST(test_tanh());
  34. CALL_SUBTEST(test_sigmoid());
  35. }