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.

MemoryBasicTest.cs 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using NumSharp;
  2. using System;
  3. using Tensorflow.Keras.ArgsDefinition;
  4. using Tensorflow.Keras.Engine.DataAdapters;
  5. using static Tensorflow.Binding;
  6. using static Tensorflow.KerasApi;
  7. namespace Tensorflow
  8. {
  9. class MemoryBasicTest
  10. {
  11. public Action<int, int> Placeholder
  12. => (epoch, iterate) =>
  13. {
  14. var ph = array_ops.placeholder(tf.float32, (10, 512, 512, 3));
  15. };
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. public Action<int, int> Constant
  20. => (epoch, iterate) =>
  21. {
  22. var tensor = tf.constant(3112.0f);
  23. };
  24. public Action<int, int> Constant2x3
  25. => (epoch, iterate) =>
  26. {
  27. var nd = np.arange(1000).reshape(10, 100);
  28. var tensor = tf.constant(nd);
  29. var data = tensor.numpy();
  30. };
  31. public Action<int, int> ConstantString
  32. => (epoch, iterate) =>
  33. {
  34. var tensor = tf.constant(new string[]
  35. {
  36. "Biden immigration bill would put millions of illegal immigrants on 8-year fast-track to citizenship",
  37. "The Associated Press, which also reported that the eight-year path is in the bill.",
  38. "The bill would also include provisions to stem the flow of migration by addressing root causes of migration from south of the border."
  39. });
  40. var data = tensor.numpy();
  41. };
  42. public Action<int, int> Variable
  43. => (epoch, iterate) =>
  44. {
  45. var nd = np.arange(1 * 256 * 256 * 3).reshape(1, 256, 256, 3);
  46. ResourceVariable variable = tf.Variable(nd);
  47. };
  48. public Action<int, int> VariableRead
  49. => (epoch, iterate) =>
  50. {
  51. var nd = np.zeros(1 * 256 * 256 * 3).astype(np.float32).reshape(1, 256, 256, 3);
  52. ResourceVariable variable = tf.Variable(nd);
  53. for (int i = 0; i< 10; i++)
  54. {
  55. var v = variable.numpy();
  56. }
  57. };
  58. public Action<int, int> VariableAssign
  59. => (epoch, iterate) =>
  60. {
  61. ResourceVariable variable = tf.Variable(3112f);
  62. AssignVariable(variable);
  63. for (int i = 0; i < 100; i++)
  64. {
  65. var v = variable.numpy();
  66. if ((float)v != 1984f)
  67. throw new ValueError("");
  68. }
  69. };
  70. void AssignVariable(IVariableV1 v)
  71. {
  72. using var tensor = tf.constant(1984f);
  73. v.assign(tensor);
  74. }
  75. public Action<int, int> MathAdd
  76. => (epoch, iterate) =>
  77. {
  78. var x = tf.constant(3112.0f);
  79. var y = tf.constant(3112.0f);
  80. var z = x + y;
  81. };
  82. public Action<int, int> Gradient
  83. => (epoch, iterate) =>
  84. {
  85. var w = tf.constant(3112.0f);
  86. using var tape = tf.GradientTape();
  87. tape.watch(w);
  88. var loss = w * w;
  89. var grad = tape.gradient(loss, w);
  90. };
  91. public Action<int, int> Conv2DWithTensor
  92. => (epoch, iterate) =>
  93. {
  94. var input = array_ops.zeros((10, 32, 32, 3), dtypes.float32);
  95. var filter = array_ops.zeros((3, 3, 3, 32), dtypes.float32);
  96. var strides = new[] { 1, 1, 1, 1 };
  97. var dilations = new[] { 1, 1, 1, 1 };
  98. var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName,
  99. "Conv2D", null,
  100. null,
  101. input, filter,
  102. "strides", strides,
  103. "use_cudnn_on_gpu", true,
  104. "padding", "VALID",
  105. "explicit_paddings", new int[0],
  106. "data_format", "NHWC",
  107. "dilations", dilations);
  108. };
  109. public Action<int, int> Conv2DWithVariable
  110. => (epoch, iterate) =>
  111. {
  112. var input = array_ops.zeros((10, 32, 32, 3), dtypes.float32);
  113. var filter = tf.Variable(array_ops.zeros((3, 3, 3, 32), dtypes.float32));
  114. var strides = new[] { 1, 1, 1, 1 };
  115. var dilations = new[] { 1, 1, 1, 1 };
  116. var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName,
  117. "Conv2D", null,
  118. null,
  119. input, filter,
  120. "strides", strides,
  121. "use_cudnn_on_gpu", true,
  122. "padding", "VALID",
  123. "explicit_paddings", new int[0],
  124. "data_format", "NHWC",
  125. "dilations", dilations);
  126. };
  127. public Action<int, int> Dataset
  128. => (epoch, iterate) =>
  129. {
  130. TensorShape shape = (16, 32, 32, 3);
  131. var images = np.arange(shape.size).astype(np.float32).reshape(shape.dims);
  132. var data_handler = new DataHandler(new DataHandlerArgs
  133. {
  134. X = images,
  135. BatchSize = 2,
  136. StepsPerEpoch = -1,
  137. InitialEpoch = 0,
  138. Epochs = 2,
  139. MaxQueueSize = 10,
  140. Workers = 1,
  141. UseMultiprocessing = false,
  142. StepsPerExecution = tf.Variable(1)
  143. });
  144. /*foreach (var (_epoch, iterator) in data_handler.enumerate_epochs())
  145. {
  146. foreach (var step in data_handler.steps())
  147. iterator.next();
  148. }*/
  149. };
  150. }
  151. }