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.

Array.Creation.Test.cs 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.NumPy;
  7. namespace TensorFlowNET.UnitTest.Numpy
  8. {
  9. /// <summary>
  10. /// https://numpy.org/doc/stable/reference/routines.array-creation.html
  11. /// </summary>
  12. [TestClass]
  13. public class NumpyArrayCreationTest : EagerModeTestBase
  14. {
  15. [TestMethod]
  16. public void empty_zeros_ones_full()
  17. {
  18. var empty = np.empty((2, 2));
  19. var zeros = np.zeros((2, 2));
  20. var ones = np.ones((2, 2));
  21. var full = np.full((2, 2), 0.1f);
  22. }
  23. [TestMethod]
  24. public void arange()
  25. {
  26. var x = np.arange(3);
  27. AssetSequenceEqual(new[] { 0, 1, 2 }, x.Data<int>());
  28. x = np.arange(3f);
  29. Assert.IsTrue(Equal(new float[] { 0, 1, 2 }, x.Data<float>()));
  30. var y = np.arange(3, 7);
  31. AssetSequenceEqual(new[] { 3, 4, 5, 6 }, y.Data<int>());
  32. y = np.arange(3, 7, 2);
  33. AssetSequenceEqual(new[] { 3, 5 }, y.Data<int>());
  34. }
  35. [TestMethod]
  36. public void array()
  37. {
  38. var x = np.array(1, 2, 3);
  39. AssetSequenceEqual(new[] { 1, 2, 3 }, x.Data<int>());
  40. x = np.array(new[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } });
  41. AssetSequenceEqual(new[] { 1, 2, 3, 4, 5, 6 }, x.Data<int>());
  42. }
  43. [TestMethod]
  44. public void eye()
  45. {
  46. var x = np.eye(3, k: 1);
  47. Assert.IsTrue(Equal(new double[] { 0, 1, 0, 0, 0, 1, 0, 0, 0 }, x.Data<double>()));
  48. }
  49. [TestMethod]
  50. public void linspace()
  51. {
  52. var x = np.linspace(2.0, 3.0, num: 5);
  53. Assert.IsTrue(Equal(new double[] { 2, 2.25, 2.5, 2.75, 3 }, x.Data<double>()));
  54. x = np.linspace(2.0, 3.0, num: 5, endpoint: false);
  55. Assert.IsTrue(Equal(new double[] { 2, 2.2, 2.4, 2.6, 2.8 }, x.Data<double>()));
  56. }
  57. [TestMethod]
  58. public void meshgrid()
  59. {
  60. var x = np.linspace(0, 1, num: 3);
  61. var y = np.linspace(0, 1, num: 2);
  62. var (xv, yv) = np.meshgrid(x, y);
  63. Assert.IsTrue(Equal(new double[] { 0, 0.5, 1, 0, 0.5, 1 }, xv.Data<double>()));
  64. Assert.IsTrue(Equal(new double[] { 0, 0, 0, 1, 1, 1 }, yv.Data<double>()));
  65. (xv, yv) = np.meshgrid(x, y, sparse: true);
  66. Assert.IsTrue(Equal(new double[] { 0, 0.5, 1 }, xv.Data<double>()));
  67. AssetSequenceEqual(new long[] { 1, 3 }, xv.shape.dims);
  68. Assert.IsTrue(Equal(new double[] { 0, 1 }, yv.Data<double>()));
  69. AssetSequenceEqual(new long[] { 2, 1 }, yv.shape.dims);
  70. }
  71. }
  72. }