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.

Tensorflow.cs 1.7 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using TF_DataType = Tensorflow.DataType;
  6. using attr_value_pb2 = Tensorflow;
  7. using Tensorflow;
  8. namespace TensorFlowNET.Core
  9. {
  10. public static class Tensorflow
  11. {
  12. public delegate void Deallocator(IntPtr data, IntPtr size, IntPtr deallocatorData);
  13. public static unsafe Tensor constant(object value)
  14. {
  15. var g = ops.get_default_graph();
  16. var tensor_value = new attr_value_pb2.AttrValue();
  17. var tensor_pb = tensor_util.make_tensor_proto(value);
  18. tensor_value.Tensor = tensor_pb;
  19. var dtype_value = new attr_value_pb2.AttrValue
  20. {
  21. Type = tensor_value.Tensor.Dtype,
  22. };
  23. var attrs = new Dictionary<string, AttrValue>();
  24. attrs["dtype"] = dtype_value;
  25. attrs["value"] = tensor_value;
  26. var const_tensor = g.create_op("Const", null, new TF_DataType[] { dtype_value.Type }, attrs: attrs).outputs[0];
  27. return const_tensor;
  28. }
  29. public static Deallocator FreeTensorDataDelegate = FreeTensorData;
  30. [MonoPInvokeCallback(typeof(Deallocator))]
  31. internal static void FreeTensorData(IntPtr data, IntPtr len, IntPtr closure)
  32. {
  33. Marshal.FreeHGlobal(data);
  34. }
  35. public static string VERSION => Marshal.PtrToStringAnsi(c_api.TF_Version());
  36. public static Graph get_default_graph()
  37. {
  38. return ops.get_default_graph();
  39. }
  40. public static Graph Graph()
  41. {
  42. Graph g = new Graph(c_api.TF_NewGraph());
  43. return g;
  44. }
  45. }
  46. }

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

Contributors (1)