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.

TensorOperate.cs 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using FluentAssertions;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using NumSharp;
  4. using System.Linq;
  5. using Tensorflow;
  6. using static Tensorflow.Binding;
  7. namespace Tensorflow.UnitTest.TF_API
  8. {
  9. [TestClass]
  10. public class TensorOperate
  11. {
  12. [TestMethod]
  13. public void TransposeTest()
  14. {
  15. var a = tf.constant(np.array(new[, , ,] { { { { 1, 11, 2, 22 } }, { { 3, 33, 4, 44 } } },
  16. { { { 5, 55, 6, 66 } }, { { 7, 77, 8, 88 } } } }));
  17. var b = tf.transpose(a, new[] { 3, 1, 2, 0 });
  18. var transpose_a = tf.constant(np.array(new[, , ,] { { { { 1, 5 } }, { { 3, 7 } } },
  19. { { { 11, 55 } }, { { 33, 77 } } }, { { { 2, 6 } }, { { 4, 8 } } },
  20. { { { 22, 66 } }, { { 44, 88 } } } }));
  21. Assert.IsTrue(Enumerable.SequenceEqual(new[] { 4, 2, 1, 2 }, b.shape));
  22. Assert.IsTrue(Enumerable.SequenceEqual(transpose_a.numpy().ToArray<int>(), b.numpy().ToArray<int>()));
  23. }
  24. [TestMethod]
  25. public void InitTensorTest()
  26. {
  27. var a = tf.constant(new NDArray(new[, ,] { { { 1 }, { 2 }, { 3 } }, { { 4 }, { 5 }, { 6 } } }));
  28. var b = tf.constant(new[, ,] { { { 1 }, { 2 }, { 3 } }, { { 4 }, { 5 }, { 6 } } });
  29. //Test Result : a is OK , and b is error .
  30. Assert.IsTrue(Enumerable.SequenceEqual(new[] { 2, 3, 1 }, a.shape));
  31. Assert.IsTrue(Enumerable.SequenceEqual(new[] { 2, 3, 1 }, b.shape));
  32. }
  33. [TestMethod]
  34. public void ConcatTest()
  35. {
  36. var a = tf.constant(new[,] { { 1, 2 }, { 3, 4 } });
  37. var b = tf.constant(new[,] { { 5, 6 }, { 7, 8 } });
  38. var c = tf.constant(new[,] { { 9, 10 }, { 11, 12 } });
  39. var concatValue = tf.concat(new[] { a, b, c }, axis: 0);
  40. Assert.IsTrue(Enumerable.SequenceEqual(new[] { 6, 2 }, concatValue.shape));
  41. }
  42. [TestMethod]
  43. public void ConcatDoubleTest()
  44. {//double type has some error
  45. var a = tf.constant(new[,] { { 1.0, 2.0 }, { 3.0, 4.0 } });
  46. var b = tf.constant(new[,] { { 5.0, 6.0 }, { 7.0, 8.0 } });
  47. var c = tf.constant(new[,] { { 9.0, 10.0 }, { 11.0, 12.0 } });
  48. var concatValue = tf.concat(new[] { a, b, c }, axis: 0);
  49. Assert.IsTrue(Enumerable.SequenceEqual(new[] { 6, 2 }, concatValue.shape));
  50. }
  51. [TestMethod]
  52. public void SplitTest()
  53. {
  54. var a = tf.constant(new[,] { { 1, 2 }, { 3, 4 } });
  55. var b = tf.constant(new[,] { { 5, 6 }, { 7, 8 } });
  56. var c = tf.constant(new[,] { { 9, 10 }, { 11, 12 } });
  57. var concatValue = tf.concat(new[] { a, b, c }, axis: 0);
  58. var splitValue = tf.split(concatValue, 3, axis: new Tensor(0));
  59. Assert.IsTrue(Enumerable.SequenceEqual(new[] { 2, 2 }, splitValue[0].shape));
  60. }
  61. }
  62. }