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 2.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 Tensorflow;
  8. using Tensorflow.Util;
  9. namespace TensorFlowNET.UnitTest
  10. {
  11. /// <summary>
  12. /// Use as base class for test classes to get additional assertions
  13. /// </summary>
  14. public class PythonTest : Python
  15. {
  16. #region python compatibility layer
  17. protected PythonTest self { get => this; }
  18. protected object None {
  19. get { return null; }
  20. }
  21. #endregion
  22. #region pytest assertions
  23. public void assertItemsEqual(ICollection given, ICollection expected)
  24. {
  25. Assert.IsNotNull(expected);
  26. Assert.IsNotNull(given);
  27. var e = expected.OfType<object>().ToArray();
  28. var g = given.OfType<object>().ToArray();
  29. Assert.AreEqual(e.Length, g.Length, $"The collections differ in length expected {e.Length} but got {g.Length}");
  30. for (int i = 0; i < e.Length; i++)
  31. Assert.AreEqual(e[i], g[i], $"Items differ at index {i}, expected {e[i]} but got {g[i]}");
  32. }
  33. public void assertEqual(object given, object expected)
  34. {
  35. if (given is ICollection && expected is ICollection)
  36. {
  37. assertItemsEqual(given as ICollection, expected as ICollection);
  38. return;
  39. }
  40. Assert.AreEqual(expected, given);
  41. }
  42. public void assertEquals(object given, object expected)
  43. {
  44. assertEqual(given, expected);
  45. }
  46. public void assertIsNotNone(object given)
  47. {
  48. Assert.IsNotNull(given);
  49. }
  50. #endregion
  51. #region tensor evaluation
  52. protected object _eval_helper(Tensor[] tensors)
  53. {
  54. if (tensors == null)
  55. return null;
  56. //return nest.map_structure(self._eval_tensor, tensors);
  57. return null;
  58. }
  59. //def evaluate(self, tensors) :
  60. // """Evaluates tensors and returns numpy values.
  61. // Args:
  62. // tensors: A Tensor or a nested list/tuple of Tensors.
  63. // Returns:
  64. // tensors numpy values.
  65. // """
  66. // if context.executing_eagerly():
  67. // return self._eval_helper(tensors)
  68. // else:
  69. // sess = ops.get_default_session()
  70. // if sess is None:
  71. // with self.test_session() as sess:
  72. // return sess.run(tensors)
  73. // else:
  74. // return sess.run(tensors)
  75. #endregion
  76. }
  77. }

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