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.

OperationsTest.cs 16 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using NumSharp;
  7. using Tensorflow;
  8. using Buffer = Tensorflow.Buffer;
  9. namespace TensorFlowNET.UnitTest
  10. {
  11. [TestClass]
  12. public class OperationsTest
  13. {
  14. /// <summary>
  15. /// Port from tensorflow\c\c_api_test.cc
  16. /// `TEST(CAPI, GetAllOpList)`
  17. /// </summary>
  18. [TestMethod]
  19. public void GetAllOpList()
  20. {
  21. var handle = c_api.TF_GetAllOpList();
  22. var buffer = new Buffer(handle);
  23. var op_list = OpList.Parser.ParseFrom(buffer);
  24. var _registered_ops = new Dictionary<string, OpDef>();
  25. foreach (var op_def in op_list.Op)
  26. _registered_ops[op_def.Name] = op_def;
  27. // r1.14 added NN op
  28. var op = _registered_ops.FirstOrDefault(x => x.Key == "NearestNeighbors");
  29. Assert.IsTrue(op_list.Op.Count > 1000);
  30. }
  31. [TestMethod]
  32. public void addInPlaceholder()
  33. {
  34. var a = tf.placeholder(tf.float32);
  35. var b = tf.placeholder(tf.float32);
  36. var c = tf.add(a, b);
  37. using(var sess = tf.Session())
  38. {
  39. var o = sess.run(c,
  40. new FeedItem(a, 3.0f),
  41. new FeedItem(b, 2.0f));
  42. Assert.AreEqual((float)o, 5.0f);
  43. }
  44. }
  45. [TestMethod]
  46. public void addInConstant()
  47. {
  48. var a = tf.constant(4.0f);
  49. var b = tf.constant(5.0f);
  50. var c = tf.add(a, b);
  51. using (var sess = tf.Session())
  52. {
  53. var o = sess.run(c);
  54. Assert.AreEqual((float)o, 9.0f);
  55. }
  56. }
  57. [TestMethod]
  58. public void addOpTests()
  59. {
  60. const int rows = 2; // to avoid broadcasting effect
  61. const int cols = 10;
  62. #region intTest
  63. const int firstIntVal = 2;
  64. const int secondIntVal = 3;
  65. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  66. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  67. var intResult = firstIntFeed.Sum() + secondIntFeed.Sum();
  68. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  69. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  70. var c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  71. using (var sess = tf.Session())
  72. {
  73. var o = sess.run(c,
  74. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  75. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  76. Assert.AreEqual((int)o, intResult);
  77. }
  78. // Testing `operator +(Tensor x, Tensor y)`
  79. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  80. using (var sess = tf.Session())
  81. {
  82. var o = sess.run(c,
  83. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  84. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  85. Assert.AreEqual((int)o, intResult);
  86. }
  87. // Testing `operator +(Tensor x, int y)`
  88. c = tf.reduce_sum(tf.reduce_sum(a + secondIntVal, 1));
  89. using (var sess = tf.Session())
  90. {
  91. var o = sess.run(c,
  92. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  93. Assert.AreEqual((int)o, intResult);
  94. }
  95. // Testing `operator +(int x, Tensor y)`
  96. c = tf.reduce_sum(tf.reduce_sum(secondIntVal + a, 1));
  97. using (var sess = tf.Session())
  98. {
  99. var o = sess.run(c,
  100. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  101. Assert.AreEqual((int)o, intResult);
  102. }
  103. #endregion
  104. #region floatTest
  105. const float firstFloatVal = 2.0f;
  106. const float secondFloatVal = 3.0f;
  107. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  108. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  109. var floatResult = firstFloatFeed.Sum() + secondFloatFeed.Sum();
  110. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  111. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  112. c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  113. using (var sess = tf.Session())
  114. {
  115. var o = sess.run(c,
  116. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  117. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  118. Assert.AreEqual((float)o, floatResult);
  119. }
  120. // Testing `operator +(Tensor x, Tensor y)
  121. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  122. using (var sess = tf.Session())
  123. {
  124. var o = sess.run(c,
  125. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  126. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  127. Assert.AreEqual((float)o, floatResult);
  128. }
  129. // Testing `operator +(Tensor x, float y)
  130. c = tf.reduce_sum(tf.reduce_sum(a + secondFloatVal, 1));
  131. using (var sess = tf.Session())
  132. {
  133. var o = sess.run(c,
  134. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  135. Assert.AreEqual((float)o, floatResult);
  136. }
  137. // Testing `operator +(float x, Tensor y)
  138. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal + a, 1));
  139. using (var sess = tf.Session())
  140. {
  141. var o = sess.run(c,
  142. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  143. Assert.AreEqual((float)o, floatResult);
  144. }
  145. #endregion
  146. #region doubleTest
  147. const double firstDoubleVal = 2.0;
  148. const double secondDoubleVal = 3.0;
  149. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  150. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  151. var doubleResult = firstDoubleFeed.Sum() + secondDoubleFeed.Sum();
  152. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  153. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  154. c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  155. using (var sess = tf.Session())
  156. {
  157. var o = sess.run(c,
  158. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  159. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  160. Assert.AreEqual((double)o, doubleResult);
  161. }
  162. // Testing `operator +(Tensor x, Tensor y)
  163. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  164. using (var sess = tf.Session())
  165. {
  166. var o = sess.run(c,
  167. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  168. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  169. Assert.AreEqual((double)o, doubleResult);
  170. }
  171. // Testing `operator +(Tensor x, double y)
  172. c = tf.reduce_sum(tf.reduce_sum(a + secondFloatVal, 1));
  173. using (var sess = tf.Session())
  174. {
  175. var o = sess.run(c,
  176. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  177. Assert.AreEqual((double)o, doubleResult);
  178. }
  179. // Testing `operator +(double x, Tensor y)
  180. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal + a, 1));
  181. using (var sess = tf.Session())
  182. {
  183. var o = sess.run(c,
  184. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  185. Assert.AreEqual((double)o, doubleResult);
  186. }
  187. #endregion
  188. }
  189. [TestMethod]
  190. public void subOpTests()
  191. {
  192. const int rows = 2; // to avoid broadcasting effect
  193. const int cols = 10;
  194. #region intTest
  195. const int firstIntVal = -2;
  196. const int secondIntVal = 3;
  197. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  198. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  199. var intResult = firstIntFeed.Sum() - secondIntFeed.Sum();
  200. var intResultTwo = -firstIntFeed.Sum();
  201. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  202. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  203. var c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  204. using (var sess = tf.Session())
  205. {
  206. var o = sess.run(c,
  207. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  208. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  209. Assert.AreEqual((int)o, intResult);
  210. }
  211. // Testing `operator -(Tensor x, Tensor y)
  212. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  213. using (var sess = tf.Session())
  214. {
  215. var o = sess.run(c,
  216. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  217. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  218. Assert.AreEqual((int)o, intResult);
  219. }
  220. // Testing `operator -(Tensor x, int y)
  221. c = tf.reduce_sum(tf.reduce_sum(a - secondIntVal, 1));
  222. using (var sess = tf.Session())
  223. {
  224. var o = sess.run(c,
  225. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  226. Assert.AreEqual((int)o, intResult);
  227. }
  228. // Testing `operator -(int x, Tensor y)
  229. c = tf.reduce_sum(tf.reduce_sum(secondIntVal - a, 1));
  230. using (var sess = tf.Session())
  231. {
  232. var o = sess.run(c,
  233. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  234. Assert.AreEqual((int)o, Math.Abs(intResult));
  235. }
  236. // Testing `operator -(Tensor x)
  237. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  238. using (var sess = tf.Session())
  239. {
  240. var o = sess.run(c,
  241. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  242. Assert.AreEqual((int)o, intResultTwo);
  243. }
  244. #endregion
  245. #region floatTest
  246. const float firstFloatVal = -2.0f;
  247. const float secondFloatVal = 3.0f;
  248. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  249. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  250. var floatResult = firstFloatFeed.Sum() - secondFloatFeed.Sum();
  251. var floatResultTwo = -firstFloatFeed.Sum();
  252. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  253. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  254. c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  255. using (var sess = tf.Session())
  256. {
  257. var o = sess.run(c,
  258. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  259. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  260. Assert.AreEqual((float)o, floatResult);
  261. }
  262. // Testing `operator -(Tensor x, Tensor y)
  263. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  264. using (var sess = tf.Session())
  265. {
  266. var o = sess.run(c,
  267. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  268. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  269. Assert.AreEqual((float)o, floatResult);
  270. }
  271. // Testing `operator -(Tensor x, float y)
  272. c = tf.reduce_sum(tf.reduce_sum(a - secondFloatVal, 1));
  273. using (var sess = tf.Session())
  274. {
  275. var o = sess.run(c,
  276. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  277. Assert.AreEqual((float)o, floatResult);
  278. }
  279. // Testing `operator -(float x, Tensor y)
  280. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal - a, 1));
  281. using (var sess = tf.Session())
  282. {
  283. var o = sess.run(c,
  284. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  285. Assert.AreEqual((float)o, Math.Abs(floatResult));
  286. }
  287. // Testing `operator -(Tensor x)
  288. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  289. using (var sess = tf.Session())
  290. {
  291. var o = sess.run(c,
  292. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  293. Assert.AreEqual((float)o, floatResultTwo);
  294. }
  295. #endregion
  296. #region doubleTest
  297. const double firstDoubleVal = -2.0;
  298. const double secondDoubleVal = 3.0;
  299. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  300. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  301. var doubleResult = firstDoubleFeed.Sum() - secondDoubleFeed.Sum();
  302. var doubleResultTwo = -firstDoubleFeed.Sum();
  303. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  304. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  305. c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  306. using (var sess = tf.Session())
  307. {
  308. var o = sess.run(c,
  309. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  310. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  311. Assert.AreEqual((double)o, doubleResult);
  312. }
  313. // Testing `operator -(Tensor x, Tensor y)
  314. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  315. using (var sess = tf.Session())
  316. {
  317. var o = sess.run(c,
  318. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  319. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  320. Assert.AreEqual((double)o, doubleResult);
  321. }
  322. // Testing `operator -(Tensor x, double y)
  323. c = tf.reduce_sum(tf.reduce_sum(a - secondFloatVal, 1));
  324. using (var sess = tf.Session())
  325. {
  326. var o = sess.run(c,
  327. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  328. Assert.AreEqual((double)o, doubleResult);
  329. }
  330. // Testing `operator -(double x, Tensor y)
  331. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal - a, 1));
  332. using (var sess = tf.Session())
  333. {
  334. var o = sess.run(c,
  335. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  336. Assert.AreEqual((double)o, Math.Abs(doubleResult));
  337. }
  338. // Testing `operator -(Tensor x)
  339. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  340. using (var sess = tf.Session())
  341. {
  342. var o = sess.run(c,
  343. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  344. Assert.AreEqual((double)o, doubleResultTwo);
  345. }
  346. #endregion
  347. }
  348. }
  349. }