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

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