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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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, ref AttrValue attr_value, Status s)
  12. {
  13. var buffer = new Buffer();
  14. c_api.TF_OperationGetAttrValueProto(oper, attr_name, buffer, s);
  15. attr_value = AttrValue.Parser.ParseFrom(buffer.Data);
  16. buffer.Dispose();
  17. return s.Code == TF_Code.TF_OK;
  18. }
  19. public static void PlaceholderHelper(Graph graph, Status s, string name, TF_DataType dtype, long[] dims, ref Operation op)
  20. {
  21. var desc = c_api.TF_NewOperation(graph, "Placeholder", name);
  22. c_api.TF_SetAttrType(desc, "dtype", dtype);
  23. if(dims != null)
  24. {
  25. c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length);
  26. }
  27. op = c_api.TF_FinishOperation(desc, s);
  28. s.Check();
  29. }
  30. public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
  31. {
  32. Operation op = null;
  33. PlaceholderHelper(graph, s, name, dtype, dims, ref op);
  34. return op;
  35. }
  36. public static void ConstHelper(Tensor t, Graph graph, Status s, string name, ref Operation op)
  37. {
  38. var desc = c_api.TF_NewOperation(graph, "Const", name);
  39. c_api.TF_SetAttrTensor(desc, "value", t, s);
  40. s.Check();
  41. c_api.TF_SetAttrType(desc, "dtype", t.dtype);
  42. op = c_api.TF_FinishOperation(desc, s);
  43. s.Check();
  44. if(op == null)
  45. {
  46. throw new Exception("c_api.TF_FinishOperation failed.");
  47. }
  48. }
  49. public static Operation Const(Tensor t, Graph graph, Status s, string name)
  50. {
  51. Operation op = null;
  52. ConstHelper(t, graph, s, name, ref op);
  53. return op;
  54. }
  55. public static Operation ScalarConst(int v, Graph graph, Status s, string name = "Const")
  56. {
  57. return Const(new Tensor(v), graph, s, name);
  58. }
  59. }
  60. }

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

Contributors (1)