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.

Python.cs 3.8 kB

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using NumSharp;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Text;
  8. namespace Tensorflow
  9. {
  10. /// <summary>
  11. /// Mapping C# functions to Python
  12. /// </summary>
  13. public class Python
  14. {
  15. protected void print(object obj)
  16. {
  17. Console.WriteLine(obj.ToString());
  18. }
  19. protected int len<T>(IEnumerable<T> a)
  20. => a.Count();
  21. protected IEnumerable<int> range(int end)
  22. {
  23. return Enumerable.Range(0, end);
  24. }
  25. public static T New<T>(object args) where T : IPyClass
  26. {
  27. var instance = Activator.CreateInstance<T>();
  28. instance.__init__(instance, args);
  29. return instance;
  30. }
  31. public static void with(IPython py, Action<IPython> action)
  32. {
  33. try
  34. {
  35. py.__enter__();
  36. action(py);
  37. }
  38. catch (Exception ex)
  39. {
  40. Console.WriteLine(ex.ToString());
  41. throw ex;
  42. }
  43. finally
  44. {
  45. py.__exit__();
  46. py.Dispose();
  47. }
  48. }
  49. public static void with<T>(T py, Action<T> action) where T : IPython
  50. {
  51. try
  52. {
  53. py.__enter__();
  54. action(py);
  55. }
  56. catch (Exception ex)
  57. {
  58. Console.WriteLine(ex.ToString());
  59. throw ex;
  60. }
  61. finally
  62. {
  63. py.__exit__();
  64. py.Dispose();
  65. }
  66. }
  67. public static TOut with<TIn, TOut>(TIn py, Func<TIn, TOut> action) where TIn : IPython
  68. {
  69. try
  70. {
  71. py.__enter__();
  72. return action(py);
  73. }
  74. catch (Exception ex)
  75. {
  76. Console.WriteLine(ex.ToString());
  77. return default(TOut);
  78. }
  79. finally
  80. {
  81. py.__exit__();
  82. py.Dispose();
  83. }
  84. }
  85. public static float time()
  86. {
  87. return (float)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
  88. }
  89. public static IEnumerable<(T, T)> zip<T>(NDArray t1, NDArray t2)
  90. {
  91. for (int i = 0; i < t1.size; i++)
  92. yield return (t1.Data<T>(i), t2.Data<T>(i));
  93. }
  94. public static IEnumerable<(T1, T2)> zip<T1, T2>(IList<T1> t1, IList<T2> t2)
  95. {
  96. for (int i = 0; i < t1.Count; i++)
  97. yield return (t1[i], t2[i]);
  98. }
  99. public static IEnumerable<(T1, T2)> zip<T1, T2>(NDArray t1, NDArray t2)
  100. {
  101. for (int i = 0; i < t1.size; i++)
  102. yield return (t1.Data<T1>(i), t2.Data<T2>(i));
  103. }
  104. public static IEnumerable<(int, T)> enumerate<T>(IList<T> values)
  105. {
  106. for (int i = 0; i < values.Count; i++)
  107. yield return (i, values[i]);
  108. }
  109. public static Dictionary<string, object> ConvertToDict(object dyn)
  110. {
  111. var dictionary = new Dictionary<string, object>();
  112. foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(dyn))
  113. {
  114. object obj = propertyDescriptor.GetValue(dyn);
  115. string name = propertyDescriptor.Name;
  116. dictionary.Add(name, obj);
  117. }
  118. return dictionary;
  119. }
  120. }
  121. public interface IPython : IDisposable
  122. {
  123. void __enter__();
  124. void __exit__();
  125. }
  126. public class PyObject<T> where T : IPyClass
  127. {
  128. public T Instance { get; set; }
  129. }
  130. }

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