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

6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using NumSharp.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Tensorflow
  6. {
  7. /// <summary>
  8. /// Mapping C# functions to Python
  9. /// </summary>
  10. public class Python
  11. {
  12. protected void print(object obj)
  13. {
  14. Console.WriteLine(obj.ToString());
  15. }
  16. public static void with(IPython py, Action<IPython> action)
  17. {
  18. try
  19. {
  20. py.__enter__();
  21. action(py);
  22. }
  23. catch (Exception ex)
  24. {
  25. Console.WriteLine(ex.ToString());
  26. throw ex;
  27. }
  28. finally
  29. {
  30. py.__exit__();
  31. py.Dispose();
  32. }
  33. }
  34. public static void with<T>(IPython py, Action<T> action) where T : IPython
  35. {
  36. try
  37. {
  38. py.__enter__();
  39. action((T)py);
  40. }
  41. catch (Exception ex)
  42. {
  43. Console.WriteLine(ex.ToString());
  44. throw ex;
  45. }
  46. finally
  47. {
  48. py.__exit__();
  49. py.Dispose();
  50. }
  51. }
  52. public static IEnumerable<(T, T)> zip<T>(NDArray t1, NDArray t2)
  53. {
  54. int index = 0;
  55. yield return(t1.Data<T>(index), t2.Data<T>(index));
  56. }
  57. public static IEnumerable<(T, T)> zip<T>(IList<T> t1, IList<T> t2)
  58. {
  59. for (int i = 0; i < t1.Count; i++)
  60. yield return (t1[i], t2[i]);
  61. }
  62. }
  63. public interface IPython : IDisposable
  64. {
  65. void __enter__();
  66. void __exit__();
  67. }
  68. }

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