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

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