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.

Math.Test.cs 3.5 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Tensorflow;
  7. using Tensorflow.NumPy;
  8. namespace TensorFlowNET.UnitTest.NumPy
  9. {
  10. /// <summary>
  11. /// https://numpy.org/doc/stable/reference/generated/numpy.prod.html
  12. /// </summary>
  13. [TestClass]
  14. public class MathTest : EagerModeTestBase
  15. {
  16. [TestMethod]
  17. public void prod()
  18. {
  19. var p = np.prod(1.0, 2.0);
  20. Assert.AreEqual(p, 2.0);
  21. p = np.prod(new[,] { { 1.0, 2.0 }, { 3.0, 4.0 } });
  22. Assert.AreEqual(p, 24.0);
  23. p = np.prod(new[,] { { 1.0, 2.0 }, { 3.0, 4.0 } }, axis: 1);
  24. Assert.AreEqual(p.shape, 2);
  25. Assert.IsTrue(Equal(p.ToArray<double>(), new[] { 2.0, 12.0 }));
  26. }
  27. [TestMethod]
  28. public void astype()
  29. {
  30. var x = np.array(new byte[] { 1, 100, 200 });
  31. var x1 = x.astype(np.float32);
  32. Assert.AreEqual(x1[2], 200f);
  33. }
  34. [TestMethod]
  35. public void divide()
  36. {
  37. var x = np.array(new float[] { 1, 100, 200 });
  38. var y = x / 2;
  39. Assert.AreEqual(y.dtype, np.float32);
  40. }
  41. [TestMethod]
  42. public void sin()
  43. {
  44. var x = np.sin(np.pi / 2);
  45. Assert.AreEqual(x, 1d);
  46. }
  47. [TestMethod]
  48. public void cos()
  49. {
  50. var x = np.cos(np.pi / 2);
  51. Assert.AreEqual(x, 6.123233995736766e-17);
  52. }
  53. [TestMethod]
  54. public void power()
  55. {
  56. var x = np.arange(6);
  57. var y = np.power(x, 3);
  58. Assert.AreEqual(y, new[] { 0, 1, 8, 27, 64, 125 });
  59. }
  60. [TestMethod]
  61. public void square()
  62. {
  63. var x = np.arange(6);
  64. var y = np.square(x);
  65. Assert.AreEqual(y, new[] { 0, 1, 4, 9, 16, 25 });
  66. }
  67. [TestMethod]
  68. public void dotproduct()
  69. {
  70. var x1 = new NDArray(new[] { 1, 2, 3 });
  71. var x2 = new NDArray(new[] { 4, 5, 6 });
  72. double result1 = np.dot(x1, x2);
  73. NDArray y1 = new float[,] {
  74. { 1.0f, 2.0f, 3.0f },
  75. { 4.0f, 5.1f,6.0f },
  76. { 4.0f, 5.1f,6.0f }
  77. };
  78. NDArray y2 = new float[,] {
  79. { 3.0f, 2.0f, 1.0f },
  80. { 6.0f, 5.1f, 4.0f },
  81. { 6.0f, 5.1f, 4.0f }
  82. };
  83. double result2 = np.dot(y1, y2);
  84. Assert.AreEqual(result1, 32);
  85. Assert.AreEqual(Math.Round(result2, 2), 158.02);
  86. }
  87. [TestMethod]
  88. public void maximum()
  89. {
  90. var x1 = new NDArray(new[,] { { 1, 2, 3 }, { 4, 5.1, 6 } });
  91. var x2 = new NDArray(new[,] { { 3, 2, 1 }, { 6, 5.1, 4 } });
  92. var y0 = np.maximum(x1,x2);
  93. var y1 = np.maximum(x1, x2, axis: 0);
  94. var y2 = np.maximum(x1, x2, axis: 1);
  95. var y3 = new NDArray(new[,] { { 3, 2, 3 }, { 6, 5.1, 6 } });
  96. var y4 = new NDArray(new[] { 6, 5.1, 6 });
  97. var y5 = new NDArray(new[] { 3.0, 6 });
  98. Assert.AreEqual(y0, y3);
  99. Assert.AreEqual(y1, y4);
  100. Assert.AreEqual(y2, y5);
  101. }
  102. }
  103. }