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.Indexing.Test.cs 2.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. using static Tensorflow.Binding;
  9. namespace TensorFlowNET.UnitTest.NumPy
  10. {
  11. /// <summary>
  12. /// https://numpy.org/doc/stable/user/basics.indexing.html
  13. /// </summary>
  14. [TestClass]
  15. public class ArrayIndexingTest : EagerModeTestBase
  16. {
  17. [TestMethod]
  18. public void int_params()
  19. {
  20. var x = np.arange(24).reshape((2, 3, 4));
  21. x[1, 2, 3] = 1;
  22. var y = x[1, 2, 3];
  23. Assert.AreEqual(y.shape, Shape.Scalar);
  24. Assert.AreEqual(y, 1);
  25. x[0, 0] = new[] { 3, 1, 1, 2 };
  26. y = x[0, 0];
  27. Assert.AreEqual(y.shape, 4);
  28. Assert.AreEqual(y, new[] { 3, 1, 1, 2 });
  29. y = x[0];
  30. Assert.AreEqual(y.shape, (3, 4));
  31. var z = np.arange(12).reshape((3, 4));
  32. x[1] = z;
  33. Assert.AreEqual(x[1], z);
  34. }
  35. [TestMethod]
  36. public void slice_params()
  37. {
  38. var x = np.arange(12).reshape((3, 4));
  39. var y = x[new Slice(0, 1), new Slice(2)];
  40. Assert.AreEqual(y.shape, (1, 2));
  41. Assert.AreEqual(y, np.array(new[] { 2, 3 }).reshape((1, 2)));
  42. }
  43. [TestMethod]
  44. public void slice_string_params()
  45. {
  46. var x = np.arange(12).reshape((3, 4));
  47. var y = x[Slice.ParseSlices("0:1,2:")];
  48. Assert.AreEqual(y.shape, (1, 2));
  49. Assert.AreEqual(y, np.array(new[] { 2, 3 }).reshape((1, 2)));
  50. }
  51. [TestMethod]
  52. public void slice_out_bound()
  53. {
  54. var input_shape = tf.constant(new int[] { 1, 1 });
  55. var input_shape_val = input_shape.numpy();
  56. input_shape_val[(int)input_shape.size - 1] = 1;
  57. input_shape.Dispose();
  58. }
  59. }
  60. }