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.

c_test_util.cs 2.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using Tensorflow;
  6. using Buffer = Tensorflow.Buffer;
  7. namespace TensorFlowNET.UnitTest
  8. {
  9. public static class c_test_util
  10. {
  11. public static bool GetAttrValue(Operation oper, string attr_name, AttrValue attr_value, Status s)
  12. {
  13. var buffer = c_api.TF_NewBuffer();
  14. return s.Code == TF_Code.TF_OK;
  15. }
  16. public static void PlaceholderHelper(Graph graph, Status s, string name, TF_DataType dtype, long[] dims, ref Operation op)
  17. {
  18. var desc = c_api.TF_NewOperation(graph, "Placeholder", name);
  19. c_api.TF_SetAttrType(desc, "dtype", dtype);
  20. if(dims != null)
  21. {
  22. c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length);
  23. }
  24. op = c_api.TF_FinishOperation(desc, s);
  25. s.Check();
  26. }
  27. public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
  28. {
  29. Operation op = null;
  30. PlaceholderHelper(graph, s, name, dtype, dims, ref op);
  31. return op;
  32. }
  33. public static void ConstHelper(Tensor t, Graph graph, Status s, string name, ref Operation op)
  34. {
  35. var desc = c_api.TF_NewOperation(graph, "Const", name);
  36. c_api.TF_SetAttrTensor(desc, "value", t, s);
  37. s.Check();
  38. c_api.TF_SetAttrType(desc, "dtype", t.dtype);
  39. op = c_api.TF_FinishOperation(desc, s);
  40. s.Check();
  41. if(op == null)
  42. {
  43. throw new Exception("c_api.TF_FinishOperation failed.");
  44. }
  45. }
  46. public static Operation Const(Tensor t, Graph graph, Status s, string name)
  47. {
  48. Operation op = null;
  49. ConstHelper(t, graph, s, name, ref op);
  50. return op;
  51. }
  52. public static Operation ScalarConst(int v, Graph graph, Status s, string name = "Const")
  53. {
  54. return Const(new Tensor(v), graph, s, name);
  55. }
  56. }
  57. }

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

Contributors (1)