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