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