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

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