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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Tensorflow.NumPy;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Tensorflow;
  7. using static Tensorflow.Binding;
  8. using Buffer = Tensorflow.Buffer;
  9. namespace TensorFlowNET.UnitTest.Basics
  10. {
  11. [TestClass]
  12. public class OperationsTest : GraphModeTestBase
  13. {
  14. /// <summary>
  15. /// Port from tensorflow\c\c_api_test.cc
  16. /// `TEST(CAPI, GetAllOpList)`
  17. /// </summary>
  18. [TestMethod]
  19. public void GetAllOpList()
  20. {
  21. var handle = c_api.TF_GetAllOpList();
  22. using var buffer = new Buffer(handle);
  23. var op_list = OpList.Parser.ParseFrom(buffer.ToArray());
  24. var _registered_ops = new Dictionary<string, OpDef>();
  25. foreach (var op_def in op_list.Op)
  26. _registered_ops[op_def.Name] = op_def;
  27. // r1.14 added NN op
  28. var op = _registered_ops.FirstOrDefault(x => x.Key == "NearestNeighbors");
  29. Assert.IsTrue(op_list.Op.Count > 1000);
  30. }
  31. [TestMethod]
  32. public void addInPlaceholder()
  33. {
  34. var a = tf.placeholder(tf.float32);
  35. var b = tf.placeholder(tf.float32);
  36. var c = tf.add(a, b);
  37. using (var sess = tf.Session())
  38. {
  39. var o = sess.run(c,
  40. new FeedItem(a, 3.0f),
  41. new FeedItem(b, 2.0f));
  42. Assert.AreEqual(o, 5.0f);
  43. }
  44. }
  45. [TestMethod]
  46. public void addInConstant()
  47. {
  48. var a = tf.constant(4.0f);
  49. var b = tf.constant(5.0f);
  50. var c = tf.add(a, b);
  51. using (var sess = tf.Session())
  52. {
  53. var o = sess.run(c);
  54. Assert.AreEqual(o, 9.0f);
  55. }
  56. }
  57. [TestMethod]
  58. public void isFinite()
  59. {
  60. var a = tf.constant(new[] { 1, np.nan, 2, np.nan, 3, np.nan, 4, np.nan });
  61. var b = tf.cast(tf.is_finite(a), tf.float32);
  62. var check = np.array(1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
  63. using (var sess = tf.Session())
  64. {
  65. var o = sess.run(b);
  66. Assert.IsTrue(np.array_equal(o, check));
  67. }
  68. }
  69. [TestMethod]
  70. public void isNan()
  71. {
  72. var a = tf.constant(new[] { 1, np.nan, 2, np.nan, 3, np.nan, 4, np.nan });
  73. var b = tf.cast(tf.is_nan(a), tf.float32);
  74. var check = np.array(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
  75. using (var sess = tf.Session())
  76. {
  77. var o = sess.run(b);
  78. Assert.IsTrue(np.array_equal(o, check));
  79. }
  80. }
  81. [TestMethod]
  82. public void cumSumTest()
  83. {
  84. var a = tf.constant(new[] { 1, 1, 2, 3, 4, 5 });
  85. var b = tf.cumsum(a);
  86. var check = np.array(1, 2, 4, 7, 11, 16);
  87. using (var sess = tf.Session())
  88. {
  89. var o = sess.run(b);
  90. Assert.IsTrue(np.array_equal(o, check));
  91. }
  92. b = tf.cumsum(a, exclusive: true);
  93. check = np.array(0, 1, 2, 4, 7, 11);
  94. using (var sess = tf.Session())
  95. {
  96. var o = sess.run(b);
  97. Assert.IsTrue(np.array_equal(o, check));
  98. }
  99. b = tf.cumsum(a, reverse: true);
  100. check = np.array(16, 15, 14, 12, 9, 5);
  101. using (var sess = tf.Session())
  102. {
  103. var o = sess.run(b);
  104. Assert.IsTrue(np.array_equal(o, check));
  105. }
  106. b = tf.cumsum(a, exclusive: true, reverse: true);
  107. check = np.array(15, 14, 12, 9, 5, 0);
  108. using (var sess = tf.Session())
  109. {
  110. var o = sess.run(b);
  111. Assert.IsTrue(np.array_equal(o, check));
  112. }
  113. }
  114. [TestMethod]
  115. public void logicalOpsTest()
  116. {
  117. var a = tf.constant(new[] { 1f, 2f, 3f, 4f, -4f, -3f, -2f, -1f });
  118. var b = tf.less(a, 0f);
  119. var c = tf.greater(a, 0f);
  120. var d = tf.cast(tf.logical_and(b, c), tf.int32);
  121. var check = np.array(new[] { 0, 0, 0, 0, 0, 0, 0, 0 });
  122. using (var sess = tf.Session())
  123. {
  124. var o = sess.run(d);
  125. Assert.IsTrue(np.array_equal(o, check));
  126. }
  127. d = tf.cast(tf.logical_not(b), tf.int32);
  128. check = np.array(new[] { 1, 1, 1, 1, 0, 0, 0, 0 });
  129. using (var sess = tf.Session())
  130. {
  131. var o = sess.run(d);
  132. Assert.IsTrue(np.array_equal(o, check));
  133. }
  134. d = tf.cast(tf.logical_or(b, c), tf.int32);
  135. check = np.array(new[] { 1, 1, 1, 1, 1, 1, 1, 1 });
  136. using (var sess = tf.Session())
  137. {
  138. var o = sess.run(d);
  139. Assert.IsTrue(np.array_equal(o, check));
  140. }
  141. d = tf.cast(tf.logical_xor(b, c), tf.int32);
  142. check = np.array(new[] { 1, 1, 1, 1, 1, 1, 1, 1 });
  143. using (var sess = tf.Session())
  144. {
  145. var o = sess.run(d);
  146. Assert.IsTrue(np.array_equal(o, check));
  147. }
  148. }
  149. [TestMethod]
  150. public void addOpTests()
  151. {
  152. const int rows = 2; // to avoid broadcasting effect
  153. const int cols = 10;
  154. #region intTest
  155. const int firstIntVal = 2;
  156. const int secondIntVal = 3;
  157. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  158. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  159. var intResult = firstIntFeed.Sum() + secondIntFeed.Sum();
  160. var a = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
  161. var b = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
  162. var c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  163. using (var sess = tf.Session())
  164. {
  165. var o = sess.run(c,
  166. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  167. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  168. Assert.AreEqual(o, intResult);
  169. }
  170. // Testing `operator +(Tensor x, Tensor y)`
  171. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  172. using (var sess = tf.Session())
  173. {
  174. var o = sess.run(c,
  175. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  176. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  177. Assert.AreEqual(o, intResult);
  178. }
  179. // Testing `operator +(Tensor x, int y)`
  180. c = tf.reduce_sum(tf.reduce_sum(a + secondIntVal, 1));
  181. using (var sess = tf.Session())
  182. {
  183. var o = sess.run(c,
  184. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  185. Assert.AreEqual(o, intResult);
  186. }
  187. // Testing `operator +(int x, Tensor y)`
  188. c = tf.reduce_sum(tf.reduce_sum(secondIntVal + a, 1));
  189. using (var sess = tf.Session())
  190. {
  191. var o = sess.run(c,
  192. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  193. Assert.AreEqual(o, intResult);
  194. }
  195. #endregion
  196. #region floatTest
  197. const float firstFloatVal = 2.0f;
  198. const float secondFloatVal = 3.0f;
  199. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  200. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  201. var floatResult = firstFloatFeed.Sum() + secondFloatFeed.Sum();
  202. a = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
  203. b = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
  204. c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  205. using (var sess = tf.Session())
  206. {
  207. var o = sess.run(c,
  208. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  209. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  210. Assert.AreEqual(o, floatResult);
  211. }
  212. // Testing `operator +(Tensor x, Tensor y)
  213. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  214. using (var sess = tf.Session())
  215. {
  216. var o = sess.run(c,
  217. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  218. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  219. Assert.AreEqual(o, floatResult);
  220. }
  221. // Testing `operator +(Tensor x, float y)
  222. c = tf.reduce_sum(tf.reduce_sum(a + secondFloatVal, 1));
  223. using (var sess = tf.Session())
  224. {
  225. var o = sess.run(c,
  226. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  227. Assert.AreEqual(o, floatResult);
  228. }
  229. // Testing `operator +(float x, Tensor y)
  230. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal + a, 1));
  231. using (var sess = tf.Session())
  232. {
  233. var o = sess.run(c,
  234. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  235. Assert.AreEqual(o, floatResult);
  236. }
  237. #endregion
  238. #region doubleTest
  239. const double firstDoubleVal = 2.0;
  240. const double secondDoubleVal = 3.0;
  241. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  242. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  243. var doubleResult = firstDoubleFeed.Sum() + secondDoubleFeed.Sum();
  244. a = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
  245. b = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
  246. c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  247. using (var sess = tf.Session())
  248. {
  249. var o = sess.run(c,
  250. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  251. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  252. Assert.AreEqual((double)o, doubleResult);
  253. }
  254. // Testing `operator +(Tensor x, Tensor y)
  255. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  256. using (var sess = tf.Session())
  257. {
  258. var o = sess.run(c,
  259. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  260. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  261. Assert.AreEqual(o, doubleResult);
  262. }
  263. // Testing `operator +(Tensor x, double y)
  264. c = tf.reduce_sum(tf.reduce_sum(a + secondDoubleVal, 1));
  265. using (var sess = tf.Session())
  266. {
  267. var o = sess.run(c,
  268. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  269. Assert.AreEqual(o, doubleResult);
  270. }
  271. // Testing `operator +(double x, Tensor y)
  272. c = tf.reduce_sum(tf.reduce_sum(secondDoubleVal + a, 1));
  273. using (var sess = tf.Session())
  274. {
  275. var o = sess.run(c,
  276. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  277. Assert.AreEqual(o, doubleResult);
  278. }
  279. #endregion
  280. }
  281. [TestMethod]
  282. public void subOpTests()
  283. {
  284. const int rows = 2; // to avoid broadcasting effect
  285. const int cols = 10;
  286. #region intTest
  287. const int firstIntVal = -2;
  288. const int secondIntVal = 3;
  289. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  290. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  291. var intResult = firstIntFeed.Sum() - secondIntFeed.Sum();
  292. var intResultTwo = -firstIntFeed.Sum();
  293. var a = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
  294. var b = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
  295. var c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  296. using (var sess = tf.Session())
  297. {
  298. var o = sess.run(c,
  299. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  300. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  301. Assert.AreEqual((int)o, intResult);
  302. }
  303. // Testing `operator -(Tensor x, Tensor y)
  304. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  305. using (var sess = tf.Session())
  306. {
  307. var o = sess.run(c,
  308. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  309. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  310. Assert.AreEqual((int)o, intResult);
  311. }
  312. // Testing `operator -(Tensor x, int y)
  313. c = tf.reduce_sum(tf.reduce_sum(a - secondIntVal, 1));
  314. using (var sess = tf.Session())
  315. {
  316. var o = sess.run(c,
  317. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  318. Assert.AreEqual((int)o, intResult);
  319. }
  320. // Testing `operator -(int x, Tensor y)
  321. c = tf.reduce_sum(tf.reduce_sum(secondIntVal - a, 1));
  322. using (var sess = tf.Session())
  323. {
  324. var o = sess.run(c,
  325. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  326. Assert.AreEqual((int)o, Math.Abs(intResult));
  327. }
  328. // Testing `operator -(Tensor x)
  329. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  330. using (var sess = tf.Session())
  331. {
  332. var o = sess.run(c,
  333. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  334. Assert.AreEqual((int)o, intResultTwo);
  335. }
  336. #endregion
  337. #region floatTest
  338. const float firstFloatVal = -2.0f;
  339. const float secondFloatVal = 3.0f;
  340. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  341. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  342. var floatResult = firstFloatFeed.Sum() - secondFloatFeed.Sum();
  343. var floatResultTwo = -firstFloatFeed.Sum();
  344. a = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
  345. b = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
  346. c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  347. using (var sess = tf.Session())
  348. {
  349. var o = sess.run(c,
  350. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  351. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  352. Assert.AreEqual((float)o, floatResult);
  353. }
  354. // Testing `operator -(Tensor x, Tensor y)
  355. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  356. using (var sess = tf.Session())
  357. {
  358. var o = sess.run(c,
  359. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  360. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  361. Assert.AreEqual((float)o, floatResult);
  362. }
  363. // Testing `operator -(Tensor x, float y)
  364. c = tf.reduce_sum(tf.reduce_sum(a - secondFloatVal, 1));
  365. using (var sess = tf.Session())
  366. {
  367. var o = sess.run(c,
  368. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  369. Assert.AreEqual((float)o, floatResult);
  370. }
  371. // Testing `operator -(float x, Tensor y)
  372. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal - a, 1));
  373. using (var sess = tf.Session())
  374. {
  375. var o = sess.run(c,
  376. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  377. Assert.AreEqual((float)o, Math.Abs(floatResult));
  378. }
  379. // Testing `operator -(Tensor x)
  380. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  381. using (var sess = tf.Session())
  382. {
  383. var o = sess.run(c,
  384. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  385. Assert.AreEqual((float)o, floatResultTwo);
  386. }
  387. #endregion
  388. #region doubleTest
  389. const double firstDoubleVal = -2.0;
  390. const double secondDoubleVal = 3.0;
  391. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  392. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  393. var doubleResult = firstDoubleFeed.Sum() - secondDoubleFeed.Sum();
  394. var doubleResultTwo = -firstDoubleFeed.Sum();
  395. a = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
  396. b = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
  397. c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  398. using (var sess = tf.Session())
  399. {
  400. var o = sess.run(c,
  401. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  402. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  403. Assert.AreEqual((double)o, doubleResult);
  404. }
  405. // Testing `operator -(Tensor x, Tensor y)
  406. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  407. using (var sess = tf.Session())
  408. {
  409. var o = sess.run(c,
  410. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  411. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  412. Assert.AreEqual((double)o, doubleResult);
  413. }
  414. // Testing `operator -(Tensor x, double y)
  415. c = tf.reduce_sum(tf.reduce_sum(a - secondDoubleVal, 1));
  416. using (var sess = tf.Session())
  417. {
  418. var o = sess.run(c,
  419. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  420. Assert.AreEqual((double)o, doubleResult);
  421. }
  422. // Testing `operator -(double x, Tensor y)
  423. c = tf.reduce_sum(tf.reduce_sum(secondDoubleVal - a, 1));
  424. using (var sess = tf.Session())
  425. {
  426. var o = sess.run(c,
  427. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  428. Assert.AreEqual((double)o, Math.Abs(doubleResult));
  429. }
  430. // Testing `operator -(Tensor x)
  431. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  432. using (var sess = tf.Session())
  433. {
  434. var o = sess.run(c,
  435. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  436. Assert.AreEqual((double)o, doubleResultTwo);
  437. }
  438. #endregion
  439. }
  440. private IEnumerable<int> MultiplyArray(IReadOnlyCollection<int> first, IReadOnlyCollection<int> second)
  441. {
  442. if (first.Count != second.Count)
  443. throw new ArgumentException("Arrays should be of equal size!");
  444. var firstEnumerator = first.GetEnumerator();
  445. var secondEnumerator = second.GetEnumerator();
  446. var result = new List<int>();
  447. while (firstEnumerator.MoveNext())
  448. {
  449. secondEnumerator.MoveNext();
  450. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  451. }
  452. firstEnumerator.Dispose();
  453. secondEnumerator.Dispose();
  454. return result;
  455. }
  456. private IEnumerable<float> MultiplyArray(IReadOnlyCollection<float> first, IReadOnlyCollection<float> second)
  457. {
  458. if (first.Count != second.Count)
  459. throw new ArgumentException("Arrays should be of equal size!");
  460. var firstEnumerator = first.GetEnumerator();
  461. var secondEnumerator = second.GetEnumerator();
  462. var result = new List<float>();
  463. while (firstEnumerator.MoveNext())
  464. {
  465. secondEnumerator.MoveNext();
  466. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  467. }
  468. firstEnumerator.Dispose();
  469. secondEnumerator.Dispose();
  470. return result;
  471. }
  472. private IEnumerable<double> MultiplyArray(IReadOnlyCollection<double> first, IReadOnlyCollection<double> second)
  473. {
  474. if (first.Count != second.Count)
  475. throw new ArgumentException("Arrays should be of equal size!");
  476. var firstEnumerator = first.GetEnumerator();
  477. var secondEnumerator = second.GetEnumerator();
  478. var result = new List<double>();
  479. while (firstEnumerator.MoveNext())
  480. {
  481. secondEnumerator.MoveNext();
  482. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  483. }
  484. firstEnumerator.Dispose();
  485. secondEnumerator.Dispose();
  486. return result;
  487. }
  488. [TestMethod]
  489. public void mulOpTests()
  490. {
  491. const int rows = 2; // to avoid broadcasting effect
  492. const int cols = 10;
  493. #region intTest
  494. const int firstIntVal = 2;
  495. const int secondIntVal = 3;
  496. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  497. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  498. var intResult = MultiplyArray(firstIntFeed, secondIntFeed).Sum();
  499. var a = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
  500. var b = tf.placeholder(tf.int32, shape: new Shape(rows, cols));
  501. var c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  502. using (var sess = tf.Session())
  503. {
  504. var o = sess.run(c,
  505. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  506. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  507. Assert.AreEqual((int)o, intResult);
  508. }
  509. // Testing `operator *(Tensor x, Tensor y)
  510. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  511. using (var sess = tf.Session())
  512. {
  513. var o = sess.run(c,
  514. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  515. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  516. Assert.AreEqual((int)o, intResult);
  517. }
  518. // Testing `operator *(Tensor x, int y)
  519. c = tf.reduce_sum(tf.reduce_sum(a * secondIntVal, 1));
  520. using (var sess = tf.Session())
  521. {
  522. var o = sess.run(c,
  523. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  524. Assert.AreEqual((int)o, intResult);
  525. }
  526. // Testing `operator *(int x, Tensor y)
  527. c = tf.reduce_sum(tf.reduce_sum(firstIntVal * b, 1));
  528. using (var sess = tf.Session())
  529. {
  530. var o = sess.run(c,
  531. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  532. Assert.AreEqual((int)o, intResult);
  533. }
  534. #endregion
  535. #region floatTest
  536. const float firstFloatVal = 2.0f;
  537. const float secondFloatVal = 3.0f;
  538. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  539. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  540. var floatResult = MultiplyArray(firstFloatFeed, secondFloatFeed).Sum();
  541. a = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
  542. b = tf.placeholder(tf.float32, shape: new Shape(rows, cols));
  543. c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  544. using (var sess = tf.Session())
  545. {
  546. var o = sess.run(c,
  547. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  548. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  549. Assert.AreEqual((float)o, floatResult);
  550. }
  551. // Testing `operator *(Tensor x, Tensor y)
  552. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  553. using (var sess = tf.Session())
  554. {
  555. var o = sess.run(c,
  556. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  557. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  558. Assert.AreEqual((float)o, floatResult);
  559. }
  560. // Testing `operator *(Tensor x, float y)
  561. c = tf.reduce_sum(tf.reduce_sum(a * secondFloatVal, 1));
  562. using (var sess = tf.Session())
  563. {
  564. var o = sess.run(c,
  565. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  566. Assert.AreEqual((float)o, floatResult);
  567. }
  568. // Testing `operator *(float x, Tensor y)
  569. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal * b, 1));
  570. using (var sess = tf.Session())
  571. {
  572. var o = sess.run(c,
  573. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  574. Assert.AreEqual((float)o, floatResult);
  575. }
  576. #endregion
  577. #region doubleTest
  578. const double firstDoubleVal = 2.0;
  579. const double secondDoubleVal = 3.0;
  580. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  581. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  582. var doubleResult = MultiplyArray(firstDoubleFeed, secondDoubleFeed).Sum();
  583. a = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
  584. b = tf.placeholder(tf.float64, shape: new Shape(rows, cols));
  585. c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  586. using (var sess = tf.Session())
  587. {
  588. var o = sess.run(c,
  589. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  590. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  591. Assert.AreEqual((double)o, doubleResult);
  592. }
  593. // Testing `operator *(Tensor x, Tensor y)
  594. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  595. using (var sess = tf.Session())
  596. {
  597. var o = sess.run(c,
  598. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  599. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  600. Assert.AreEqual((double)o, doubleResult);
  601. }
  602. // Testing `operator *(Tensor x, double y)
  603. c = tf.reduce_sum(tf.reduce_sum(a * secondDoubleVal, 1));
  604. using (var sess = tf.Session())
  605. {
  606. var o = sess.run(c,
  607. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  608. Assert.AreEqual((double)o, doubleResult);
  609. }
  610. // Testing `operator *(double x, Tensor y)
  611. c = tf.reduce_sum(tf.reduce_sum(firstDoubleVal * b, 1));
  612. using (var sess = tf.Session())
  613. {
  614. var o = sess.run(c,
  615. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  616. Assert.AreEqual((double)o, doubleResult);
  617. }
  618. #endregion
  619. }
  620. [Ignore]
  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 Shape(rows, cols));
  633. var b = tf.placeholder(tf.int32, shape: new Shape(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 Shape(rows, cols));
  675. b = tf.placeholder(tf.float32, shape: new Shape(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 Shape(rows, cols));
  717. b = tf.placeholder(tf.float64, shape: new Shape(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 Shape(rows, cols));
  765. var b = tf.placeholder(tf.int32, shape: new Shape(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 Shape(rows, cols));
  807. b = tf.placeholder(tf.float32, shape: new Shape(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 Shape(rows, cols));
  849. b = tf.placeholder(tf.float64, shape: new Shape(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 Shape(rows, cols));
  897. var b = tf.placeholder(tf.int32, shape: new Shape(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 Shape(rows, cols));
  939. b = tf.placeholder(tf.float32, shape: new Shape(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 Shape(rows, cols));
  981. b = tf.placeholder(tf.float64, shape: new Shape(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 Shape(rows, cols));
  1029. var b = tf.placeholder(tf.int32, shape: new Shape(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 Shape(rows, cols));
  1071. b = tf.placeholder(tf.float32, shape: new Shape(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 Shape(rows, cols));
  1113. b = tf.placeholder(tf.float64, shape: new Shape(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 Shape(rows, cols));
  1161. var b = tf.placeholder(tf.int32, shape: new Shape(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 Shape(rows, cols));
  1203. b = tf.placeholder(tf.float32, shape: new Shape(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 Shape(rows, cols));
  1245. b = tf.placeholder(tf.float64, shape: new Shape(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. }