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.

CApiColocationTest.cs 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 CApiColocationTest`
  12. /// </summary>
  13. [TestClass]
  14. public class CApiColocationTest : CApiTest, IDisposable
  15. {
  16. private Graph graph_ = new Graph();
  17. private Status s_ = new Status();
  18. private Operation feed1_;
  19. private Operation feed2_;
  20. private Operation constant_;
  21. private OperationDescription desc_;
  22. [TestInitialize]
  23. public void SetUp()
  24. {
  25. feed1_ = c_test_util.Placeholder(graph_, s_, "feed1");
  26. s_.Check();
  27. feed2_ = c_test_util.Placeholder(graph_, s_, "feed2");
  28. s_.Check();
  29. constant_ = c_test_util.ScalarConst(10, graph_, s_);
  30. s_.Check();
  31. desc_ = c_api.TF_NewOperation(graph_, "AddN", "add");
  32. s_.Check();
  33. TF_Output[] inputs = { new TF_Output(feed1_, 0), new TF_Output(constant_, 0) };
  34. desc_.AddInputList(inputs);
  35. s_.Check();
  36. }
  37. private void SetViaStringList(OperationDescription desc, string[] list)
  38. {
  39. string[] list_ptrs = new string[list.Length];
  40. uint[] list_lens = new uint[list.Length];
  41. StringVectorToArrays(list, list_ptrs, list_lens);
  42. c_api.TF_SetAttrStringList(desc, "_class", list_ptrs, list_lens, list.Length);
  43. }
  44. private void StringVectorToArrays(string[] v, string[] ptrs, uint[] lens)
  45. {
  46. for (int i = 0; i < v.Length; ++i)
  47. {
  48. ptrs[i] = v[i];// Marshal.StringToHGlobalAnsi(v[i]);
  49. lens[i] = (uint)v[i].Length;
  50. }
  51. }
  52. private void FinishAndVerify(OperationDescription desc, string[] expected)
  53. {
  54. Operation op = c_api.TF_FinishOperation(desc_, s_);
  55. ASSERT_EQ(TF_Code.TF_OK, s_.Code);
  56. VerifyCollocation(op, expected);
  57. }
  58. private void VerifyCollocation(Operation op, string[] expected)
  59. {
  60. var handle = c_api.TF_OperationGetAttrMetadata(op, "_class", s_);
  61. TF_AttrMetadata m = new TF_AttrMetadata();
  62. if (expected.Length == 0)
  63. {
  64. ASSERT_EQ(TF_Code.TF_INVALID_ARGUMENT, s_.Code);
  65. EXPECT_EQ("Operation 'add' has no attr named '_class'.", s_.Message);
  66. return;
  67. }
  68. EXPECT_EQ(TF_Code.TF_OK, s_.Code);
  69. EXPECT_EQ(1, m.is_list);
  70. EXPECT_EQ(expected.Length, m.list_size);
  71. EXPECT_EQ(TF_AttrType.TF_ATTR_STRING, m.type);
  72. string[] values = new string[expected.Length];
  73. uint[] lens = new uint[expected.Length];
  74. string[] storage = new string[m.total_size];
  75. //c_api.TF_OperationGetAttrStringList(op, "_class", values, lens, expected.Length, storage, m.total_size, s_);
  76. EXPECT_EQ(TF_Code.TF_OK, s_.Code);
  77. for (int i = 0; i < expected.Length; ++i)
  78. {
  79. EXPECT_EQ(expected[i], values[i] + lens[i]);
  80. }
  81. }
  82. [TestMethod]
  83. public void ColocateWith()
  84. {
  85. c_api.TF_ColocateWith(desc_, feed1_);
  86. FinishAndVerify(desc_, new string[] { "loc:@feed1" });
  87. }
  88. [TestMethod]
  89. public void StringList()
  90. {
  91. SetViaStringList(desc_, new string[] { "loc:@feed1" });
  92. FinishAndVerify(desc_, new string[] { "loc:@feed1" });
  93. }
  94. [TestCleanup]
  95. public void Dispose()
  96. {
  97. graph_.Dispose();
  98. s_.Dispose();
  99. }
  100. }
  101. }

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

Contributors (1)