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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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. private IEnumerable<int> MultiplyArray(IReadOnlyCollection<int> first, IReadOnlyCollection<int> second)
  405. {
  406. if(first.Count != second.Count)
  407. throw new ArgumentException("Arrays should be of equal size!");
  408. var firstEnumerator = first.GetEnumerator();
  409. var secondEnumerator = second.GetEnumerator();
  410. var result = new List<int>();
  411. while (firstEnumerator.MoveNext())
  412. {
  413. secondEnumerator.MoveNext();
  414. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  415. }
  416. firstEnumerator.Dispose();
  417. secondEnumerator.Dispose();
  418. return result;
  419. }
  420. private IEnumerable<float> MultiplyArray(IReadOnlyCollection<float> first, IReadOnlyCollection<float> second)
  421. {
  422. if(first.Count != second.Count)
  423. throw new ArgumentException("Arrays should be of equal size!");
  424. var firstEnumerator = first.GetEnumerator();
  425. var secondEnumerator = second.GetEnumerator();
  426. var result = new List<float>();
  427. while (firstEnumerator.MoveNext())
  428. {
  429. secondEnumerator.MoveNext();
  430. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  431. }
  432. firstEnumerator.Dispose();
  433. secondEnumerator.Dispose();
  434. return result;
  435. }
  436. private IEnumerable<double> MultiplyArray(IReadOnlyCollection<double> first, IReadOnlyCollection<double> second)
  437. {
  438. if(first.Count != second.Count)
  439. throw new ArgumentException("Arrays should be of equal size!");
  440. var firstEnumerator = first.GetEnumerator();
  441. var secondEnumerator = second.GetEnumerator();
  442. var result = new List<double>();
  443. while (firstEnumerator.MoveNext())
  444. {
  445. secondEnumerator.MoveNext();
  446. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  447. }
  448. firstEnumerator.Dispose();
  449. secondEnumerator.Dispose();
  450. return result;
  451. }
  452. [TestMethod]
  453. public void mulOpTests()
  454. {
  455. const int rows = 2; // to avoid broadcasting effect
  456. const int cols = 10;
  457. #region intTest
  458. const int firstIntVal = 2;
  459. const int secondIntVal = 3;
  460. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  461. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  462. var intResult = MultiplyArray(firstIntFeed, secondIntFeed).Sum();
  463. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  464. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  465. var c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  466. using (var sess = tf.Session())
  467. {
  468. var o = sess.run(c,
  469. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  470. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  471. Assert.AreEqual((int)o, intResult);
  472. }
  473. // Testing `operator *(Tensor x, Tensor y)
  474. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  475. using (var sess = tf.Session())
  476. {
  477. var o = sess.run(c,
  478. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  479. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  480. Assert.AreEqual((int)o, intResult);
  481. }
  482. // Testing `operator *(Tensor x, int y)
  483. c = tf.reduce_sum(tf.reduce_sum(a * secondIntVal, 1));
  484. using (var sess = tf.Session())
  485. {
  486. var o = sess.run(c,
  487. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  488. Assert.AreEqual((int)o, intResult);
  489. }
  490. // Testing `operator *(int x, Tensor y)
  491. c = tf.reduce_sum(tf.reduce_sum(firstIntVal * b, 1));
  492. using (var sess = tf.Session())
  493. {
  494. var o = sess.run(c,
  495. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  496. Assert.AreEqual((int)o, intResult);
  497. }
  498. #endregion
  499. #region floatTest
  500. const float firstFloatVal = 2.0f;
  501. const float secondFloatVal = 3.0f;
  502. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  503. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  504. var floatResult = MultiplyArray(firstFloatFeed, secondFloatFeed).Sum();
  505. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  506. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  507. c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  508. using (var sess = tf.Session())
  509. {
  510. var o = sess.run(c,
  511. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  512. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  513. Assert.AreEqual((float)o, floatResult);
  514. }
  515. // Testing `operator *(Tensor x, Tensor y)
  516. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  517. using (var sess = tf.Session())
  518. {
  519. var o = sess.run(c,
  520. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  521. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  522. Assert.AreEqual((float)o, floatResult);
  523. }
  524. // Testing `operator *(Tensor x, float y)
  525. c = tf.reduce_sum(tf.reduce_sum(a * secondFloatVal, 1));
  526. using (var sess = tf.Session())
  527. {
  528. var o = sess.run(c,
  529. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  530. Assert.AreEqual((float)o, floatResult);
  531. }
  532. // Testing `operator *(float x, Tensor y)
  533. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal * b, 1));
  534. using (var sess = tf.Session())
  535. {
  536. var o = sess.run(c,
  537. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  538. Assert.AreEqual((float)o, floatResult);
  539. }
  540. #endregion
  541. #region doubleTest
  542. const double firstDoubleVal = 2.0;
  543. const double secondDoubleVal = 3.0;
  544. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  545. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  546. var doubleResult = MultiplyArray(firstDoubleFeed, secondDoubleFeed).Sum();
  547. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  548. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  549. c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  550. using (var sess = tf.Session())
  551. {
  552. var o = sess.run(c,
  553. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  554. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  555. Assert.AreEqual((double)o, doubleResult);
  556. }
  557. // Testing `operator *(Tensor x, Tensor y)
  558. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  559. using (var sess = tf.Session())
  560. {
  561. var o = sess.run(c,
  562. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  563. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  564. Assert.AreEqual((double)o, doubleResult);
  565. }
  566. // Testing `operator *(Tensor x, double y)
  567. c = tf.reduce_sum(tf.reduce_sum(a * secondFloatVal, 1));
  568. using (var sess = tf.Session())
  569. {
  570. var o = sess.run(c,
  571. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  572. Assert.AreEqual((double)o, doubleResult);
  573. }
  574. // Testing `operator *(double x, Tensor y)
  575. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal * b, 1));
  576. using (var sess = tf.Session())
  577. {
  578. var o = sess.run(c,
  579. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  580. Assert.AreEqual((double) o, doubleResult);
  581. }
  582. #endregion
  583. }
  584. [TestMethod]
  585. public void divOpTests()
  586. {
  587. const int rows = 2; // to avoid broadcasting effect
  588. const int cols = 10;
  589. #region intTest
  590. const int firstIntVal = 6;
  591. const int secondIntVal = 3;
  592. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  593. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  594. var intResult = (int)(firstIntFeed.Sum() / (float)secondIntVal);
  595. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  596. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  597. var c = tf.reduce_sum(tf.reduce_sum(gen_math_ops.floor_div(a, b), 1));
  598. using (var sess = tf.Session())
  599. {
  600. var o = sess.run(c,
  601. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  602. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  603. Assert.AreEqual((int)o, intResult);
  604. }
  605. // Testing `operator /(Tensor x, Tensor y)
  606. c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
  607. using (var sess = tf.Session())
  608. {
  609. var o = sess.run(c,
  610. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  611. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  612. Assert.AreEqual((int)o, intResult);
  613. }
  614. // Testing `operator /(Tensor x, int y)
  615. c = tf.reduce_sum(tf.reduce_sum(a / secondIntVal, 1));
  616. using (var sess = tf.Session())
  617. {
  618. var o = sess.run(c,
  619. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  620. Assert.AreEqual((int)o, intResult);
  621. }
  622. // Testing `operator /(int x, Tensor y)
  623. c = tf.reduce_sum(tf.reduce_sum(firstIntVal / b, 1));
  624. using (var sess = tf.Session())
  625. {
  626. var o = sess.run(c,
  627. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  628. Assert.AreEqual((int)o, intResult);
  629. }
  630. #endregion
  631. #region floatTest
  632. const float firstFloatVal = 6.0f;
  633. const float secondFloatVal = 3.0f;
  634. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  635. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  636. var floatResult = MultiplyArray(firstFloatFeed, secondFloatFeed.Select(x => 1/x).ToArray()).Sum();
  637. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  638. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  639. c = tf.reduce_sum(tf.reduce_sum(tf.divide(a, b), 1));
  640. using (var sess = tf.Session())
  641. {
  642. var o = sess.run(c,
  643. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  644. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  645. Assert.AreEqual((float)o, floatResult);
  646. }
  647. // Testing `operator /(Tensor x, Tensor y)
  648. c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
  649. using (var sess = tf.Session())
  650. {
  651. var o = sess.run(c,
  652. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  653. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  654. Assert.AreEqual((float)o, floatResult);
  655. }
  656. // Testing `operator /(Tensor x, float y)
  657. c = tf.reduce_sum(tf.reduce_sum(a / secondFloatVal, 1));
  658. using (var sess = tf.Session())
  659. {
  660. var o = sess.run(c,
  661. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  662. Assert.AreEqual((float)o, floatResult);
  663. }
  664. // Testing `operator /(float x, Tensor y)
  665. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal / b, 1));
  666. using (var sess = tf.Session())
  667. {
  668. var o = sess.run(c,
  669. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  670. Assert.AreEqual((float)o, floatResult);
  671. }
  672. #endregion
  673. #region doubleTest
  674. const double firstDoubleVal = 6.0;
  675. const double secondDoubleVal = 3.0;
  676. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  677. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  678. var doubleResult = MultiplyArray(firstDoubleFeed, secondDoubleFeed.Select(x => 1/x).ToArray()).Sum();
  679. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  680. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  681. c = tf.reduce_sum(tf.reduce_sum(tf.divide(a, b), 1));
  682. using (var sess = tf.Session())
  683. {
  684. var o = sess.run(c,
  685. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  686. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  687. Assert.AreEqual((double)o, doubleResult);
  688. }
  689. // Testing `operator /(Tensor x, Tensor y)
  690. c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
  691. using (var sess = tf.Session())
  692. {
  693. var o = sess.run(c,
  694. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  695. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  696. Assert.AreEqual((double)o, doubleResult);
  697. }
  698. // Testing `operator /(Tensor x, double y)
  699. c = tf.reduce_sum(tf.reduce_sum(a / secondFloatVal, 1));
  700. using (var sess = tf.Session())
  701. {
  702. var o = sess.run(c,
  703. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  704. Assert.AreEqual((double)o, doubleResult);
  705. }
  706. // Testing `operator /(double x, Tensor y)
  707. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal / b, 1));
  708. using (var sess = tf.Session())
  709. {
  710. var o = sess.run(c,
  711. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  712. Assert.AreEqual((double)o, doubleResult);
  713. }
  714. #endregion
  715. }
  716. [TestMethod]
  717. public void greaterThanOpTests()
  718. {
  719. const int rows = 2; // to avoid broadcasting effect
  720. const int cols = 10;
  721. #region intTest
  722. const int intThreshold = 10;
  723. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  724. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  725. var intResult = firstIntFeed.Count(elem => elem > intThreshold);
  726. var intResultTwo = firstIntFeed.Count(elem => elem < intThreshold);
  727. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  728. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  729. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
  730. using (var sess = tf.Session())
  731. {
  732. var o = sess.run(c,
  733. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  734. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  735. Assert.AreEqual((int)o, intResult);
  736. }
  737. // Testing `operator >(Tensor x, Tensor y)
  738. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
  739. using (var sess = tf.Session())
  740. {
  741. var o = sess.run(c,
  742. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  743. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  744. Assert.AreEqual((int)o, intResult);
  745. }
  746. // Testing `operator >(Tensor x, int y)
  747. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > intThreshold, tf.int32), 1));
  748. using (var sess = tf.Session())
  749. {
  750. var o = sess.run(c,
  751. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  752. Assert.AreEqual((int)o, intResult);
  753. }
  754. // Testing `operator >(int x, Tensor y)
  755. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold > a, tf.int32), 1));
  756. using (var sess = tf.Session())
  757. {
  758. var o = sess.run(c,
  759. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  760. Assert.AreEqual((int)o, intResultTwo);
  761. }
  762. #endregion
  763. #region floatTest
  764. const float floatThreshold = 10.0f;
  765. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  766. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  767. var floatResult = firstFloatFeed.Count(elem => elem > floatThreshold);
  768. var floatResultTwo = firstFloatFeed.Count(elem => elem < floatThreshold);
  769. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  770. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  771. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
  772. using (var sess = tf.Session())
  773. {
  774. var o = sess.run(c,
  775. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  776. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  777. Assert.AreEqual((int)o, floatResult);
  778. }
  779. // Testing `operator >(Tensor x, Tensor y)
  780. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
  781. using (var sess = tf.Session())
  782. {
  783. var o = sess.run(c,
  784. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  785. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  786. Assert.AreEqual((int)o, floatResult);
  787. }
  788. // Testing `operator >(Tensor x, float y)
  789. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > floatThreshold, tf.int32), 1));
  790. using (var sess = tf.Session())
  791. {
  792. var o = sess.run(c,
  793. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  794. Assert.AreEqual((int)o, floatResult);
  795. }
  796. // Testing `operator >(float x, Tensor y)
  797. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold > a, tf.int32), 1));
  798. using (var sess = tf.Session())
  799. {
  800. var o = sess.run(c,
  801. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  802. Assert.AreEqual((int)o, floatResultTwo);
  803. }
  804. #endregion
  805. #region doubleTest
  806. const double doubleThreshold = 10.0;
  807. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  808. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  809. var doubleResult = firstDoubleFeed.Count(elem => elem > doubleThreshold);
  810. var doubleResultTwo = firstDoubleFeed.Count(elem => elem < doubleThreshold);
  811. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  812. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  813. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
  814. using (var sess = tf.Session())
  815. {
  816. var o = sess.run(c,
  817. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  818. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  819. Assert.AreEqual((int)o, doubleResult);
  820. }
  821. // Testing `operator >(Tensor x, Tensor y)
  822. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
  823. using (var sess = tf.Session())
  824. {
  825. var o = sess.run(c,
  826. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  827. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  828. Assert.AreEqual((int)o, doubleResult);
  829. }
  830. // Testing `operator >(Tensor x, double y)
  831. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > doubleThreshold, tf.int32), 1));
  832. using (var sess = tf.Session())
  833. {
  834. var o = sess.run(c,
  835. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  836. Assert.AreEqual((int)o, doubleResult);
  837. }
  838. // Testing `operator >(double x, Tensor y)
  839. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold > a, tf.int32), 1));
  840. using (var sess = tf.Session())
  841. {
  842. var o = sess.run(c,
  843. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  844. Assert.AreEqual((int)o, doubleResultTwo);
  845. }
  846. #endregion
  847. }
  848. [TestMethod]
  849. public void lessThanOpTests()
  850. {
  851. const int rows = 2; // to avoid broadcasting effect
  852. const int cols = 10;
  853. #region intTest
  854. const int intThreshold = 10;
  855. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  856. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  857. var intResult = firstIntFeed.Count(elem => elem < intThreshold);
  858. var intResultTwo = firstIntFeed.Count(elem => elem > intThreshold);
  859. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  860. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  861. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
  862. using (var sess = tf.Session())
  863. {
  864. var o = sess.run(c,
  865. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  866. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  867. Assert.AreEqual((int)o, intResult);
  868. }
  869. // Testing `operator <(Tensor x, Tensor y)
  870. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
  871. using (var sess = tf.Session())
  872. {
  873. var o = sess.run(c,
  874. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  875. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  876. Assert.AreEqual((int)o, intResult);
  877. }
  878. // Testing `operator <(Tensor x, int y)
  879. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < intThreshold, tf.int32), 1));
  880. using (var sess = tf.Session())
  881. {
  882. var o = sess.run(c,
  883. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  884. Assert.AreEqual((int)o, intResult);
  885. }
  886. // Testing `operator <(int x, Tensor y)
  887. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold < a, tf.int32), 1));
  888. using (var sess = tf.Session())
  889. {
  890. var o = sess.run(c,
  891. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  892. Assert.AreEqual((int)o, intResultTwo);
  893. }
  894. #endregion
  895. #region floatTest
  896. const float floatThreshold = 10.0f;
  897. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  898. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  899. var floatResult = firstFloatFeed.Count(elem => elem < floatThreshold);
  900. var floatResultTwo = firstFloatFeed.Count(elem => elem > floatThreshold);
  901. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  902. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  903. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
  904. using (var sess = tf.Session())
  905. {
  906. var o = sess.run(c,
  907. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  908. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  909. Assert.AreEqual((int)o, floatResult);
  910. }
  911. // Testing `operator <(Tensor x, Tensor y)
  912. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
  913. using (var sess = tf.Session())
  914. {
  915. var o = sess.run(c,
  916. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  917. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  918. Assert.AreEqual((int)o, floatResult);
  919. }
  920. // Testing `operator <(Tensor x, float y)
  921. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < floatThreshold, tf.int32), 1));
  922. using (var sess = tf.Session())
  923. {
  924. var o = sess.run(c,
  925. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  926. Assert.AreEqual((int)o, floatResult);
  927. }
  928. // Testing `operator <(float x, Tensor y)
  929. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold < a, tf.int32), 1));
  930. using (var sess = tf.Session())
  931. {
  932. var o = sess.run(c,
  933. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  934. Assert.AreEqual((int)o, floatResultTwo);
  935. }
  936. #endregion
  937. #region doubleTest
  938. const double doubleThreshold = 10.0;
  939. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  940. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  941. var doubleResult = firstDoubleFeed.Count(elem => elem < doubleThreshold);
  942. var doubleResultTwo = firstDoubleFeed.Count(elem => elem > doubleThreshold);
  943. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  944. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  945. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
  946. using (var sess = tf.Session())
  947. {
  948. var o = sess.run(c,
  949. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  950. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  951. Assert.AreEqual((int)o, doubleResult);
  952. }
  953. // Testing `operator <(Tensor x, Tensor y)
  954. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
  955. using (var sess = tf.Session())
  956. {
  957. var o = sess.run(c,
  958. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  959. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  960. Assert.AreEqual((int)o, doubleResult);
  961. }
  962. // Testing `operator <(Tensor x, double y)
  963. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < doubleThreshold, tf.int32), 1));
  964. using (var sess = tf.Session())
  965. {
  966. var o = sess.run(c,
  967. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  968. Assert.AreEqual((int)o, doubleResult);
  969. }
  970. // Testing `operator <(double x, Tensor y)
  971. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold < a, tf.int32), 1));
  972. using (var sess = tf.Session())
  973. {
  974. var o = sess.run(c,
  975. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  976. Assert.AreEqual((int)o, doubleResultTwo);
  977. }
  978. #endregion
  979. }
  980. [TestMethod]
  981. public void greaterOrEqualThanOpTests()
  982. {
  983. const int rows = 2; // to avoid broadcasting effect
  984. const int cols = 10;
  985. #region intTest
  986. const int intThreshold = 10;
  987. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  988. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  989. var intResult = firstIntFeed.Count(elem => elem >= intThreshold);
  990. var intResultTwo = firstIntFeed.Count(elem => elem <= intThreshold);
  991. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  992. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  993. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
  994. using (var sess = tf.Session())
  995. {
  996. var o = sess.run(c,
  997. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  998. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  999. Assert.AreEqual((int)o, intResult);
  1000. }
  1001. // Testing `operator >=(Tensor x, Tensor y)
  1002. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= b, tf.int32), 1));
  1003. using (var sess = tf.Session())
  1004. {
  1005. var o = sess.run(c,
  1006. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  1007. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  1008. Assert.AreEqual((int)o, intResult);
  1009. }
  1010. // Testing `operator >=(Tensor x, int y)
  1011. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= intThreshold, tf.int32), 1));
  1012. using (var sess = tf.Session())
  1013. {
  1014. var o = sess.run(c,
  1015. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  1016. Assert.AreEqual((int)o, intResult);
  1017. }
  1018. // Testing `operator >=(int x, Tensor y)
  1019. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold >= a, tf.int32), 1));
  1020. using (var sess = tf.Session())
  1021. {
  1022. var o = sess.run(c,
  1023. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  1024. Assert.AreEqual((int)o, intResultTwo);
  1025. }
  1026. #endregion
  1027. #region floatTest
  1028. const float floatThreshold = 10.0f;
  1029. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  1030. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  1031. var floatResult = firstFloatFeed.Count(elem => elem >= floatThreshold);
  1032. var floatResultTwo = firstFloatFeed.Count(elem => elem <= floatThreshold);
  1033. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  1034. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  1035. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
  1036. using (var sess = tf.Session())
  1037. {
  1038. var o = sess.run(c,
  1039. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  1040. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  1041. Assert.AreEqual((int)o, floatResult);
  1042. }
  1043. // Testing `operator >=(Tensor x, Tensor y)
  1044. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= b, tf.int32), 1));
  1045. using (var sess = tf.Session())
  1046. {
  1047. var o = sess.run(c,
  1048. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  1049. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  1050. Assert.AreEqual((int)o, floatResult);
  1051. }
  1052. // Testing `operator >=(Tensor x, float y)
  1053. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= floatThreshold, tf.int32), 1));
  1054. using (var sess = tf.Session())
  1055. {
  1056. var o = sess.run(c,
  1057. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  1058. Assert.AreEqual((int)o, floatResult);
  1059. }
  1060. // Testing `operator >=(float x, Tensor y)
  1061. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold >= a, tf.int32), 1));
  1062. using (var sess = tf.Session())
  1063. {
  1064. var o = sess.run(c,
  1065. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  1066. Assert.AreEqual((int)o, floatResultTwo);
  1067. }
  1068. #endregion
  1069. #region doubleTest
  1070. const double doubleThreshold = 10.0;
  1071. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  1072. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  1073. var doubleResult = firstDoubleFeed.Count(elem => elem >= doubleThreshold);
  1074. var doubleResultTwo = firstDoubleFeed.Count(elem => elem <= doubleThreshold);
  1075. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  1076. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  1077. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
  1078. using (var sess = tf.Session())
  1079. {
  1080. var o = sess.run(c,
  1081. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  1082. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  1083. Assert.AreEqual((int)o, doubleResult);
  1084. }
  1085. // Testing `operator >=(Tensor x, Tensor y)
  1086. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= b, tf.int32), 1));
  1087. using (var sess = tf.Session())
  1088. {
  1089. var o = sess.run(c,
  1090. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  1091. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  1092. Assert.AreEqual((int)o, doubleResult);
  1093. }
  1094. // Testing `operator >=(Tensor x, double y)
  1095. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= doubleThreshold, tf.int32), 1));
  1096. using (var sess = tf.Session())
  1097. {
  1098. var o = sess.run(c,
  1099. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  1100. Assert.AreEqual((int)o, doubleResult);
  1101. }
  1102. // Testing `operator >=(double x, Tensor y)
  1103. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold >= a, tf.int32), 1));
  1104. using (var sess = tf.Session())
  1105. {
  1106. var o = sess.run(c,
  1107. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  1108. Assert.AreEqual((int)o, doubleResultTwo);
  1109. }
  1110. #endregion
  1111. }
  1112. [TestMethod]
  1113. public void lessOrEqualThanOpTests()
  1114. {
  1115. const int rows = 2; // to avoid broadcasting effect
  1116. const int cols = 10;
  1117. #region intTest
  1118. const int intThreshold = 10;
  1119. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  1120. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  1121. var intResult = firstIntFeed.Count(elem => elem <= intThreshold);
  1122. var intResultTwo = firstIntFeed.Count(elem => elem >= intThreshold);
  1123. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  1124. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  1125. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
  1126. using (var sess = tf.Session())
  1127. {
  1128. var o = sess.run(c,
  1129. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  1130. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  1131. Assert.AreEqual((int)o, intResult);
  1132. }
  1133. // Testing `operator <=(Tensor x, Tensor y)
  1134. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= b, tf.int32), 1));
  1135. using (var sess = tf.Session())
  1136. {
  1137. var o = sess.run(c,
  1138. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  1139. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  1140. Assert.AreEqual((int)o, intResult);
  1141. }
  1142. // Testing `operator <=(Tensor x, int y)
  1143. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= intThreshold, tf.int32), 1));
  1144. using (var sess = tf.Session())
  1145. {
  1146. var o = sess.run(c,
  1147. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  1148. Assert.AreEqual((int)o, intResult);
  1149. }
  1150. // Testing `operator <=(int x, Tensor y)
  1151. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold <= a, tf.int32), 1));
  1152. using (var sess = tf.Session())
  1153. {
  1154. var o = sess.run(c,
  1155. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  1156. Assert.AreEqual((int)o, intResultTwo);
  1157. }
  1158. #endregion
  1159. #region floatTest
  1160. const float floatThreshold = 10.0f;
  1161. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  1162. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  1163. var floatResult = firstFloatFeed.Count(elem => elem <= floatThreshold);
  1164. var floatResultTwo = firstFloatFeed.Count(elem => elem >= floatThreshold);
  1165. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  1166. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  1167. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
  1168. using (var sess = tf.Session())
  1169. {
  1170. var o = sess.run(c,
  1171. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  1172. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  1173. Assert.AreEqual((int)o, floatResult);
  1174. }
  1175. // Testing `operator <=(Tensor x, Tensor y)
  1176. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= b, tf.int32), 1));
  1177. using (var sess = tf.Session())
  1178. {
  1179. var o = sess.run(c,
  1180. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  1181. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  1182. Assert.AreEqual((int)o, floatResult);
  1183. }
  1184. // Testing `operator <=(Tensor x, float y)
  1185. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= floatThreshold, tf.int32), 1));
  1186. using (var sess = tf.Session())
  1187. {
  1188. var o = sess.run(c,
  1189. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  1190. Assert.AreEqual((int)o, floatResult);
  1191. }
  1192. // Testing `operator <=(float x, Tensor y)
  1193. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold <= a, tf.int32), 1));
  1194. using (var sess = tf.Session())
  1195. {
  1196. var o = sess.run(c,
  1197. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  1198. Assert.AreEqual((int)o, floatResultTwo);
  1199. }
  1200. #endregion
  1201. #region doubleTest
  1202. const double doubleThreshold = 10.0;
  1203. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  1204. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  1205. var doubleResult = firstDoubleFeed.Count(elem => elem <= doubleThreshold);
  1206. var doubleResultTwo = firstDoubleFeed.Count(elem => elem >= doubleThreshold);
  1207. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  1208. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  1209. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
  1210. using (var sess = tf.Session())
  1211. {
  1212. var o = sess.run(c,
  1213. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  1214. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  1215. Assert.AreEqual((int)o, doubleResult);
  1216. }
  1217. // Testing `operator <=(Tensor x, Tensor y)
  1218. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= b, tf.int32), 1));
  1219. using (var sess = tf.Session())
  1220. {
  1221. var o = sess.run(c,
  1222. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  1223. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  1224. Assert.AreEqual((int)o, doubleResult);
  1225. }
  1226. // Testing `operator <=(Tensor x, double y)
  1227. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= doubleThreshold, tf.int32), 1));
  1228. using (var sess = tf.Session())
  1229. {
  1230. var o = sess.run(c,
  1231. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  1232. Assert.AreEqual((int)o, doubleResult);
  1233. }
  1234. // Testing `operator <=(double x, Tensor y)
  1235. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold <= a, tf.int32), 1));
  1236. using (var sess = tf.Session())
  1237. {
  1238. var o = sess.run(c,
  1239. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  1240. Assert.AreEqual((int)o, doubleResultTwo);
  1241. }
  1242. #endregion
  1243. }
  1244. }
  1245. }