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

OperationsTest.cs 64 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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, 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, 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.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.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.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.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.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.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.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.array_equal(check));
  132. }
  133. }
  134. [TestMethod]
  135. public void addOpTests()
  136. {
  137. const int rows = 2; // to avoid broadcasting effect
  138. const int cols = 10;
  139. #region intTest
  140. const int firstIntVal = 2;
  141. const int secondIntVal = 3;
  142. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  143. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  144. var intResult = firstIntFeed.Sum() + secondIntFeed.Sum();
  145. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  146. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  147. var c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  148. using (var sess = tf.Session())
  149. {
  150. var o = sess.run(c,
  151. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  152. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  153. Assert.AreEqual((int)o, intResult);
  154. }
  155. // Testing `operator +(Tensor x, Tensor y)`
  156. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  157. using (var sess = tf.Session())
  158. {
  159. var o = sess.run(c,
  160. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  161. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  162. Assert.AreEqual((int)o, intResult);
  163. }
  164. // Testing `operator +(Tensor x, int y)`
  165. c = tf.reduce_sum(tf.reduce_sum(a + secondIntVal, 1));
  166. using (var sess = tf.Session())
  167. {
  168. var o = sess.run(c,
  169. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  170. Assert.AreEqual((int)o, intResult);
  171. }
  172. // Testing `operator +(int x, Tensor y)`
  173. c = tf.reduce_sum(tf.reduce_sum(secondIntVal + a, 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. Assert.AreEqual((int)o, intResult);
  179. }
  180. #endregion
  181. #region floatTest
  182. const float firstFloatVal = 2.0f;
  183. const float secondFloatVal = 3.0f;
  184. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  185. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  186. var floatResult = firstFloatFeed.Sum() + secondFloatFeed.Sum();
  187. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  188. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  189. c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  190. using (var sess = tf.Session())
  191. {
  192. var o = sess.run(c,
  193. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  194. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  195. Assert.AreEqual((float)o, floatResult);
  196. }
  197. // Testing `operator +(Tensor x, Tensor y)
  198. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  199. using (var sess = tf.Session())
  200. {
  201. var o = sess.run(c,
  202. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  203. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  204. Assert.AreEqual((float)o, floatResult);
  205. }
  206. // Testing `operator +(Tensor x, float y)
  207. c = tf.reduce_sum(tf.reduce_sum(a + secondFloatVal, 1));
  208. using (var sess = tf.Session())
  209. {
  210. var o = sess.run(c,
  211. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  212. Assert.AreEqual((float)o, floatResult);
  213. }
  214. // Testing `operator +(float x, Tensor y)
  215. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal + a, 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. Assert.AreEqual((float)o, floatResult);
  221. }
  222. #endregion
  223. #region doubleTest
  224. const double firstDoubleVal = 2.0;
  225. const double secondDoubleVal = 3.0;
  226. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  227. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  228. var doubleResult = firstDoubleFeed.Sum() + secondDoubleFeed.Sum();
  229. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  230. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  231. c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  232. using (var sess = tf.Session())
  233. {
  234. var o = sess.run(c,
  235. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  236. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  237. Assert.AreEqual((double)o, doubleResult);
  238. }
  239. // Testing `operator +(Tensor x, Tensor y)
  240. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  241. using (var sess = tf.Session())
  242. {
  243. var o = sess.run(c,
  244. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  245. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  246. Assert.AreEqual((double)o, doubleResult);
  247. }
  248. // Testing `operator +(Tensor x, double y)
  249. c = tf.reduce_sum(tf.reduce_sum(a + secondFloatVal, 1));
  250. using (var sess = tf.Session())
  251. {
  252. var o = sess.run(c,
  253. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  254. Assert.AreEqual((double)o, doubleResult);
  255. }
  256. // Testing `operator +(double x, Tensor y)
  257. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal + a, 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. Assert.AreEqual((double)o, doubleResult);
  263. }
  264. #endregion
  265. }
  266. [TestMethod]
  267. public void subOpTests()
  268. {
  269. const int rows = 2; // to avoid broadcasting effect
  270. const int cols = 10;
  271. #region intTest
  272. const int firstIntVal = -2;
  273. const int secondIntVal = 3;
  274. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  275. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  276. var intResult = firstIntFeed.Sum() - secondIntFeed.Sum();
  277. var intResultTwo = -firstIntFeed.Sum();
  278. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  279. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  280. var c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  281. using (var sess = tf.Session())
  282. {
  283. var o = sess.run(c,
  284. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  285. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  286. Assert.AreEqual((int)o, intResult);
  287. }
  288. // Testing `operator -(Tensor x, Tensor y)
  289. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  290. using (var sess = tf.Session())
  291. {
  292. var o = sess.run(c,
  293. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  294. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  295. Assert.AreEqual((int)o, intResult);
  296. }
  297. // Testing `operator -(Tensor x, int y)
  298. c = tf.reduce_sum(tf.reduce_sum(a - secondIntVal, 1));
  299. using (var sess = tf.Session())
  300. {
  301. var o = sess.run(c,
  302. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  303. Assert.AreEqual((int)o, intResult);
  304. }
  305. // Testing `operator -(int x, Tensor y)
  306. c = tf.reduce_sum(tf.reduce_sum(secondIntVal - a, 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. Assert.AreEqual((int)o, Math.Abs(intResult));
  312. }
  313. // Testing `operator -(Tensor x)
  314. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  315. using (var sess = tf.Session())
  316. {
  317. var o = sess.run(c,
  318. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  319. Assert.AreEqual((int)o, intResultTwo);
  320. }
  321. #endregion
  322. #region floatTest
  323. const float firstFloatVal = -2.0f;
  324. const float secondFloatVal = 3.0f;
  325. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  326. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  327. var floatResult = firstFloatFeed.Sum() - secondFloatFeed.Sum();
  328. var floatResultTwo = -firstFloatFeed.Sum();
  329. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  330. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  331. c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  332. using (var sess = tf.Session())
  333. {
  334. var o = sess.run(c,
  335. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  336. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  337. Assert.AreEqual((float)o, floatResult);
  338. }
  339. // Testing `operator -(Tensor x, Tensor y)
  340. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  341. using (var sess = tf.Session())
  342. {
  343. var o = sess.run(c,
  344. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  345. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  346. Assert.AreEqual((float)o, floatResult);
  347. }
  348. // Testing `operator -(Tensor x, float y)
  349. c = tf.reduce_sum(tf.reduce_sum(a - secondFloatVal, 1));
  350. using (var sess = tf.Session())
  351. {
  352. var o = sess.run(c,
  353. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  354. Assert.AreEqual((float)o, floatResult);
  355. }
  356. // Testing `operator -(float x, Tensor y)
  357. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal - a, 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. Assert.AreEqual((float)o, Math.Abs(floatResult));
  363. }
  364. // Testing `operator -(Tensor x)
  365. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  366. using (var sess = tf.Session())
  367. {
  368. var o = sess.run(c,
  369. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  370. Assert.AreEqual((float)o, floatResultTwo);
  371. }
  372. #endregion
  373. #region doubleTest
  374. const double firstDoubleVal = -2.0;
  375. const double secondDoubleVal = 3.0;
  376. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  377. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  378. var doubleResult = firstDoubleFeed.Sum() - secondDoubleFeed.Sum();
  379. var doubleResultTwo = -firstDoubleFeed.Sum();
  380. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  381. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  382. c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  383. using (var sess = tf.Session())
  384. {
  385. var o = sess.run(c,
  386. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  387. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  388. Assert.AreEqual((double)o, doubleResult);
  389. }
  390. // Testing `operator -(Tensor x, Tensor y)
  391. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  392. using (var sess = tf.Session())
  393. {
  394. var o = sess.run(c,
  395. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  396. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  397. Assert.AreEqual((double)o, doubleResult);
  398. }
  399. // Testing `operator -(Tensor x, double y)
  400. c = tf.reduce_sum(tf.reduce_sum(a - secondFloatVal, 1));
  401. using (var sess = tf.Session())
  402. {
  403. var o = sess.run(c,
  404. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  405. Assert.AreEqual((double)o, doubleResult);
  406. }
  407. // Testing `operator -(double x, Tensor y)
  408. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal - a, 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. Assert.AreEqual((double)o, Math.Abs(doubleResult));
  414. }
  415. // Testing `operator -(Tensor x)
  416. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  417. using (var sess = tf.Session())
  418. {
  419. var o = sess.run(c,
  420. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  421. Assert.AreEqual((double)o, doubleResultTwo);
  422. }
  423. #endregion
  424. }
  425. private IEnumerable<int> MultiplyArray(IReadOnlyCollection<int> first, IReadOnlyCollection<int> second)
  426. {
  427. if(first.Count != second.Count)
  428. throw new ArgumentException("Arrays should be of equal size!");
  429. var firstEnumerator = first.GetEnumerator();
  430. var secondEnumerator = second.GetEnumerator();
  431. var result = new List<int>();
  432. while (firstEnumerator.MoveNext())
  433. {
  434. secondEnumerator.MoveNext();
  435. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  436. }
  437. firstEnumerator.Dispose();
  438. secondEnumerator.Dispose();
  439. return result;
  440. }
  441. private IEnumerable<float> MultiplyArray(IReadOnlyCollection<float> first, IReadOnlyCollection<float> second)
  442. {
  443. if(first.Count != second.Count)
  444. throw new ArgumentException("Arrays should be of equal size!");
  445. var firstEnumerator = first.GetEnumerator();
  446. var secondEnumerator = second.GetEnumerator();
  447. var result = new List<float>();
  448. while (firstEnumerator.MoveNext())
  449. {
  450. secondEnumerator.MoveNext();
  451. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  452. }
  453. firstEnumerator.Dispose();
  454. secondEnumerator.Dispose();
  455. return result;
  456. }
  457. private IEnumerable<double> MultiplyArray(IReadOnlyCollection<double> first, IReadOnlyCollection<double> second)
  458. {
  459. if(first.Count != second.Count)
  460. throw new ArgumentException("Arrays should be of equal size!");
  461. var firstEnumerator = first.GetEnumerator();
  462. var secondEnumerator = second.GetEnumerator();
  463. var result = new List<double>();
  464. while (firstEnumerator.MoveNext())
  465. {
  466. secondEnumerator.MoveNext();
  467. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  468. }
  469. firstEnumerator.Dispose();
  470. secondEnumerator.Dispose();
  471. return result;
  472. }
  473. [TestMethod]
  474. public void mulOpTests()
  475. {
  476. const int rows = 2; // to avoid broadcasting effect
  477. const int cols = 10;
  478. #region intTest
  479. const int firstIntVal = 2;
  480. const int secondIntVal = 3;
  481. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  482. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  483. var intResult = MultiplyArray(firstIntFeed, secondIntFeed).Sum();
  484. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  485. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  486. var c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  487. using (var sess = tf.Session())
  488. {
  489. var o = sess.run(c,
  490. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  491. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  492. Assert.AreEqual((int)o, intResult);
  493. }
  494. // Testing `operator *(Tensor x, Tensor y)
  495. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  496. using (var sess = tf.Session())
  497. {
  498. var o = sess.run(c,
  499. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  500. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  501. Assert.AreEqual((int)o, intResult);
  502. }
  503. // Testing `operator *(Tensor x, int y)
  504. c = tf.reduce_sum(tf.reduce_sum(a * secondIntVal, 1));
  505. using (var sess = tf.Session())
  506. {
  507. var o = sess.run(c,
  508. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  509. Assert.AreEqual((int)o, intResult);
  510. }
  511. // Testing `operator *(int x, Tensor y)
  512. c = tf.reduce_sum(tf.reduce_sum(firstIntVal * b, 1));
  513. using (var sess = tf.Session())
  514. {
  515. var o = sess.run(c,
  516. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  517. Assert.AreEqual((int)o, intResult);
  518. }
  519. #endregion
  520. #region floatTest
  521. const float firstFloatVal = 2.0f;
  522. const float secondFloatVal = 3.0f;
  523. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  524. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  525. var floatResult = MultiplyArray(firstFloatFeed, secondFloatFeed).Sum();
  526. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  527. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  528. c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  529. using (var sess = tf.Session())
  530. {
  531. var o = sess.run(c,
  532. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  533. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  534. Assert.AreEqual((float)o, floatResult);
  535. }
  536. // Testing `operator *(Tensor x, Tensor y)
  537. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  538. using (var sess = tf.Session())
  539. {
  540. var o = sess.run(c,
  541. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  542. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  543. Assert.AreEqual((float)o, floatResult);
  544. }
  545. // Testing `operator *(Tensor x, float y)
  546. c = tf.reduce_sum(tf.reduce_sum(a * secondFloatVal, 1));
  547. using (var sess = tf.Session())
  548. {
  549. var o = sess.run(c,
  550. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  551. Assert.AreEqual((float)o, floatResult);
  552. }
  553. // Testing `operator *(float x, Tensor y)
  554. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal * b, 1));
  555. using (var sess = tf.Session())
  556. {
  557. var o = sess.run(c,
  558. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  559. Assert.AreEqual((float)o, floatResult);
  560. }
  561. #endregion
  562. #region doubleTest
  563. const double firstDoubleVal = 2.0;
  564. const double secondDoubleVal = 3.0;
  565. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  566. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  567. var doubleResult = MultiplyArray(firstDoubleFeed, secondDoubleFeed).Sum();
  568. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  569. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  570. c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  571. using (var sess = tf.Session())
  572. {
  573. var o = sess.run(c,
  574. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  575. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  576. Assert.AreEqual((double)o, doubleResult);
  577. }
  578. // Testing `operator *(Tensor x, Tensor y)
  579. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  580. using (var sess = tf.Session())
  581. {
  582. var o = sess.run(c,
  583. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  584. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  585. Assert.AreEqual((double)o, doubleResult);
  586. }
  587. // Testing `operator *(Tensor x, double y)
  588. c = tf.reduce_sum(tf.reduce_sum(a * secondFloatVal, 1));
  589. using (var sess = tf.Session())
  590. {
  591. var o = sess.run(c,
  592. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  593. Assert.AreEqual((double)o, doubleResult);
  594. }
  595. // Testing `operator *(double x, Tensor y)
  596. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal * b, 1));
  597. using (var sess = tf.Session())
  598. {
  599. var o = sess.run(c,
  600. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  601. Assert.AreEqual((double) o, doubleResult);
  602. }
  603. #endregion
  604. }
  605. [TestMethod]
  606. public void divOpTests()
  607. {
  608. const int rows = 2; // to avoid broadcasting effect
  609. const int cols = 10;
  610. #region intTest
  611. const int firstIntVal = 6;
  612. const int secondIntVal = 3;
  613. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  614. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  615. var intResult = (int)(firstIntFeed.Sum() / (float)secondIntVal);
  616. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  617. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  618. var c = tf.reduce_sum(tf.reduce_sum(gen_math_ops.floor_div(a, b), 1));
  619. using (var sess = tf.Session())
  620. {
  621. var o = sess.run(c,
  622. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  623. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  624. Assert.AreEqual((int)o, intResult);
  625. }
  626. // Testing `operator /(Tensor x, Tensor y)
  627. c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
  628. using (var sess = tf.Session())
  629. {
  630. var o = sess.run(c,
  631. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  632. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  633. Assert.AreEqual((int)o, intResult);
  634. }
  635. // Testing `operator /(Tensor x, int y)
  636. c = tf.reduce_sum(tf.reduce_sum(a / secondIntVal, 1));
  637. using (var sess = tf.Session())
  638. {
  639. var o = sess.run(c,
  640. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  641. Assert.AreEqual((int)o, intResult);
  642. }
  643. // Testing `operator /(int x, Tensor y)
  644. c = tf.reduce_sum(tf.reduce_sum(firstIntVal / b, 1));
  645. using (var sess = tf.Session())
  646. {
  647. var o = sess.run(c,
  648. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  649. Assert.AreEqual((int)o, intResult);
  650. }
  651. #endregion
  652. #region floatTest
  653. const float firstFloatVal = 6.0f;
  654. const float secondFloatVal = 3.0f;
  655. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  656. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  657. var floatResult = MultiplyArray(firstFloatFeed, secondFloatFeed.Select(x => 1/x).ToArray()).Sum();
  658. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  659. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  660. c = tf.reduce_sum(tf.reduce_sum(tf.divide(a, b), 1));
  661. using (var sess = tf.Session())
  662. {
  663. var o = sess.run(c,
  664. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  665. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  666. Assert.AreEqual((float)o, floatResult);
  667. }
  668. // Testing `operator /(Tensor x, Tensor y)
  669. c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
  670. using (var sess = tf.Session())
  671. {
  672. var o = sess.run(c,
  673. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  674. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  675. Assert.AreEqual((float)o, floatResult);
  676. }
  677. // Testing `operator /(Tensor x, float y)
  678. c = tf.reduce_sum(tf.reduce_sum(a / secondFloatVal, 1));
  679. using (var sess = tf.Session())
  680. {
  681. var o = sess.run(c,
  682. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  683. Assert.AreEqual((float)o, floatResult);
  684. }
  685. // Testing `operator /(float x, Tensor y)
  686. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal / b, 1));
  687. using (var sess = tf.Session())
  688. {
  689. var o = sess.run(c,
  690. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  691. Assert.AreEqual((float)o, floatResult);
  692. }
  693. #endregion
  694. #region doubleTest
  695. const double firstDoubleVal = 6.0;
  696. const double secondDoubleVal = 3.0;
  697. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  698. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  699. var doubleResult = MultiplyArray(firstDoubleFeed, secondDoubleFeed.Select(x => 1/x).ToArray()).Sum();
  700. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  701. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  702. c = tf.reduce_sum(tf.reduce_sum(tf.divide(a, b), 1));
  703. using (var sess = tf.Session())
  704. {
  705. var o = sess.run(c,
  706. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  707. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  708. Assert.AreEqual((double)o, doubleResult);
  709. }
  710. // Testing `operator /(Tensor x, Tensor y)
  711. c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
  712. using (var sess = tf.Session())
  713. {
  714. var o = sess.run(c,
  715. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  716. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  717. Assert.AreEqual((double)o, doubleResult);
  718. }
  719. // Testing `operator /(Tensor x, double y)
  720. c = tf.reduce_sum(tf.reduce_sum(a / secondFloatVal, 1));
  721. using (var sess = tf.Session())
  722. {
  723. var o = sess.run(c,
  724. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  725. Assert.AreEqual((double)o, doubleResult);
  726. }
  727. // Testing `operator /(double x, Tensor y)
  728. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal / b, 1));
  729. using (var sess = tf.Session())
  730. {
  731. var o = sess.run(c,
  732. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  733. Assert.AreEqual((double)o, doubleResult);
  734. }
  735. #endregion
  736. }
  737. [TestMethod]
  738. public void greaterThanOpTests()
  739. {
  740. const int rows = 2; // to avoid broadcasting effect
  741. const int cols = 10;
  742. #region intTest
  743. const int intThreshold = 10;
  744. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  745. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  746. var intResult = firstIntFeed.Count(elem => elem > intThreshold);
  747. var intResultTwo = firstIntFeed.Count(elem => elem < intThreshold);
  748. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  749. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  750. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
  751. using (var sess = tf.Session())
  752. {
  753. var o = sess.run(c,
  754. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  755. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  756. Assert.AreEqual((int)o, intResult);
  757. }
  758. // Testing `operator >(Tensor x, Tensor y)
  759. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
  760. using (var sess = tf.Session())
  761. {
  762. var o = sess.run(c,
  763. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  764. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  765. Assert.AreEqual((int)o, intResult);
  766. }
  767. // Testing `operator >(Tensor x, int y)
  768. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > intThreshold, tf.int32), 1));
  769. using (var sess = tf.Session())
  770. {
  771. var o = sess.run(c,
  772. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  773. Assert.AreEqual((int)o, intResult);
  774. }
  775. // Testing `operator >(int x, Tensor y)
  776. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold > a, 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. Assert.AreEqual((int)o, intResultTwo);
  782. }
  783. #endregion
  784. #region floatTest
  785. const float floatThreshold = 10.0f;
  786. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  787. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  788. var floatResult = firstFloatFeed.Count(elem => elem > floatThreshold);
  789. var floatResultTwo = firstFloatFeed.Count(elem => elem < floatThreshold);
  790. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  791. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  792. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
  793. using (var sess = tf.Session())
  794. {
  795. var o = sess.run(c,
  796. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  797. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  798. Assert.AreEqual((int)o, floatResult);
  799. }
  800. // Testing `operator >(Tensor x, Tensor y)
  801. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
  802. using (var sess = tf.Session())
  803. {
  804. var o = sess.run(c,
  805. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  806. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  807. Assert.AreEqual((int)o, floatResult);
  808. }
  809. // Testing `operator >(Tensor x, float y)
  810. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > floatThreshold, tf.int32), 1));
  811. using (var sess = tf.Session())
  812. {
  813. var o = sess.run(c,
  814. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  815. Assert.AreEqual((int)o, floatResult);
  816. }
  817. // Testing `operator >(float x, Tensor y)
  818. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold > a, 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. Assert.AreEqual((int)o, floatResultTwo);
  824. }
  825. #endregion
  826. #region doubleTest
  827. const double doubleThreshold = 10.0;
  828. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  829. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  830. var doubleResult = firstDoubleFeed.Count(elem => elem > doubleThreshold);
  831. var doubleResultTwo = firstDoubleFeed.Count(elem => elem < doubleThreshold);
  832. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  833. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  834. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
  835. using (var sess = tf.Session())
  836. {
  837. var o = sess.run(c,
  838. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  839. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  840. Assert.AreEqual((int)o, doubleResult);
  841. }
  842. // Testing `operator >(Tensor x, Tensor y)
  843. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
  844. using (var sess = tf.Session())
  845. {
  846. var o = sess.run(c,
  847. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  848. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  849. Assert.AreEqual((int)o, doubleResult);
  850. }
  851. // Testing `operator >(Tensor x, double y)
  852. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > doubleThreshold, tf.int32), 1));
  853. using (var sess = tf.Session())
  854. {
  855. var o = sess.run(c,
  856. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  857. Assert.AreEqual((int)o, doubleResult);
  858. }
  859. // Testing `operator >(double x, Tensor y)
  860. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold > a, 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. Assert.AreEqual((int)o, doubleResultTwo);
  866. }
  867. #endregion
  868. }
  869. [TestMethod]
  870. public void lessThanOpTests()
  871. {
  872. const int rows = 2; // to avoid broadcasting effect
  873. const int cols = 10;
  874. #region intTest
  875. const int intThreshold = 10;
  876. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  877. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  878. var intResult = firstIntFeed.Count(elem => elem < intThreshold);
  879. var intResultTwo = firstIntFeed.Count(elem => elem > intThreshold);
  880. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  881. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  882. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
  883. using (var sess = tf.Session())
  884. {
  885. var o = sess.run(c,
  886. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  887. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  888. Assert.AreEqual((int)o, intResult);
  889. }
  890. // Testing `operator <(Tensor x, Tensor y)
  891. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
  892. using (var sess = tf.Session())
  893. {
  894. var o = sess.run(c,
  895. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  896. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  897. Assert.AreEqual((int)o, intResult);
  898. }
  899. // Testing `operator <(Tensor x, int y)
  900. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < intThreshold, tf.int32), 1));
  901. using (var sess = tf.Session())
  902. {
  903. var o = sess.run(c,
  904. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  905. Assert.AreEqual((int)o, intResult);
  906. }
  907. // Testing `operator <(int x, Tensor y)
  908. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold < a, 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. Assert.AreEqual((int)o, intResultTwo);
  914. }
  915. #endregion
  916. #region floatTest
  917. const float floatThreshold = 10.0f;
  918. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  919. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  920. var floatResult = firstFloatFeed.Count(elem => elem < floatThreshold);
  921. var floatResultTwo = firstFloatFeed.Count(elem => elem > floatThreshold);
  922. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  923. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  924. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
  925. using (var sess = tf.Session())
  926. {
  927. var o = sess.run(c,
  928. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  929. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  930. Assert.AreEqual((int)o, floatResult);
  931. }
  932. // Testing `operator <(Tensor x, Tensor y)
  933. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
  934. using (var sess = tf.Session())
  935. {
  936. var o = sess.run(c,
  937. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  938. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  939. Assert.AreEqual((int)o, floatResult);
  940. }
  941. // Testing `operator <(Tensor x, float y)
  942. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < floatThreshold, tf.int32), 1));
  943. using (var sess = tf.Session())
  944. {
  945. var o = sess.run(c,
  946. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  947. Assert.AreEqual((int)o, floatResult);
  948. }
  949. // Testing `operator <(float x, Tensor y)
  950. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold < a, 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. Assert.AreEqual((int)o, floatResultTwo);
  956. }
  957. #endregion
  958. #region doubleTest
  959. const double doubleThreshold = 10.0;
  960. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  961. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  962. var doubleResult = firstDoubleFeed.Count(elem => elem < doubleThreshold);
  963. var doubleResultTwo = firstDoubleFeed.Count(elem => elem > doubleThreshold);
  964. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  965. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  966. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
  967. using (var sess = tf.Session())
  968. {
  969. var o = sess.run(c,
  970. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  971. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  972. Assert.AreEqual((int)o, doubleResult);
  973. }
  974. // Testing `operator <(Tensor x, Tensor y)
  975. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
  976. using (var sess = tf.Session())
  977. {
  978. var o = sess.run(c,
  979. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  980. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  981. Assert.AreEqual((int)o, doubleResult);
  982. }
  983. // Testing `operator <(Tensor x, double y)
  984. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < doubleThreshold, tf.int32), 1));
  985. using (var sess = tf.Session())
  986. {
  987. var o = sess.run(c,
  988. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  989. Assert.AreEqual((int)o, doubleResult);
  990. }
  991. // Testing `operator <(double x, Tensor y)
  992. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold < a, 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. Assert.AreEqual((int)o, doubleResultTwo);
  998. }
  999. #endregion
  1000. }
  1001. [TestMethod]
  1002. public void greaterOrEqualThanOpTests()
  1003. {
  1004. const int rows = 2; // to avoid broadcasting effect
  1005. const int cols = 10;
  1006. #region intTest
  1007. const int intThreshold = 10;
  1008. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  1009. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  1010. var intResult = firstIntFeed.Count(elem => elem >= intThreshold);
  1011. var intResultTwo = firstIntFeed.Count(elem => elem <= intThreshold);
  1012. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  1013. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  1014. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
  1015. using (var sess = tf.Session())
  1016. {
  1017. var o = sess.run(c,
  1018. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  1019. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  1020. Assert.AreEqual((int)o, intResult);
  1021. }
  1022. // Testing `operator >=(Tensor x, Tensor y)
  1023. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= b, tf.int32), 1));
  1024. using (var sess = tf.Session())
  1025. {
  1026. var o = sess.run(c,
  1027. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  1028. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  1029. Assert.AreEqual((int)o, intResult);
  1030. }
  1031. // Testing `operator >=(Tensor x, int y)
  1032. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= intThreshold, tf.int32), 1));
  1033. using (var sess = tf.Session())
  1034. {
  1035. var o = sess.run(c,
  1036. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  1037. Assert.AreEqual((int)o, intResult);
  1038. }
  1039. // Testing `operator >=(int x, Tensor y)
  1040. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold >= a, 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. Assert.AreEqual((int)o, intResultTwo);
  1046. }
  1047. #endregion
  1048. #region floatTest
  1049. const float floatThreshold = 10.0f;
  1050. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  1051. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  1052. var floatResult = firstFloatFeed.Count(elem => elem >= floatThreshold);
  1053. var floatResultTwo = firstFloatFeed.Count(elem => elem <= floatThreshold);
  1054. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  1055. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  1056. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
  1057. using (var sess = tf.Session())
  1058. {
  1059. var o = sess.run(c,
  1060. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  1061. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  1062. Assert.AreEqual((int)o, floatResult);
  1063. }
  1064. // Testing `operator >=(Tensor x, Tensor y)
  1065. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= b, tf.int32), 1));
  1066. using (var sess = tf.Session())
  1067. {
  1068. var o = sess.run(c,
  1069. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  1070. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  1071. Assert.AreEqual((int)o, floatResult);
  1072. }
  1073. // Testing `operator >=(Tensor x, float y)
  1074. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= floatThreshold, tf.int32), 1));
  1075. using (var sess = tf.Session())
  1076. {
  1077. var o = sess.run(c,
  1078. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  1079. Assert.AreEqual((int)o, floatResult);
  1080. }
  1081. // Testing `operator >=(float x, Tensor y)
  1082. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold >= a, 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. Assert.AreEqual((int)o, floatResultTwo);
  1088. }
  1089. #endregion
  1090. #region doubleTest
  1091. const double doubleThreshold = 10.0;
  1092. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  1093. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  1094. var doubleResult = firstDoubleFeed.Count(elem => elem >= doubleThreshold);
  1095. var doubleResultTwo = firstDoubleFeed.Count(elem => elem <= doubleThreshold);
  1096. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  1097. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  1098. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater_equal(a, b), tf.int32), 1));
  1099. using (var sess = tf.Session())
  1100. {
  1101. var o = sess.run(c,
  1102. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  1103. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  1104. Assert.AreEqual((int)o, doubleResult);
  1105. }
  1106. // Testing `operator >=(Tensor x, Tensor y)
  1107. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= b, tf.int32), 1));
  1108. using (var sess = tf.Session())
  1109. {
  1110. var o = sess.run(c,
  1111. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  1112. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  1113. Assert.AreEqual((int)o, doubleResult);
  1114. }
  1115. // Testing `operator >=(Tensor x, double y)
  1116. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a >= doubleThreshold, tf.int32), 1));
  1117. using (var sess = tf.Session())
  1118. {
  1119. var o = sess.run(c,
  1120. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  1121. Assert.AreEqual((int)o, doubleResult);
  1122. }
  1123. // Testing `operator >=(double x, Tensor y)
  1124. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold >= a, 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. Assert.AreEqual((int)o, doubleResultTwo);
  1130. }
  1131. #endregion
  1132. }
  1133. [TestMethod]
  1134. public void lessOrEqualThanOpTests()
  1135. {
  1136. const int rows = 2; // to avoid broadcasting effect
  1137. const int cols = 10;
  1138. #region intTest
  1139. const int intThreshold = 10;
  1140. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  1141. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  1142. var intResult = firstIntFeed.Count(elem => elem <= intThreshold);
  1143. var intResultTwo = firstIntFeed.Count(elem => elem >= intThreshold);
  1144. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  1145. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  1146. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
  1147. using (var sess = tf.Session())
  1148. {
  1149. var o = sess.run(c,
  1150. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  1151. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  1152. Assert.AreEqual((int)o, intResult);
  1153. }
  1154. // Testing `operator <=(Tensor x, Tensor y)
  1155. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= b, tf.int32), 1));
  1156. using (var sess = tf.Session())
  1157. {
  1158. var o = sess.run(c,
  1159. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  1160. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  1161. Assert.AreEqual((int)o, intResult);
  1162. }
  1163. // Testing `operator <=(Tensor x, int y)
  1164. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= intThreshold, tf.int32), 1));
  1165. using (var sess = tf.Session())
  1166. {
  1167. var o = sess.run(c,
  1168. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  1169. Assert.AreEqual((int)o, intResult);
  1170. }
  1171. // Testing `operator <=(int x, Tensor y)
  1172. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold <= a, 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. Assert.AreEqual((int)o, intResultTwo);
  1178. }
  1179. #endregion
  1180. #region floatTest
  1181. const float floatThreshold = 10.0f;
  1182. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  1183. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  1184. var floatResult = firstFloatFeed.Count(elem => elem <= floatThreshold);
  1185. var floatResultTwo = firstFloatFeed.Count(elem => elem >= floatThreshold);
  1186. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  1187. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  1188. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
  1189. using (var sess = tf.Session())
  1190. {
  1191. var o = sess.run(c,
  1192. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  1193. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  1194. Assert.AreEqual((int)o, floatResult);
  1195. }
  1196. // Testing `operator <=(Tensor x, Tensor y)
  1197. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= b, tf.int32), 1));
  1198. using (var sess = tf.Session())
  1199. {
  1200. var o = sess.run(c,
  1201. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  1202. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  1203. Assert.AreEqual((int)o, floatResult);
  1204. }
  1205. // Testing `operator <=(Tensor x, float y)
  1206. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= floatThreshold, tf.int32), 1));
  1207. using (var sess = tf.Session())
  1208. {
  1209. var o = sess.run(c,
  1210. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  1211. Assert.AreEqual((int)o, floatResult);
  1212. }
  1213. // Testing `operator <=(float x, Tensor y)
  1214. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold <= a, 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. Assert.AreEqual((int)o, floatResultTwo);
  1220. }
  1221. #endregion
  1222. #region doubleTest
  1223. const double doubleThreshold = 10.0;
  1224. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  1225. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  1226. var doubleResult = firstDoubleFeed.Count(elem => elem <= doubleThreshold);
  1227. var doubleResultTwo = firstDoubleFeed.Count(elem => elem >= doubleThreshold);
  1228. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  1229. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  1230. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less_equal(a, b), tf.int32), 1));
  1231. using (var sess = tf.Session())
  1232. {
  1233. var o = sess.run(c,
  1234. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  1235. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  1236. Assert.AreEqual((int)o, doubleResult);
  1237. }
  1238. // Testing `operator <=(Tensor x, Tensor y)
  1239. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= b, tf.int32), 1));
  1240. using (var sess = tf.Session())
  1241. {
  1242. var o = sess.run(c,
  1243. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  1244. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  1245. Assert.AreEqual((int)o, doubleResult);
  1246. }
  1247. // Testing `operator <=(Tensor x, double y)
  1248. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a <= doubleThreshold, tf.int32), 1));
  1249. using (var sess = tf.Session())
  1250. {
  1251. var o = sess.run(c,
  1252. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  1253. Assert.AreEqual((int)o, doubleResult);
  1254. }
  1255. // Testing `operator <=(double x, Tensor y)
  1256. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold <= a, 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. Assert.AreEqual((int)o, doubleResultTwo);
  1262. }
  1263. #endregion
  1264. }
  1265. }
  1266. }