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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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. [TestMethod]
  1282. public void map_fn()
  1283. {
  1284. var a = tf.constant(new[] { 1, 2, 3, 4 });
  1285. var b = tf.constant(new[] { 17, 12, 11, 10 });
  1286. var ab = tf.stack(new[] { a, b }, 1);
  1287. Func<Tensor, Tensor> map_operation = (value_ab) =>
  1288. {
  1289. var value_a = value_ab[0];
  1290. var value_b = value_ab[1];
  1291. return value_a + value_b;
  1292. };
  1293. var map_result = tf.map_fn(map_operation, ab);
  1294. }
  1295. }
  1296. }