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

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

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