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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. var handle = Marshal.StringToHGlobalAnsi("bunny");
  62. c_api.TF_SetAttrString(desc, "v", handle, 5);
  63. //var oper = c_api.TF_FinishOperation(desc, s_);
  64. //ASSERT_EQ(TF_Code.TF_OK, s_.Code);
  65. //EXPECT_TF_META(oper, "v", -1, TF_AttrType.TF_ATTR_STRING, 5);
  66. //var value = new char[5];
  67. //c_api.TF_OperationGetAttrString(oper, "v", value, 5, s_);
  68. //EXPECT_EQ(TF_Code.TF_OK, s_.Code);
  69. //EXPECT_EQ("bunny", value, 5));
  70. }
  71. public void Dispose()
  72. {
  73. graph_.Dispose();
  74. s_.Dispose();
  75. }
  76. }
  77. }

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