You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

MathApiTest.cs 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Linq;
  4. using Tensorflow;
  5. using Tensorflow.NumPy;
  6. using static Tensorflow.Binding;
  7. namespace TensorFlowNET.UnitTest.ManagedAPI
  8. {
  9. [TestClass]
  10. public class MathApiTest : EagerModeTestBase
  11. {
  12. // A constant vector of size 6
  13. Tensor a = tf.constant(new float[] { 1.0f, -0.5f, 3.4f, -2.1f, 0.0f, -6.5f });
  14. Tensor b = tf.constant(new float[,] { { 1.0f, -0.5f, 3.4f }, { -2.1f, 0.0f, -6.5f } });
  15. [TestMethod]
  16. public void Sin()
  17. {
  18. var b = tf.sin(a, name: "Sin");
  19. var expected = new float[] { 0.84147096f, -0.47942555f, -0.2555412f, -0.86320937f, 0f, -0.21511999f };
  20. var actual = b.ToArray<float>();
  21. Assert.IsTrue(Equal(expected, actual));
  22. }
  23. [TestMethod]
  24. public void Tan()
  25. {
  26. var b = tf.tan(a, name: "Tan");
  27. var expected = new float[] { 1.5574077f, -0.5463025f, 0.264317f, 1.709847f, 0f, -0.2202772f };
  28. var actual = b.ToArray<float>();
  29. Assert.IsTrue(Equal(expected, actual));
  30. }
  31. [TestMethod]
  32. public void ReduceSum()
  33. {
  34. var x1 = tf.reduce_sum(b);
  35. Assert.AreEqual(-4.7f, (float)x1);
  36. var x2 = tf.reduce_sum(b, 0);
  37. Assert.IsTrue(Enumerable.SequenceEqual(new[] { -1.0999999f, -0.5f, -3.1f }, x2.ToArray<float>()));
  38. var x3 = tf.reduce_sum(b, 1);
  39. Assert.IsTrue(Enumerable.SequenceEqual(new[] { 3.9f, -8.6f }, x3.ToArray<float>()));
  40. var x4 = tf.reduce_sum(b, 1, keepdims: true);
  41. Assert.AreEqual((2, 1), x4.shape);
  42. var x5 = tf.reduce_sum(b, (0, 1));
  43. Assert.AreEqual(-4.7f, (float)x5);
  44. }
  45. [TestMethod]
  46. public void Erf()
  47. {
  48. var erf = tf.math.erf(a, name: "erf");
  49. var expected = new float[] { 0.8427007f, -0.5204999f, 0.99999845f, -0.9970206f, 0f, -1f };
  50. var actual = erf.ToArray<float>();
  51. Assert.IsTrue(Equal(expected, actual));
  52. }
  53. [TestMethod]
  54. public void ReduceEuclideanNorm()
  55. {
  56. var x = tf.constant(new[,] { { 1, 2, 3 }, { 1, 1, 1 } });
  57. Assert.AreEqual(tf.math.reduce_euclidean_norm(x).numpy(), 4);
  58. var y = tf.constant(new[,] { { 1, 2, 3 }, { 1, 1, 1 } }, dtype: tf.float32);
  59. Assert.IsTrue(Equal(tf.math.reduce_euclidean_norm(y).numpy(), 4.1231055f));
  60. Assert.IsTrue(Equal(tf.math.reduce_euclidean_norm(y, 0).ToArray<float>(),
  61. new float[] { np.sqrt(2f), np.sqrt(5f), np.sqrt(10f) }));
  62. Assert.IsTrue(Equal(tf.math.reduce_euclidean_norm(y, 1).ToArray<float>(),
  63. new float[] { np.sqrt(14f), np.sqrt(3f) }));
  64. Assert.IsTrue(Equal(tf.math.reduce_euclidean_norm(y, 1, keepdims: true).ToArray<float>(),
  65. new float[] { np.sqrt(14f), np.sqrt(3f) }));
  66. Assert.AreEqual(tf.math.reduce_euclidean_norm(y, (0, 1)).numpy(), np.sqrt(17f));
  67. }
  68. }
  69. }