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.

CApiAttributesTestcs.cs 3.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using Tensorflow;
  7. namespace TensorFlowNET.UnitTest
  8. {
  9. /// <summary>
  10. /// tensorflow\c\c_api_test.cc
  11. /// `class CApiAttributesTest`
  12. /// </summary>
  13. [TestClass]
  14. public class CApiAttributesTestcs : CApiTest, IDisposable
  15. {
  16. private Graph graph_;
  17. private int counter_;
  18. private Status s_;
  19. public CApiAttributesTestcs()
  20. {
  21. s_ = new Status();
  22. graph_ = new Graph();
  23. }
  24. private OperationDescription init(string type)
  25. {
  26. // Construct op_name to match the name used by REGISTER_OP in the
  27. // ATTR_TEST_REGISTER calls above.
  28. string op_name = "CApiAttributesTestOp";
  29. if (type.Contains("list("))
  30. {
  31. op_name += "List";
  32. type = type.Substring(5, type.Length - 6);
  33. }
  34. op_name += type;
  35. return c_api.TF_NewOperation(graph_, op_name, $"name{counter_++}");
  36. }
  37. /// <summary>
  38. /// REGISTER_OP for CApiAttributesTest test cases.
  39. /// Registers two ops, each with a single attribute called 'v'.
  40. /// The attribute in one op will have a type 'type', the other
  41. /// will have list(type).
  42. /// </summary>
  43. /// <param name="type"></param>
  44. private void ATTR_TEST_REGISTER_OP(string type)
  45. {
  46. }
  47. private void EXPECT_TF_META(Operation oper, string attr_name, int expected_list_size, TF_AttrType expected_type, uint expected_total_size)
  48. {
  49. var m = c_api.TF_OperationGetAttrMetadata(oper, attr_name, s_);
  50. EXPECT_EQ(TF_Code.TF_OK, s_.Code);
  51. char e = expected_list_size >= 0 ? (char)1 : (char)0;
  52. /*EXPECT_EQ(e, m.is_list);
  53. EXPECT_EQ(expected_list_size, m.list_size);
  54. EXPECT_EQ(expected_type, m.type);
  55. EXPECT_EQ(expected_total_size, m.total_size);*/
  56. }
  57. [TestMethod]
  58. public void String()
  59. {
  60. var desc = init("string");
  61. c_api.TF_SetAttrString(desc, "v", "bunny", 5);
  62. var oper = c_api.TF_FinishOperation(desc, s_);
  63. //ASSERT_EQ(TF_Code.TF_OK, s_.Code);
  64. //EXPECT_TF_META(oper, "v", -1, TF_AttrType.TF_ATTR_STRING, 5);
  65. //var value = new char[5];
  66. //c_api.TF_OperationGetAttrString(oper, "v", value, 5, s_);
  67. //EXPECT_EQ(TF_Code.TF_OK, s_.Code);
  68. //EXPECT_EQ("bunny", value, 5));
  69. }
  70. [TestMethod]
  71. public void GetAttributesTest()
  72. {
  73. var desc = graph_.NewOperation("Placeholder", "node");
  74. desc.SetAttrType("dtype", TF_DataType.TF_FLOAT);
  75. long[] ref_shape = new long[3] { 1, 2, 3 };
  76. desc.SetAttrShape("shape", ref_shape);
  77. var oper = desc.FinishOperation(s_);
  78. var metadata = oper.GetAttributeMetadata("shape", s_);
  79. }
  80. public void Dispose()
  81. {
  82. graph_.Dispose();
  83. s_.Dispose();
  84. }
  85. }
  86. }

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