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.

NameScopeTest.cs 2.8 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Tensorflow;
  4. using Tensorflow.UnitTest;
  5. using static Tensorflow.Binding;
  6. namespace TensorFlowNET.UnitTest.Basics
  7. {
  8. [TestClass]
  9. public class NameScopeTest : GraphModeTestBase
  10. {
  11. string name = "";
  12. [TestMethod]
  13. public void NestedNameScope()
  14. {
  15. Graph g = tf.Graph().as_default();
  16. tf_with(new ops.NameScope("scope1"), scope1 =>
  17. {
  18. name = scope1;
  19. Assert.AreEqual("scope1", g._name_stack);
  20. Assert.AreEqual("scope1/", name);
  21. var const1 = tf.constant(1.0);
  22. Assert.AreEqual("scope1/Const:0", const1.name);
  23. tf_with(new ops.NameScope("scope2"), scope2 =>
  24. {
  25. name = scope2;
  26. Assert.AreEqual("scope1/scope2", g._name_stack);
  27. Assert.AreEqual("scope1/scope2/", name);
  28. var const2 = tf.constant(2.0);
  29. Assert.AreEqual("scope1/scope2/Const:0", const2.name);
  30. });
  31. Assert.AreEqual("scope1", g._name_stack);
  32. var const3 = tf.constant(2.0);
  33. Assert.AreEqual("scope1/Const_1:0", const3.name);
  34. });
  35. g.Dispose();
  36. Assert.AreEqual("", g._name_stack);
  37. }
  38. [TestMethod]
  39. public void NameScopeInEagerMode()
  40. {
  41. tf.enable_eager_execution();
  42. tf_with(new ops.NameScope("scope"), scope =>
  43. {
  44. string name = scope;
  45. var const1 = tf.constant(1.0);
  46. });
  47. tf.compat.v1.disable_eager_execution();
  48. }
  49. [TestMethod, Ignore("Unimplemented Usage")]
  50. public void NestedNameScope_Using()
  51. {
  52. Graph g = tf.Graph().as_default();
  53. using (var name = new ops.NameScope("scope1"))
  54. {
  55. Assert.AreEqual("scope1", g._name_stack);
  56. Assert.AreEqual("scope1/", name);
  57. var const1 = tf.constant(1.0);
  58. Assert.AreEqual("scope1/Const:0", const1.name);
  59. using (var name2 = new ops.NameScope("scope2"))
  60. {
  61. Assert.AreEqual("scope1/scope2", g._name_stack);
  62. Assert.AreEqual("scope1/scope2/", name);
  63. var const2 = tf.constant(2.0);
  64. Assert.AreEqual("scope1/scope2/Const:0", const2.name);
  65. }
  66. Assert.AreEqual("scope1", g._name_stack);
  67. var const3 = tf.constant(2.0);
  68. Assert.AreEqual("scope1/Const_1:0", const3.name);
  69. };
  70. g.Dispose();
  71. Assert.AreEqual("", g._name_stack);
  72. }
  73. }
  74. }