You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

OperationsTest.cs 64 kB

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