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

6 years ago
6 years ago
6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  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 Tensorflow.Util;
  8. using Buffer = Tensorflow.Buffer;
  9. using static Tensorflow.Binding;
  10. namespace TensorFlowNET.UnitTest
  11. {
  12. [TestClass]
  13. public class OperationsTest
  14. {
  15. /// <summary>
  16. /// Port from tensorflow\c\c_api_test.cc
  17. /// `TEST(CAPI, GetAllOpList)`
  18. /// </summary>
  19. [TestMethod]
  20. public void GetAllOpList()
  21. {
  22. var handle = c_api.TF_GetAllOpList();
  23. var buffer = new Buffer(handle);
  24. var op_list = OpList.Parser.ParseFrom(buffer.MemoryBlock.Stream());
  25. var _registered_ops = new Dictionary<string, OpDef>();
  26. foreach (var op_def in op_list.Op)
  27. _registered_ops[op_def.Name] = op_def;
  28. // r1.14 added NN op
  29. var op = _registered_ops.FirstOrDefault(x => x.Key == "NearestNeighbors");
  30. Assert.IsTrue(op_list.Op.Count > 1000);
  31. }
  32. [TestMethod]
  33. public void addInPlaceholder()
  34. {
  35. var a = tf.placeholder(tf.float32);
  36. var b = tf.placeholder(tf.float32);
  37. var c = tf.add(a, b);
  38. using(var sess = tf.Session())
  39. {
  40. var o = sess.run(c,
  41. new FeedItem(a, 3.0f),
  42. new FeedItem(b, 2.0f));
  43. Assert.AreEqual((float)o, 5.0f);
  44. }
  45. }
  46. [TestMethod]
  47. public void addInConstant()
  48. {
  49. var a = tf.constant(4.0f);
  50. var b = tf.constant(5.0f);
  51. var c = tf.add(a, b);
  52. using (var sess = tf.Session())
  53. {
  54. var o = sess.run(c);
  55. Assert.AreEqual((float)o, 9.0f);
  56. }
  57. }
  58. [TestMethod]
  59. public void isFinite()
  60. {
  61. var a = tf.constant(new[] { 1, np.nan, 2, np.nan, 3, np.nan, 4, np.nan });
  62. var b = tf.cast(tf.is_finite(a), tf.float32);
  63. var check = np.array(1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
  64. using (var sess = tf.Session())
  65. {
  66. var o = sess.run(b);
  67. Assert.IsTrue(o.array_equal(check));
  68. }
  69. }
  70. [TestMethod]
  71. public void isNan()
  72. {
  73. var a = tf.constant(new[] { 1, np.nan, 2, np.nan, 3, np.nan, 4, np.nan });
  74. var b = tf.cast(tf.is_nan(a), tf.float32);
  75. var check = np.array(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
  76. using (var sess = tf.Session())
  77. {
  78. var o = sess.run(b);
  79. Assert.IsTrue(o.array_equal(check));
  80. }
  81. }
  82. [TestMethod]
  83. public void cumSumTest()
  84. {
  85. var a = tf.constant(new[] { 1, 1, 2, 3, 4, 5 });
  86. var b = tf.cumsum(a);
  87. var check = np.array(1, 2, 4, 7, 11, 16);
  88. using (var sess = tf.Session())
  89. {
  90. var o = sess.run(b);
  91. Assert.IsTrue(o.array_equal(check));
  92. }
  93. b = tf.cumsum(a, exclusive: true);
  94. check = np.array(0, 1, 2, 4, 7, 11);
  95. using (var sess = tf.Session())
  96. {
  97. var o = sess.run(b);
  98. Assert.IsTrue(o.array_equal(check));
  99. }
  100. b = tf.cumsum(a, reverse: true);
  101. check = np.array(16, 15, 14, 12, 9, 5);
  102. using (var sess = tf.Session())
  103. {
  104. var o = sess.run(b);
  105. Assert.IsTrue(o.array_equal(check));
  106. }
  107. b = tf.cumsum(a, exclusive:true, reverse: true);
  108. check = np.array(15, 14, 12, 9, 5, 0);
  109. using (var sess = tf.Session())
  110. {
  111. var o = sess.run(b);
  112. Assert.IsTrue(o.array_equal(check));
  113. }
  114. }
  115. [TestMethod]
  116. public void logicalOpsTest()
  117. {
  118. var a = tf.constant(new[] {1f, 2f, 3f, 4f, -4f, -3f, -2f, -1f});
  119. var b = tf.less(a, 0f);
  120. var c = tf.greater(a, 0f);
  121. var d = tf.cast(tf.logical_and(b, c), tf.int32);
  122. var check = np.array(new[] { 0, 0, 0, 0, 0, 0, 0, 0 });
  123. using (var sess = tf.Session())
  124. {
  125. var o = sess.run(d);
  126. Assert.IsTrue(o.array_equal(check));
  127. }
  128. d = tf.cast(tf.logical_not(b), tf.int32);
  129. check = np.array(new[] { 1, 1, 1, 1, 0, 0, 0, 0 });
  130. using (var sess = tf.Session())
  131. {
  132. var o = sess.run(d);
  133. Assert.IsTrue(o.array_equal(check));
  134. }
  135. d = tf.cast(tf.logical_or(b, c), tf.int32);
  136. check = np.array(new[] { 1, 1, 1, 1, 1, 1, 1, 1 });
  137. using (var sess = tf.Session())
  138. {
  139. var o = sess.run(d);
  140. Assert.IsTrue(o.array_equal(check));
  141. }
  142. d = tf.cast(tf.logical_xor(b, c), tf.int32);
  143. check = np.array(new[] { 1, 1, 1, 1, 1, 1, 1, 1 });
  144. using (var sess = tf.Session())
  145. {
  146. var o = sess.run(d);
  147. Assert.IsTrue(o.array_equal(check));
  148. }
  149. }
  150. [TestMethod]
  151. public void addOpTests()
  152. {
  153. const int rows = 2; // to avoid broadcasting effect
  154. const int cols = 10;
  155. #region intTest
  156. const int firstIntVal = 2;
  157. const int secondIntVal = 3;
  158. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  159. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  160. var intResult = firstIntFeed.Sum() + secondIntFeed.Sum();
  161. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  162. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  163. var c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  164. using (var sess = tf.Session())
  165. {
  166. var o = sess.run(c,
  167. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  168. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  169. Assert.AreEqual((int)o, intResult);
  170. }
  171. // Testing `operator +(Tensor x, Tensor y)`
  172. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  173. using (var sess = tf.Session())
  174. {
  175. var o = sess.run(c,
  176. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  177. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  178. Assert.AreEqual((int)o, intResult);
  179. }
  180. // Testing `operator +(Tensor x, int y)`
  181. c = tf.reduce_sum(tf.reduce_sum(a + secondIntVal, 1));
  182. using (var sess = tf.Session())
  183. {
  184. var o = sess.run(c,
  185. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  186. Assert.AreEqual((int)o, intResult);
  187. }
  188. // Testing `operator +(int x, Tensor y)`
  189. c = tf.reduce_sum(tf.reduce_sum(secondIntVal + a, 1));
  190. using (var sess = tf.Session())
  191. {
  192. var o = sess.run(c,
  193. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  194. Assert.AreEqual((int)o, intResult);
  195. }
  196. #endregion
  197. #region floatTest
  198. const float firstFloatVal = 2.0f;
  199. const float secondFloatVal = 3.0f;
  200. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  201. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  202. var floatResult = firstFloatFeed.Sum() + secondFloatFeed.Sum();
  203. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  204. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  205. c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  206. using (var sess = tf.Session())
  207. {
  208. var o = sess.run(c,
  209. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  210. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  211. Assert.AreEqual((float)o, floatResult);
  212. }
  213. // Testing `operator +(Tensor x, Tensor y)
  214. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  215. using (var sess = tf.Session())
  216. {
  217. var o = sess.run(c,
  218. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  219. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  220. Assert.AreEqual((float)o, floatResult);
  221. }
  222. // Testing `operator +(Tensor x, float y)
  223. c = tf.reduce_sum(tf.reduce_sum(a + secondFloatVal, 1));
  224. using (var sess = tf.Session())
  225. {
  226. var o = sess.run(c,
  227. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  228. Assert.AreEqual((float)o, floatResult);
  229. }
  230. // Testing `operator +(float x, Tensor y)
  231. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal + a, 1));
  232. using (var sess = tf.Session())
  233. {
  234. var o = sess.run(c,
  235. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  236. Assert.AreEqual((float)o, floatResult);
  237. }
  238. #endregion
  239. #region doubleTest
  240. const double firstDoubleVal = 2.0;
  241. const double secondDoubleVal = 3.0;
  242. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  243. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  244. var doubleResult = firstDoubleFeed.Sum() + secondDoubleFeed.Sum();
  245. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  246. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  247. c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  248. using (var sess = tf.Session())
  249. {
  250. var o = sess.run(c,
  251. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  252. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  253. Assert.AreEqual((double)o, doubleResult);
  254. }
  255. // Testing `operator +(Tensor x, Tensor y)
  256. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  257. using (var sess = tf.Session())
  258. {
  259. var o = sess.run(c,
  260. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  261. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  262. Assert.AreEqual((double)o, doubleResult);
  263. }
  264. // Testing `operator +(Tensor x, double y)
  265. c = tf.reduce_sum(tf.reduce_sum(a + secondFloatVal, 1));
  266. using (var sess = tf.Session())
  267. {
  268. var o = sess.run(c,
  269. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  270. Assert.AreEqual((double)o, doubleResult);
  271. }
  272. // Testing `operator +(double x, Tensor y)
  273. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal + a, 1));
  274. using (var sess = tf.Session())
  275. {
  276. var o = sess.run(c,
  277. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  278. Assert.AreEqual((double)o, doubleResult);
  279. }
  280. #endregion
  281. }
  282. [TestMethod]
  283. public void subOpTests()
  284. {
  285. const int rows = 2; // to avoid broadcasting effect
  286. const int cols = 10;
  287. #region intTest
  288. const int firstIntVal = -2;
  289. const int secondIntVal = 3;
  290. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  291. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  292. var intResult = firstIntFeed.Sum() - secondIntFeed.Sum();
  293. var intResultTwo = -firstIntFeed.Sum();
  294. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  295. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  296. var c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  297. using (var sess = tf.Session())
  298. {
  299. var o = sess.run(c,
  300. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  301. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  302. Assert.AreEqual((int)o, intResult);
  303. }
  304. // Testing `operator -(Tensor x, Tensor y)
  305. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  306. using (var sess = tf.Session())
  307. {
  308. var o = sess.run(c,
  309. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  310. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  311. Assert.AreEqual((int)o, intResult);
  312. }
  313. // Testing `operator -(Tensor x, int y)
  314. c = tf.reduce_sum(tf.reduce_sum(a - secondIntVal, 1));
  315. using (var sess = tf.Session())
  316. {
  317. var o = sess.run(c,
  318. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  319. Assert.AreEqual((int)o, intResult);
  320. }
  321. // Testing `operator -(int x, Tensor y)
  322. c = tf.reduce_sum(tf.reduce_sum(secondIntVal - a, 1));
  323. using (var sess = tf.Session())
  324. {
  325. var o = sess.run(c,
  326. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  327. Assert.AreEqual((int)o, Math.Abs(intResult));
  328. }
  329. // Testing `operator -(Tensor x)
  330. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  331. using (var sess = tf.Session())
  332. {
  333. var o = sess.run(c,
  334. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  335. Assert.AreEqual((int)o, intResultTwo);
  336. }
  337. #endregion
  338. #region floatTest
  339. const float firstFloatVal = -2.0f;
  340. const float secondFloatVal = 3.0f;
  341. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  342. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  343. var floatResult = firstFloatFeed.Sum() - secondFloatFeed.Sum();
  344. var floatResultTwo = -firstFloatFeed.Sum();
  345. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  346. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  347. c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  348. using (var sess = tf.Session())
  349. {
  350. var o = sess.run(c,
  351. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  352. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  353. Assert.AreEqual((float)o, floatResult);
  354. }
  355. // Testing `operator -(Tensor x, Tensor y)
  356. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  357. using (var sess = tf.Session())
  358. {
  359. var o = sess.run(c,
  360. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  361. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  362. Assert.AreEqual((float)o, floatResult);
  363. }
  364. // Testing `operator -(Tensor x, float y)
  365. c = tf.reduce_sum(tf.reduce_sum(a - secondFloatVal, 1));
  366. using (var sess = tf.Session())
  367. {
  368. var o = sess.run(c,
  369. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  370. Assert.AreEqual((float)o, floatResult);
  371. }
  372. // Testing `operator -(float x, Tensor y)
  373. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal - a, 1));
  374. using (var sess = tf.Session())
  375. {
  376. var o = sess.run(c,
  377. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  378. Assert.AreEqual((float)o, Math.Abs(floatResult));
  379. }
  380. // Testing `operator -(Tensor x)
  381. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  382. using (var sess = tf.Session())
  383. {
  384. var o = sess.run(c,
  385. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  386. Assert.AreEqual((float)o, floatResultTwo);
  387. }
  388. #endregion
  389. #region doubleTest
  390. const double firstDoubleVal = -2.0;
  391. const double secondDoubleVal = 3.0;
  392. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  393. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  394. var doubleResult = firstDoubleFeed.Sum() - secondDoubleFeed.Sum();
  395. var doubleResultTwo = -firstDoubleFeed.Sum();
  396. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  397. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  398. c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  399. using (var sess = tf.Session())
  400. {
  401. var o = sess.run(c,
  402. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  403. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  404. Assert.AreEqual((double)o, doubleResult);
  405. }
  406. // Testing `operator -(Tensor x, Tensor y)
  407. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  408. using (var sess = tf.Session())
  409. {
  410. var o = sess.run(c,
  411. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  412. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  413. Assert.AreEqual((double)o, doubleResult);
  414. }
  415. // Testing `operator -(Tensor x, double y)
  416. c = tf.reduce_sum(tf.reduce_sum(a - secondFloatVal, 1));
  417. using (var sess = tf.Session())
  418. {
  419. var o = sess.run(c,
  420. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  421. Assert.AreEqual((double)o, doubleResult);
  422. }
  423. // Testing `operator -(double x, Tensor y)
  424. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal - a, 1));
  425. using (var sess = tf.Session())
  426. {
  427. var o = sess.run(c,
  428. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  429. Assert.AreEqual((double)o, Math.Abs(doubleResult));
  430. }
  431. // Testing `operator -(Tensor x)
  432. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  433. using (var sess = tf.Session())
  434. {
  435. var o = sess.run(c,
  436. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  437. Assert.AreEqual((double)o, doubleResultTwo);
  438. }
  439. #endregion
  440. }
  441. private IEnumerable<int> MultiplyArray(IReadOnlyCollection<int> first, IReadOnlyCollection<int> second)
  442. {
  443. if(first.Count != second.Count)
  444. throw new ArgumentException("Arrays should be of equal size!");
  445. var firstEnumerator = first.GetEnumerator();
  446. var secondEnumerator = second.GetEnumerator();
  447. var result = new List<int>();
  448. while (firstEnumerator.MoveNext())
  449. {
  450. secondEnumerator.MoveNext();
  451. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  452. }
  453. firstEnumerator.Dispose();
  454. secondEnumerator.Dispose();
  455. return result;
  456. }
  457. private IEnumerable<float> MultiplyArray(IReadOnlyCollection<float> first, IReadOnlyCollection<float> second)
  458. {
  459. if(first.Count != second.Count)
  460. throw new ArgumentException("Arrays should be of equal size!");
  461. var firstEnumerator = first.GetEnumerator();
  462. var secondEnumerator = second.GetEnumerator();
  463. var result = new List<float>();
  464. while (firstEnumerator.MoveNext())
  465. {
  466. secondEnumerator.MoveNext();
  467. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  468. }
  469. firstEnumerator.Dispose();
  470. secondEnumerator.Dispose();
  471. return result;
  472. }
  473. private IEnumerable<double> MultiplyArray(IReadOnlyCollection<double> first, IReadOnlyCollection<double> second)
  474. {
  475. if(first.Count != second.Count)
  476. throw new ArgumentException("Arrays should be of equal size!");
  477. var firstEnumerator = first.GetEnumerator();
  478. var secondEnumerator = second.GetEnumerator();
  479. var result = new List<double>();
  480. while (firstEnumerator.MoveNext())
  481. {
  482. secondEnumerator.MoveNext();
  483. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  484. }
  485. firstEnumerator.Dispose();
  486. secondEnumerator.Dispose();
  487. return result;
  488. }
  489. [TestMethod]
  490. public void mulOpTests()
  491. {
  492. const int rows = 2; // to avoid broadcasting effect
  493. const int cols = 10;
  494. #region intTest
  495. const int firstIntVal = 2;
  496. const int secondIntVal = 3;
  497. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  498. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  499. var intResult = MultiplyArray(firstIntFeed, secondIntFeed).Sum();
  500. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  501. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  502. var c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  503. using (var sess = tf.Session())
  504. {
  505. var o = sess.run(c,
  506. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  507. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  508. Assert.AreEqual((int)o, intResult);
  509. }
  510. // Testing `operator *(Tensor x, Tensor y)
  511. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  512. using (var sess = tf.Session())
  513. {
  514. var o = sess.run(c,
  515. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  516. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  517. Assert.AreEqual((int)o, intResult);
  518. }
  519. // Testing `operator *(Tensor x, int y)
  520. c = tf.reduce_sum(tf.reduce_sum(a * secondIntVal, 1));
  521. using (var sess = tf.Session())
  522. {
  523. var o = sess.run(c,
  524. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  525. Assert.AreEqual((int)o, intResult);
  526. }
  527. // Testing `operator *(int x, Tensor y)
  528. c = tf.reduce_sum(tf.reduce_sum(firstIntVal * b, 1));
  529. using (var sess = tf.Session())
  530. {
  531. var o = sess.run(c,
  532. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  533. Assert.AreEqual((int)o, intResult);
  534. }
  535. #endregion
  536. #region floatTest
  537. const float firstFloatVal = 2.0f;
  538. const float secondFloatVal = 3.0f;
  539. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  540. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  541. var floatResult = MultiplyArray(firstFloatFeed, secondFloatFeed).Sum();
  542. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  543. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  544. c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  545. using (var sess = tf.Session())
  546. {
  547. var o = sess.run(c,
  548. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  549. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  550. Assert.AreEqual((float)o, floatResult);
  551. }
  552. // Testing `operator *(Tensor x, Tensor y)
  553. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  554. using (var sess = tf.Session())
  555. {
  556. var o = sess.run(c,
  557. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  558. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  559. Assert.AreEqual((float)o, floatResult);
  560. }
  561. // Testing `operator *(Tensor x, float y)
  562. c = tf.reduce_sum(tf.reduce_sum(a * secondFloatVal, 1));
  563. using (var sess = tf.Session())
  564. {
  565. var o = sess.run(c,
  566. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  567. Assert.AreEqual((float)o, floatResult);
  568. }
  569. // Testing `operator *(float x, Tensor y)
  570. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal * b, 1));
  571. using (var sess = tf.Session())
  572. {
  573. var o = sess.run(c,
  574. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  575. Assert.AreEqual((float)o, floatResult);
  576. }
  577. #endregion
  578. #region doubleTest
  579. const double firstDoubleVal = 2.0;
  580. const double secondDoubleVal = 3.0;
  581. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  582. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  583. var doubleResult = MultiplyArray(firstDoubleFeed, secondDoubleFeed).Sum();
  584. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  585. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  586. c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  587. using (var sess = tf.Session())
  588. {
  589. var o = sess.run(c,
  590. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  591. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  592. Assert.AreEqual((double)o, doubleResult);
  593. }
  594. // Testing `operator *(Tensor x, Tensor y)
  595. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  596. using (var sess = tf.Session())
  597. {
  598. var o = sess.run(c,
  599. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  600. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  601. Assert.AreEqual((double)o, doubleResult);
  602. }
  603. // Testing `operator *(Tensor x, double y)
  604. c = tf.reduce_sum(tf.reduce_sum(a * secondFloatVal, 1));
  605. using (var sess = tf.Session())
  606. {
  607. var o = sess.run(c,
  608. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  609. Assert.AreEqual((double)o, doubleResult);
  610. }
  611. // Testing `operator *(double x, Tensor y)
  612. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal * b, 1));
  613. using (var sess = tf.Session())
  614. {
  615. var o = sess.run(c,
  616. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  617. Assert.AreEqual((double)o, doubleResult);
  618. }
  619. #endregion
  620. }
  621. [TestMethod]
  622. public void divOpTests()
  623. {
  624. const int rows = 2; // to avoid broadcasting effect
  625. const int cols = 10;
  626. #region intTest
  627. const int firstIntVal = 6;
  628. const int secondIntVal = 3;
  629. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  630. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  631. var intResult = (int)(firstIntFeed.Sum() / (float)secondIntVal);
  632. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  633. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  634. var c = tf.reduce_sum(tf.reduce_sum(gen_math_ops.floor_div(a, b), 1));
  635. using (var sess = tf.Session())
  636. {
  637. var o = sess.run(c,
  638. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  639. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  640. Assert.AreEqual((int)o, intResult);
  641. }
  642. // Testing `operator /(Tensor x, Tensor y)
  643. c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
  644. using (var sess = tf.Session())
  645. {
  646. var o = sess.run(c,
  647. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  648. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  649. Assert.AreEqual((int)o, intResult);
  650. }
  651. // Testing `operator /(Tensor x, int y)
  652. c = tf.reduce_sum(tf.reduce_sum(a / secondIntVal, 1));
  653. using (var sess = tf.Session())
  654. {
  655. var o = sess.run(c,
  656. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  657. Assert.AreEqual((int)o, intResult);
  658. }
  659. // Testing `operator /(int x, Tensor y)
  660. c = tf.reduce_sum(tf.reduce_sum(firstIntVal / b, 1));
  661. using (var sess = tf.Session())
  662. {
  663. var o = sess.run(c,
  664. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  665. Assert.AreEqual((int)o, intResult);
  666. }
  667. #endregion
  668. #region floatTest
  669. const float firstFloatVal = 6.0f;
  670. const float secondFloatVal = 3.0f;
  671. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  672. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  673. var floatResult = MultiplyArray(firstFloatFeed, secondFloatFeed.Select(x => 1/x).ToArray()).Sum();
  674. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  675. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  676. c = tf.reduce_sum(tf.reduce_sum(tf.divide(a, b), 1));
  677. using (var sess = tf.Session())
  678. {
  679. var o = sess.run(c,
  680. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  681. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  682. Assert.AreEqual((float)o, floatResult);
  683. }
  684. // Testing `operator /(Tensor x, Tensor y)
  685. c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
  686. using (var sess = tf.Session())
  687. {
  688. var o = sess.run(c,
  689. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  690. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  691. Assert.AreEqual((float)o, floatResult);
  692. }
  693. // Testing `operator /(Tensor x, float y)
  694. c = tf.reduce_sum(tf.reduce_sum(a / secondFloatVal, 1));
  695. using (var sess = tf.Session())
  696. {
  697. var o = sess.run(c,
  698. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  699. Assert.AreEqual((float)o, floatResult);
  700. }
  701. // Testing `operator /(float x, Tensor y)
  702. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal / b, 1));
  703. using (var sess = tf.Session())
  704. {
  705. var o = sess.run(c,
  706. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  707. Assert.AreEqual((float)o, floatResult);
  708. }
  709. #endregion
  710. #region doubleTest
  711. const double firstDoubleVal = 6.0;
  712. const double secondDoubleVal = 3.0;
  713. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  714. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  715. var doubleResult = MultiplyArray(firstDoubleFeed, secondDoubleFeed.Select(x => 1/x).ToArray()).Sum();
  716. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  717. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  718. c = tf.reduce_sum(tf.reduce_sum(tf.divide(a, b), 1));
  719. using (var sess = tf.Session())
  720. {
  721. var o = sess.run(c,
  722. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  723. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  724. Assert.AreEqual((double)o, doubleResult);
  725. }
  726. // Testing `operator /(Tensor x, Tensor y)
  727. c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
  728. using (var sess = tf.Session())
  729. {
  730. var o = sess.run(c,
  731. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  732. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  733. Assert.AreEqual((double)o, doubleResult);
  734. }
  735. // Testing `operator /(Tensor x, double y)
  736. c = tf.reduce_sum(tf.reduce_sum(a / secondFloatVal, 1));
  737. using (var sess = tf.Session())
  738. {
  739. var o = sess.run(c,
  740. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  741. Assert.AreEqual((double)o, doubleResult);
  742. }
  743. // Testing `operator /(double x, Tensor y)
  744. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal / b, 1));
  745. using (var sess = tf.Session())
  746. {
  747. var o = sess.run(c,
  748. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  749. Assert.AreEqual((double)o, doubleResult);
  750. }
  751. #endregion
  752. }
  753. [TestMethod]
  754. public void greaterThanOpTests()
  755. {
  756. const int rows = 2; // to avoid broadcasting effect
  757. const int cols = 10;
  758. #region intTest
  759. const int intThreshold = 10;
  760. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  761. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  762. var intResult = firstIntFeed.Count(elem => elem > intThreshold);
  763. var intResultTwo = firstIntFeed.Count(elem => elem < intThreshold);
  764. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  765. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  766. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
  767. using (var sess = tf.Session())
  768. {
  769. var o = sess.run(c,
  770. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  771. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  772. Assert.AreEqual((int)o, intResult);
  773. }
  774. // Testing `operator >(Tensor x, Tensor y)
  775. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
  776. using (var sess = tf.Session())
  777. {
  778. var o = sess.run(c,
  779. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  780. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  781. Assert.AreEqual((int)o, intResult);
  782. }
  783. // Testing `operator >(Tensor x, int y)
  784. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > intThreshold, tf.int32), 1));
  785. using (var sess = tf.Session())
  786. {
  787. var o = sess.run(c,
  788. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  789. Assert.AreEqual((int)o, intResult);
  790. }
  791. // Testing `operator >(int x, Tensor y)
  792. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold > a, tf.int32), 1));
  793. using (var sess = tf.Session())
  794. {
  795. var o = sess.run(c,
  796. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  797. Assert.AreEqual((int)o, intResultTwo);
  798. }
  799. #endregion
  800. #region floatTest
  801. const float floatThreshold = 10.0f;
  802. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  803. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  804. var floatResult = firstFloatFeed.Count(elem => elem > floatThreshold);
  805. var floatResultTwo = firstFloatFeed.Count(elem => elem < floatThreshold);
  806. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  807. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  808. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
  809. using (var sess = tf.Session())
  810. {
  811. var o = sess.run(c,
  812. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  813. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  814. Assert.AreEqual((int)o, floatResult);
  815. }
  816. // Testing `operator >(Tensor x, Tensor y)
  817. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
  818. using (var sess = tf.Session())
  819. {
  820. var o = sess.run(c,
  821. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  822. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  823. Assert.AreEqual((int)o, floatResult);
  824. }
  825. // Testing `operator >(Tensor x, float y)
  826. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > floatThreshold, tf.int32), 1));
  827. using (var sess = tf.Session())
  828. {
  829. var o = sess.run(c,
  830. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  831. Assert.AreEqual((int)o, floatResult);
  832. }
  833. // Testing `operator >(float x, Tensor y)
  834. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold > a, tf.int32), 1));
  835. using (var sess = tf.Session())
  836. {
  837. var o = sess.run(c,
  838. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  839. Assert.AreEqual((int)o, floatResultTwo);
  840. }
  841. #endregion
  842. #region doubleTest
  843. const double doubleThreshold = 10.0;
  844. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  845. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  846. var doubleResult = firstDoubleFeed.Count(elem => elem > doubleThreshold);
  847. var doubleResultTwo = firstDoubleFeed.Count(elem => elem < doubleThreshold);
  848. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  849. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  850. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
  851. using (var sess = tf.Session())
  852. {
  853. var o = sess.run(c,
  854. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  855. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  856. Assert.AreEqual((int)o, doubleResult);
  857. }
  858. // Testing `operator >(Tensor x, Tensor y)
  859. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
  860. using (var sess = tf.Session())
  861. {
  862. var o = sess.run(c,
  863. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  864. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  865. Assert.AreEqual((int)o, doubleResult);
  866. }
  867. // Testing `operator >(Tensor x, double y)
  868. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > doubleThreshold, tf.int32), 1));
  869. using (var sess = tf.Session())
  870. {
  871. var o = sess.run(c,
  872. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  873. Assert.AreEqual((int)o, doubleResult);
  874. }
  875. // Testing `operator >(double x, Tensor y)
  876. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold > a, tf.int32), 1));
  877. using (var sess = tf.Session())
  878. {
  879. var o = sess.run(c,
  880. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  881. Assert.AreEqual((int)o, doubleResultTwo);
  882. }
  883. #endregion
  884. }
  885. [TestMethod]
  886. public void lessThanOpTests()
  887. {
  888. const int rows = 2; // to avoid broadcasting effect
  889. const int cols = 10;
  890. #region intTest
  891. const int intThreshold = 10;
  892. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  893. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  894. var intResult = firstIntFeed.Count(elem => elem < intThreshold);
  895. var intResultTwo = firstIntFeed.Count(elem => elem > intThreshold);
  896. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  897. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  898. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
  899. using (var sess = tf.Session())
  900. {
  901. var o = sess.run(c,
  902. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  903. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  904. Assert.AreEqual((int)o, intResult);
  905. }
  906. // Testing `operator <(Tensor x, Tensor y)
  907. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
  908. using (var sess = tf.Session())
  909. {
  910. var o = sess.run(c,
  911. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  912. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  913. Assert.AreEqual((int)o, intResult);
  914. }
  915. // Testing `operator <(Tensor x, int y)
  916. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < intThreshold, tf.int32), 1));
  917. using (var sess = tf.Session())
  918. {
  919. var o = sess.run(c,
  920. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  921. Assert.AreEqual((int)o, intResult);
  922. }
  923. // Testing `operator <(int x, Tensor y)
  924. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold < a, tf.int32), 1));
  925. using (var sess = tf.Session())
  926. {
  927. var o = sess.run(c,
  928. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  929. Assert.AreEqual((int)o, intResultTwo);
  930. }
  931. #endregion
  932. #region floatTest
  933. const float floatThreshold = 10.0f;
  934. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  935. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  936. var floatResult = firstFloatFeed.Count(elem => elem < floatThreshold);
  937. var floatResultTwo = firstFloatFeed.Count(elem => elem > floatThreshold);
  938. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  939. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  940. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
  941. using (var sess = tf.Session())
  942. {
  943. var o = sess.run(c,
  944. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  945. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  946. Assert.AreEqual((int)o, floatResult);
  947. }
  948. // Testing `operator <(Tensor x, Tensor y)
  949. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
  950. using (var sess = tf.Session())
  951. {
  952. var o = sess.run(c,
  953. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  954. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  955. Assert.AreEqual((int)o, floatResult);
  956. }
  957. // Testing `operator <(Tensor x, float y)
  958. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < floatThreshold, tf.int32), 1));
  959. using (var sess = tf.Session())
  960. {
  961. var o = sess.run(c,
  962. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  963. Assert.AreEqual((int)o, floatResult);
  964. }
  965. // Testing `operator <(float x, Tensor y)
  966. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold < a, tf.int32), 1));
  967. using (var sess = tf.Session())
  968. {
  969. var o = sess.run(c,
  970. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  971. Assert.AreEqual((int)o, floatResultTwo);
  972. }
  973. #endregion
  974. #region doubleTest
  975. const double doubleThreshold = 10.0;
  976. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  977. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  978. var doubleResult = firstDoubleFeed.Count(elem => elem < doubleThreshold);
  979. var doubleResultTwo = firstDoubleFeed.Count(elem => elem > doubleThreshold);
  980. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  981. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  982. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
  983. using (var sess = tf.Session())
  984. {
  985. var o = sess.run(c,
  986. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  987. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  988. Assert.AreEqual((int)o, doubleResult);
  989. }
  990. // Testing `operator <(Tensor x, Tensor y)
  991. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
  992. using (var sess = tf.Session())
  993. {
  994. var o = sess.run(c,
  995. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  996. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  997. Assert.AreEqual((int)o, doubleResult);
  998. }
  999. // Testing `operator <(Tensor x, double y)
  1000. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < doubleThreshold, tf.int32), 1));
  1001. using (var sess = tf.Session())
  1002. {
  1003. var o = sess.run(c,
  1004. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  1005. Assert.AreEqual((int)o, doubleResult);
  1006. }
  1007. // Testing `operator <(double x, Tensor y)
  1008. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold < a, tf.int32), 1));
  1009. using (var sess = tf.Session())
  1010. {
  1011. var o = sess.run(c,
  1012. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  1013. Assert.AreEqual((int)o, doubleResultTwo);
  1014. }
  1015. #endregion
  1016. }
  1017. [TestMethod]
  1018. public void greaterOrEqualThanOpTests()
  1019. {
  1020. const int rows = 2; // to avoid broadcasting effect
  1021. const int cols = 10;
  1022. #region intTest
  1023. const int intThreshold = 10;
  1024. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  1025. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  1026. var intResult = firstIntFeed.Count(elem => elem >= intThreshold);
  1027. var intResultTwo = firstIntFeed.Count(elem => elem <= intThreshold);
  1028. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  1029. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  1030. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
  1031. using (var sess = tf.Session())
  1032. {
  1033. var o = sess.run(c,
  1034. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  1035. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  1036. Assert.AreEqual((int)o, intResult);
  1037. }
  1038. // Testing `operator >=(Tensor x, Tensor y)
  1039. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= b, tf.int32), 1));
  1040. using (var sess = tf.Session())
  1041. {
  1042. var o = sess.run(c,
  1043. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  1044. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  1045. Assert.AreEqual((int)o, intResult);
  1046. }
  1047. // Testing `operator >=(Tensor x, int y)
  1048. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= intThreshold, tf.int32), 1));
  1049. using (var sess = tf.Session())
  1050. {
  1051. var o = sess.run(c,
  1052. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  1053. Assert.AreEqual((int)o, intResult);
  1054. }
  1055. // Testing `operator >=(int x, Tensor y)
  1056. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold >= a, tf.int32), 1));
  1057. using (var sess = tf.Session())
  1058. {
  1059. var o = sess.run(c,
  1060. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  1061. Assert.AreEqual((int)o, intResultTwo);
  1062. }
  1063. #endregion
  1064. #region floatTest
  1065. const float floatThreshold = 10.0f;
  1066. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  1067. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  1068. var floatResult = firstFloatFeed.Count(elem => elem >= floatThreshold);
  1069. var floatResultTwo = firstFloatFeed.Count(elem => elem <= floatThreshold);
  1070. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  1071. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  1072. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
  1073. using (var sess = tf.Session())
  1074. {
  1075. var o = sess.run(c,
  1076. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  1077. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  1078. Assert.AreEqual((int)o, floatResult);
  1079. }
  1080. // Testing `operator >=(Tensor x, Tensor y)
  1081. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= b, tf.int32), 1));
  1082. using (var sess = tf.Session())
  1083. {
  1084. var o = sess.run(c,
  1085. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  1086. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  1087. Assert.AreEqual((int)o, floatResult);
  1088. }
  1089. // Testing `operator >=(Tensor x, float y)
  1090. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= floatThreshold, tf.int32), 1));
  1091. using (var sess = tf.Session())
  1092. {
  1093. var o = sess.run(c,
  1094. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  1095. Assert.AreEqual((int)o, floatResult);
  1096. }
  1097. // Testing `operator >=(float x, Tensor y)
  1098. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold >= a, tf.int32), 1));
  1099. using (var sess = tf.Session())
  1100. {
  1101. var o = sess.run(c,
  1102. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  1103. Assert.AreEqual((int)o, floatResultTwo);
  1104. }
  1105. #endregion
  1106. #region doubleTest
  1107. const double doubleThreshold = 10.0;
  1108. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  1109. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  1110. var doubleResult = firstDoubleFeed.Count(elem => elem >= doubleThreshold);
  1111. var doubleResultTwo = firstDoubleFeed.Count(elem => elem <= doubleThreshold);
  1112. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  1113. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  1114. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
  1115. using (var sess = tf.Session())
  1116. {
  1117. var o = sess.run(c,
  1118. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  1119. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  1120. Assert.AreEqual((int)o, doubleResult);
  1121. }
  1122. // Testing `operator >=(Tensor x, Tensor y)
  1123. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= b, tf.int32), 1));
  1124. using (var sess = tf.Session())
  1125. {
  1126. var o = sess.run(c,
  1127. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  1128. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  1129. Assert.AreEqual((int)o, doubleResult);
  1130. }
  1131. // Testing `operator >=(Tensor x, double y)
  1132. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= doubleThreshold, tf.int32), 1));
  1133. using (var sess = tf.Session())
  1134. {
  1135. var o = sess.run(c,
  1136. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  1137. Assert.AreEqual((int)o, doubleResult);
  1138. }
  1139. // Testing `operator >=(double x, Tensor y)
  1140. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold >= a, tf.int32), 1));
  1141. using (var sess = tf.Session())
  1142. {
  1143. var o = sess.run(c,
  1144. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  1145. Assert.AreEqual((int)o, doubleResultTwo);
  1146. }
  1147. #endregion
  1148. }
  1149. [TestMethod]
  1150. public void lessOrEqualThanOpTests()
  1151. {
  1152. const int rows = 2; // to avoid broadcasting effect
  1153. const int cols = 10;
  1154. #region intTest
  1155. const int intThreshold = 10;
  1156. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  1157. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  1158. var intResult = firstIntFeed.Count(elem => elem <= intThreshold);
  1159. var intResultTwo = firstIntFeed.Count(elem => elem >= intThreshold);
  1160. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  1161. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  1162. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
  1163. using (var sess = tf.Session())
  1164. {
  1165. var o = sess.run(c,
  1166. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  1167. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  1168. Assert.AreEqual((int)o, intResult);
  1169. }
  1170. // Testing `operator <=(Tensor x, Tensor y)
  1171. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= b, tf.int32), 1));
  1172. using (var sess = tf.Session())
  1173. {
  1174. var o = sess.run(c,
  1175. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  1176. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  1177. Assert.AreEqual((int)o, intResult);
  1178. }
  1179. // Testing `operator <=(Tensor x, int y)
  1180. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= intThreshold, tf.int32), 1));
  1181. using (var sess = tf.Session())
  1182. {
  1183. var o = sess.run(c,
  1184. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  1185. Assert.AreEqual((int)o, intResult);
  1186. }
  1187. // Testing `operator <=(int x, Tensor y)
  1188. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold <= a, tf.int32), 1));
  1189. using (var sess = tf.Session())
  1190. {
  1191. var o = sess.run(c,
  1192. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  1193. Assert.AreEqual((int)o, intResultTwo);
  1194. }
  1195. #endregion
  1196. #region floatTest
  1197. const float floatThreshold = 10.0f;
  1198. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  1199. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  1200. var floatResult = firstFloatFeed.Count(elem => elem <= floatThreshold);
  1201. var floatResultTwo = firstFloatFeed.Count(elem => elem >= floatThreshold);
  1202. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  1203. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  1204. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
  1205. using (var sess = tf.Session())
  1206. {
  1207. var o = sess.run(c,
  1208. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  1209. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  1210. Assert.AreEqual((int)o, floatResult);
  1211. }
  1212. // Testing `operator <=(Tensor x, Tensor y)
  1213. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= b, tf.int32), 1));
  1214. using (var sess = tf.Session())
  1215. {
  1216. var o = sess.run(c,
  1217. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  1218. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  1219. Assert.AreEqual((int)o, floatResult);
  1220. }
  1221. // Testing `operator <=(Tensor x, float y)
  1222. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= floatThreshold, tf.int32), 1));
  1223. using (var sess = tf.Session())
  1224. {
  1225. var o = sess.run(c,
  1226. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  1227. Assert.AreEqual((int)o, floatResult);
  1228. }
  1229. // Testing `operator <=(float x, Tensor y)
  1230. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold <= a, tf.int32), 1));
  1231. using (var sess = tf.Session())
  1232. {
  1233. var o = sess.run(c,
  1234. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  1235. Assert.AreEqual((int)o, floatResultTwo);
  1236. }
  1237. #endregion
  1238. #region doubleTest
  1239. const double doubleThreshold = 10.0;
  1240. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  1241. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  1242. var doubleResult = firstDoubleFeed.Count(elem => elem <= doubleThreshold);
  1243. var doubleResultTwo = firstDoubleFeed.Count(elem => elem >= doubleThreshold);
  1244. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  1245. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  1246. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
  1247. using (var sess = tf.Session())
  1248. {
  1249. var o = sess.run(c,
  1250. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  1251. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  1252. Assert.AreEqual((int)o, doubleResult);
  1253. }
  1254. // Testing `operator <=(Tensor x, Tensor y)
  1255. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= b, tf.int32), 1));
  1256. using (var sess = tf.Session())
  1257. {
  1258. var o = sess.run(c,
  1259. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  1260. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  1261. Assert.AreEqual((int)o, doubleResult);
  1262. }
  1263. // Testing `operator <=(Tensor x, double y)
  1264. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= doubleThreshold, tf.int32), 1));
  1265. using (var sess = tf.Session())
  1266. {
  1267. var o = sess.run(c,
  1268. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  1269. Assert.AreEqual((int)o, doubleResult);
  1270. }
  1271. // Testing `operator <=(double x, Tensor y)
  1272. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold <= a, tf.int32), 1));
  1273. using (var sess = tf.Session())
  1274. {
  1275. var o = sess.run(c,
  1276. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  1277. Assert.AreEqual((int)o, doubleResultTwo);
  1278. }
  1279. #endregion
  1280. }
  1281. [Ignore("Not finished yet")]
  1282. [TestMethod]
  1283. public void map_fn()
  1284. {
  1285. var a = tf.constant(new[] { 1, 2, 3, 4 });
  1286. var b = tf.constant(new[] { 17, 12, 11, 10 });
  1287. var ab = tf.stack(new[] { a, b }, 1);
  1288. Func<Tensor, Tensor> map_operation = (value_ab) =>
  1289. {
  1290. var value_a = value_ab[0];
  1291. var value_b = value_ab[1];
  1292. return value_a + value_b;
  1293. };
  1294. var map_result = tf.map_fn(map_operation, ab);
  1295. }
  1296. }
  1297. }