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.

VariableTest.cs 3.4 kB

6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using Tensorflow;
  6. using NumSharp.Core;
  7. namespace TensorFlowNET.UnitTest
  8. {
  9. [TestClass]
  10. public class VariableTest : Python
  11. {
  12. [TestMethod]
  13. public void Initializer()
  14. {
  15. var x = tf.Variable(10, name: "x");
  16. using (var session = tf.Session())
  17. {
  18. session.run(x.initializer);
  19. var result = session.run(x);
  20. Assert.AreEqual(10, (int)result);
  21. }
  22. }
  23. [TestMethod]
  24. public void StringVar()
  25. {
  26. var mammal1 = tf.Variable("Elephant", "var1", tf.chars);
  27. var mammal2 = tf.Variable("Tiger");
  28. }
  29. [TestMethod]
  30. public void SimpleScope()
  31. {
  32. with(tf.variable_scope("foo"), delegate
  33. {
  34. with(tf.variable_scope("bar"), delegate
  35. {
  36. var v = tf.get_variable("v", new TensorShape(1));
  37. });
  38. });
  39. }
  40. [TestMethod]
  41. public void ScalarVar()
  42. {
  43. var x = tf.constant(3, name: "x");
  44. var y = tf.Variable(x + 1, name: "y");
  45. var model = tf.global_variables_initializer();
  46. using (var session = tf.Session())
  47. {
  48. session.run(model);
  49. int result = session.run(y);
  50. Assert.AreEqual(result, 4);
  51. }
  52. }
  53. [TestMethod]
  54. public void Assign1()
  55. {
  56. with<Graph>(tf.Graph().as_default(), graph =>
  57. {
  58. var variable = tf.Variable(31, name: "tree");
  59. var init = tf.global_variables_initializer();
  60. var sess = tf.Session(graph);
  61. sess.run(init);
  62. var result = sess.run(variable);
  63. Assert.IsTrue((int)result == 31);
  64. var assign = variable.assign(12);
  65. result = sess.run(assign);
  66. Assert.IsTrue((int)result == 12);
  67. });
  68. }
  69. [TestMethod]
  70. public void Assign2()
  71. {
  72. var v1 = tf.Variable(10.0f, name: "v1"); //tf.get_variable("v1", shape: new TensorShape(3), initializer: tf.zeros_initializer);
  73. var inc_v1 = v1.assign(v1 + 1.0f);
  74. // Add an op to initialize the variables.
  75. var init_op = tf.global_variables_initializer();
  76. with<Session>(tf.Session(), sess =>
  77. {
  78. sess.run(init_op);
  79. // o some work with the model.
  80. inc_v1.op.run();
  81. });
  82. }
  83. /// <summary>
  84. /// https://databricks.com/tensorflow/variables
  85. /// </summary>
  86. [TestMethod]
  87. public void Add()
  88. {
  89. int result = 0;
  90. Tensor x = tf.Variable(10, name: "x");
  91. var init_op = tf.global_variables_initializer();
  92. using (var session = tf.Session())
  93. {
  94. session.run(init_op);
  95. for(int i = 0; i < 5; i++)
  96. {
  97. x = x + 1;
  98. result = session.run(x);
  99. print(result);
  100. }
  101. }
  102. Assert.AreEqual(15, result);
  103. }
  104. }
  105. }

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