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.

PythonTest.cs 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Microsoft.VisualStudio.TestTools.UnitTesting;
  7. using Newtonsoft.Json.Linq;
  8. using NumSharp;
  9. using Tensorflow;
  10. using Tensorflow.Util;
  11. namespace TensorFlowNET.UnitTest
  12. {
  13. /// <summary>
  14. /// Use as base class for test classes to get additional assertions
  15. /// </summary>
  16. public class PythonTest : Python
  17. {
  18. #region python compatibility layer
  19. protected PythonTest self { get => this; }
  20. protected object None {
  21. get { return null; }
  22. }
  23. #endregion
  24. #region pytest assertions
  25. public void assertItemsEqual(ICollection given, ICollection expected)
  26. {
  27. if (given is Hashtable && expected is Hashtable)
  28. {
  29. Assert.AreEqual(JObject.FromObject(expected).ToString(), JObject.FromObject(given).ToString());
  30. return;
  31. }
  32. Assert.IsNotNull(expected);
  33. Assert.IsNotNull(given);
  34. var e = expected.OfType<object>().ToArray();
  35. var g = given.OfType<object>().ToArray();
  36. Assert.AreEqual(e.Length, g.Length, $"The collections differ in length expected {e.Length} but got {g.Length}");
  37. for (int i = 0; i < e.Length; i++)
  38. {
  39. if (g[i] is NDArray && e[i] is NDArray)
  40. assertItemsEqual((g[i] as NDArray).Array, (e[i] as NDArray).Array);
  41. else if (e[i] is ICollection && g[i] is ICollection)
  42. assertEqual(g[i], e[i]);
  43. else
  44. Assert.AreEqual(e[i], g[i], $"Items differ at index {i}, expected {e[i]} but got {g[i]}");
  45. }
  46. }
  47. public void assertAllEqual(ICollection given, ICollection expected)
  48. {
  49. assertItemsEqual(given, expected);
  50. }
  51. public void assertEqual(object given, object expected)
  52. {
  53. if (given is NDArray && expected is NDArray)
  54. {
  55. assertItemsEqual((given as NDArray).Array, (expected as NDArray).Array);
  56. return;
  57. }
  58. if (given is Hashtable && expected is Hashtable)
  59. {
  60. Assert.AreEqual(JObject.FromObject(expected).ToString(), JObject.FromObject(given).ToString());
  61. return;
  62. }
  63. if (given is ICollection && expected is ICollection)
  64. {
  65. assertItemsEqual(given as ICollection, expected as ICollection);
  66. return;
  67. }
  68. Assert.AreEqual(expected, given);
  69. }
  70. public void assertEquals(object given, object expected)
  71. {
  72. assertEqual(given, expected);
  73. }
  74. public void assertIsNotNone(object given)
  75. {
  76. Assert.IsNotNull(given);
  77. }
  78. public void assertFalse(bool cond)
  79. {
  80. Assert.IsFalse(cond);
  81. }
  82. public void assertTrue(bool cond)
  83. {
  84. Assert.IsTrue(cond);
  85. }
  86. #endregion
  87. #region tensor evaluation
  88. protected object _eval_helper(Tensor[] tensors)
  89. {
  90. if (tensors == null)
  91. return null;
  92. //return nest.map_structure(self._eval_tensor, tensors);
  93. return null;
  94. }
  95. //def evaluate(self, tensors) :
  96. // """Evaluates tensors and returns numpy values.
  97. // Args:
  98. // tensors: A Tensor or a nested list/tuple of Tensors.
  99. // Returns:
  100. // tensors numpy values.
  101. // """
  102. // if context.executing_eagerly():
  103. // return self._eval_helper(tensors)
  104. // else:
  105. // sess = ops.get_default_session()
  106. // if sess is None:
  107. // with self.test_session() as sess:
  108. // return sess.run(tensors)
  109. // else:
  110. // return sess.run(tensors)
  111. #endregion
  112. }
  113. }

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。