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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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 addOpTests()
  115. {
  116. const int rows = 2; // to avoid broadcasting effect
  117. const int cols = 10;
  118. #region intTest
  119. const int firstIntVal = 2;
  120. const int secondIntVal = 3;
  121. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  122. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  123. var intResult = firstIntFeed.Sum() + secondIntFeed.Sum();
  124. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  125. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  126. var c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  127. using (var sess = tf.Session())
  128. {
  129. var o = sess.run(c,
  130. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  131. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  132. Assert.AreEqual((int)o, intResult);
  133. }
  134. // Testing `operator +(Tensor x, Tensor y)`
  135. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  136. using (var sess = tf.Session())
  137. {
  138. var o = sess.run(c,
  139. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  140. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  141. Assert.AreEqual((int)o, intResult);
  142. }
  143. // Testing `operator +(Tensor x, int y)`
  144. c = tf.reduce_sum(tf.reduce_sum(a + secondIntVal, 1));
  145. using (var sess = tf.Session())
  146. {
  147. var o = sess.run(c,
  148. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  149. Assert.AreEqual((int)o, intResult);
  150. }
  151. // Testing `operator +(int x, Tensor y)`
  152. c = tf.reduce_sum(tf.reduce_sum(secondIntVal + a, 1));
  153. using (var sess = tf.Session())
  154. {
  155. var o = sess.run(c,
  156. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  157. Assert.AreEqual((int)o, intResult);
  158. }
  159. #endregion
  160. #region floatTest
  161. const float firstFloatVal = 2.0f;
  162. const float secondFloatVal = 3.0f;
  163. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  164. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  165. var floatResult = firstFloatFeed.Sum() + secondFloatFeed.Sum();
  166. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  167. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  168. c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  169. using (var sess = tf.Session())
  170. {
  171. var o = sess.run(c,
  172. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  173. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  174. Assert.AreEqual((float)o, floatResult);
  175. }
  176. // Testing `operator +(Tensor x, Tensor y)
  177. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  178. using (var sess = tf.Session())
  179. {
  180. var o = sess.run(c,
  181. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  182. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  183. Assert.AreEqual((float)o, floatResult);
  184. }
  185. // Testing `operator +(Tensor x, float y)
  186. c = tf.reduce_sum(tf.reduce_sum(a + secondFloatVal, 1));
  187. using (var sess = tf.Session())
  188. {
  189. var o = sess.run(c,
  190. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  191. Assert.AreEqual((float)o, floatResult);
  192. }
  193. // Testing `operator +(float x, Tensor y)
  194. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal + a, 1));
  195. using (var sess = tf.Session())
  196. {
  197. var o = sess.run(c,
  198. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  199. Assert.AreEqual((float)o, floatResult);
  200. }
  201. #endregion
  202. #region doubleTest
  203. const double firstDoubleVal = 2.0;
  204. const double secondDoubleVal = 3.0;
  205. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  206. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  207. var doubleResult = firstDoubleFeed.Sum() + secondDoubleFeed.Sum();
  208. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  209. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  210. c = tf.reduce_sum(tf.reduce_sum(tf.add(a, b), 1));
  211. using (var sess = tf.Session())
  212. {
  213. var o = sess.run(c,
  214. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  215. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  216. Assert.AreEqual((double)o, doubleResult);
  217. }
  218. // Testing `operator +(Tensor x, Tensor y)
  219. c = tf.reduce_sum(tf.reduce_sum(a + b, 1));
  220. using (var sess = tf.Session())
  221. {
  222. var o = sess.run(c,
  223. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  224. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  225. Assert.AreEqual((double)o, doubleResult);
  226. }
  227. // Testing `operator +(Tensor x, double y)
  228. c = tf.reduce_sum(tf.reduce_sum(a + secondFloatVal, 1));
  229. using (var sess = tf.Session())
  230. {
  231. var o = sess.run(c,
  232. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  233. Assert.AreEqual((double)o, doubleResult);
  234. }
  235. // Testing `operator +(double x, Tensor y)
  236. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal + a, 1));
  237. using (var sess = tf.Session())
  238. {
  239. var o = sess.run(c,
  240. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  241. Assert.AreEqual((double)o, doubleResult);
  242. }
  243. #endregion
  244. }
  245. [TestMethod]
  246. public void subOpTests()
  247. {
  248. const int rows = 2; // to avoid broadcasting effect
  249. const int cols = 10;
  250. #region intTest
  251. const int firstIntVal = -2;
  252. const int secondIntVal = 3;
  253. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  254. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  255. var intResult = firstIntFeed.Sum() - secondIntFeed.Sum();
  256. var intResultTwo = -firstIntFeed.Sum();
  257. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  258. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  259. var c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  260. using (var sess = tf.Session())
  261. {
  262. var o = sess.run(c,
  263. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  264. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  265. Assert.AreEqual((int)o, intResult);
  266. }
  267. // Testing `operator -(Tensor x, Tensor y)
  268. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  269. using (var sess = tf.Session())
  270. {
  271. var o = sess.run(c,
  272. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  273. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  274. Assert.AreEqual((int)o, intResult);
  275. }
  276. // Testing `operator -(Tensor x, int y)
  277. c = tf.reduce_sum(tf.reduce_sum(a - secondIntVal, 1));
  278. using (var sess = tf.Session())
  279. {
  280. var o = sess.run(c,
  281. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  282. Assert.AreEqual((int)o, intResult);
  283. }
  284. // Testing `operator -(int x, Tensor y)
  285. c = tf.reduce_sum(tf.reduce_sum(secondIntVal - a, 1));
  286. using (var sess = tf.Session())
  287. {
  288. var o = sess.run(c,
  289. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  290. Assert.AreEqual((int)o, Math.Abs(intResult));
  291. }
  292. // Testing `operator -(Tensor x)
  293. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  294. using (var sess = tf.Session())
  295. {
  296. var o = sess.run(c,
  297. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  298. Assert.AreEqual((int)o, intResultTwo);
  299. }
  300. #endregion
  301. #region floatTest
  302. const float firstFloatVal = -2.0f;
  303. const float secondFloatVal = 3.0f;
  304. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  305. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  306. var floatResult = firstFloatFeed.Sum() - secondFloatFeed.Sum();
  307. var floatResultTwo = -firstFloatFeed.Sum();
  308. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  309. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  310. c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  311. using (var sess = tf.Session())
  312. {
  313. var o = sess.run(c,
  314. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  315. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  316. Assert.AreEqual((float)o, floatResult);
  317. }
  318. // Testing `operator -(Tensor x, Tensor y)
  319. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  320. using (var sess = tf.Session())
  321. {
  322. var o = sess.run(c,
  323. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  324. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  325. Assert.AreEqual((float)o, floatResult);
  326. }
  327. // Testing `operator -(Tensor x, float y)
  328. c = tf.reduce_sum(tf.reduce_sum(a - secondFloatVal, 1));
  329. using (var sess = tf.Session())
  330. {
  331. var o = sess.run(c,
  332. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  333. Assert.AreEqual((float)o, floatResult);
  334. }
  335. // Testing `operator -(float x, Tensor y)
  336. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal - a, 1));
  337. using (var sess = tf.Session())
  338. {
  339. var o = sess.run(c,
  340. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  341. Assert.AreEqual((float)o, Math.Abs(floatResult));
  342. }
  343. // Testing `operator -(Tensor x)
  344. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  345. using (var sess = tf.Session())
  346. {
  347. var o = sess.run(c,
  348. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  349. Assert.AreEqual((float)o, floatResultTwo);
  350. }
  351. #endregion
  352. #region doubleTest
  353. const double firstDoubleVal = -2.0;
  354. const double secondDoubleVal = 3.0;
  355. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  356. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  357. var doubleResult = firstDoubleFeed.Sum() - secondDoubleFeed.Sum();
  358. var doubleResultTwo = -firstDoubleFeed.Sum();
  359. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  360. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  361. c = tf.reduce_sum(tf.reduce_sum(tf.sub(a, b), 1));
  362. using (var sess = tf.Session())
  363. {
  364. var o = sess.run(c,
  365. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  366. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  367. Assert.AreEqual((double)o, doubleResult);
  368. }
  369. // Testing `operator -(Tensor x, Tensor y)
  370. c = tf.reduce_sum(tf.reduce_sum(a - b, 1));
  371. using (var sess = tf.Session())
  372. {
  373. var o = sess.run(c,
  374. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  375. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  376. Assert.AreEqual((double)o, doubleResult);
  377. }
  378. // Testing `operator -(Tensor x, double y)
  379. c = tf.reduce_sum(tf.reduce_sum(a - secondFloatVal, 1));
  380. using (var sess = tf.Session())
  381. {
  382. var o = sess.run(c,
  383. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  384. Assert.AreEqual((double)o, doubleResult);
  385. }
  386. // Testing `operator -(double x, Tensor y)
  387. c = tf.reduce_sum(tf.reduce_sum(secondFloatVal - a, 1));
  388. using (var sess = tf.Session())
  389. {
  390. var o = sess.run(c,
  391. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  392. Assert.AreEqual((double)o, Math.Abs(doubleResult));
  393. }
  394. // Testing `operator -(Tensor x)
  395. c = tf.reduce_sum(tf.reduce_sum(-a, 1));
  396. using (var sess = tf.Session())
  397. {
  398. var o = sess.run(c,
  399. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  400. Assert.AreEqual((double)o, doubleResultTwo);
  401. }
  402. #endregion
  403. }
  404. private IEnumerable<int> MultiplyArray(IReadOnlyCollection<int> first, IReadOnlyCollection<int> second)
  405. {
  406. if(first.Count != second.Count)
  407. throw new ArgumentException("Arrays should be of equal size!");
  408. var firstEnumerator = first.GetEnumerator();
  409. var secondEnumerator = second.GetEnumerator();
  410. var result = new List<int>();
  411. while (firstEnumerator.MoveNext())
  412. {
  413. secondEnumerator.MoveNext();
  414. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  415. }
  416. firstEnumerator.Dispose();
  417. secondEnumerator.Dispose();
  418. return result;
  419. }
  420. private IEnumerable<float> MultiplyArray(IReadOnlyCollection<float> first, IReadOnlyCollection<float> second)
  421. {
  422. if(first.Count != second.Count)
  423. throw new ArgumentException("Arrays should be of equal size!");
  424. var firstEnumerator = first.GetEnumerator();
  425. var secondEnumerator = second.GetEnumerator();
  426. var result = new List<float>();
  427. while (firstEnumerator.MoveNext())
  428. {
  429. secondEnumerator.MoveNext();
  430. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  431. }
  432. firstEnumerator.Dispose();
  433. secondEnumerator.Dispose();
  434. return result;
  435. }
  436. private IEnumerable<double> MultiplyArray(IReadOnlyCollection<double> first, IReadOnlyCollection<double> second)
  437. {
  438. if(first.Count != second.Count)
  439. throw new ArgumentException("Arrays should be of equal size!");
  440. var firstEnumerator = first.GetEnumerator();
  441. var secondEnumerator = second.GetEnumerator();
  442. var result = new List<double>();
  443. while (firstEnumerator.MoveNext())
  444. {
  445. secondEnumerator.MoveNext();
  446. result.Add(firstEnumerator.Current * secondEnumerator.Current);
  447. }
  448. firstEnumerator.Dispose();
  449. secondEnumerator.Dispose();
  450. return result;
  451. }
  452. [TestMethod]
  453. public void mulOpTests()
  454. {
  455. const int rows = 2; // to avoid broadcasting effect
  456. const int cols = 10;
  457. #region intTest
  458. const int firstIntVal = 2;
  459. const int secondIntVal = 3;
  460. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  461. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  462. var intResult = MultiplyArray(firstIntFeed, secondIntFeed).Sum();
  463. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  464. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  465. var c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  466. using (var sess = tf.Session())
  467. {
  468. var o = sess.run(c,
  469. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  470. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  471. Assert.AreEqual((int)o, intResult);
  472. }
  473. // Testing `operator *(Tensor x, Tensor y)
  474. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  475. using (var sess = tf.Session())
  476. {
  477. var o = sess.run(c,
  478. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  479. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  480. Assert.AreEqual((int)o, intResult);
  481. }
  482. // Testing `operator *(Tensor x, int y)
  483. c = tf.reduce_sum(tf.reduce_sum(a * secondIntVal, 1));
  484. using (var sess = tf.Session())
  485. {
  486. var o = sess.run(c,
  487. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  488. Assert.AreEqual((int)o, intResult);
  489. }
  490. // Testing `operator *(int x, Tensor y)
  491. c = tf.reduce_sum(tf.reduce_sum(firstIntVal * b, 1));
  492. using (var sess = tf.Session())
  493. {
  494. var o = sess.run(c,
  495. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  496. Assert.AreEqual((int)o, intResult);
  497. }
  498. #endregion
  499. #region floatTest
  500. const float firstFloatVal = 2.0f;
  501. const float secondFloatVal = 3.0f;
  502. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  503. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  504. var floatResult = MultiplyArray(firstFloatFeed, secondFloatFeed).Sum();
  505. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  506. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  507. c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  508. using (var sess = tf.Session())
  509. {
  510. var o = sess.run(c,
  511. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  512. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  513. Assert.AreEqual((float)o, floatResult);
  514. }
  515. // Testing `operator *(Tensor x, Tensor y)
  516. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  517. using (var sess = tf.Session())
  518. {
  519. var o = sess.run(c,
  520. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  521. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  522. Assert.AreEqual((float)o, floatResult);
  523. }
  524. // Testing `operator *(Tensor x, float y)
  525. c = tf.reduce_sum(tf.reduce_sum(a * secondFloatVal, 1));
  526. using (var sess = tf.Session())
  527. {
  528. var o = sess.run(c,
  529. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  530. Assert.AreEqual((float)o, floatResult);
  531. }
  532. // Testing `operator *(float x, Tensor y)
  533. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal * b, 1));
  534. using (var sess = tf.Session())
  535. {
  536. var o = sess.run(c,
  537. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  538. Assert.AreEqual((float)o, floatResult);
  539. }
  540. #endregion
  541. #region doubleTest
  542. const double firstDoubleVal = 2.0;
  543. const double secondDoubleVal = 3.0;
  544. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  545. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  546. var doubleResult = MultiplyArray(firstDoubleFeed, secondDoubleFeed).Sum();
  547. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  548. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  549. c = tf.reduce_sum(tf.reduce_sum(tf.multiply(a, b), 1));
  550. using (var sess = tf.Session())
  551. {
  552. var o = sess.run(c,
  553. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  554. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  555. Assert.AreEqual((double)o, doubleResult);
  556. }
  557. // Testing `operator *(Tensor x, Tensor y)
  558. c = tf.reduce_sum(tf.reduce_sum(a * b, 1));
  559. using (var sess = tf.Session())
  560. {
  561. var o = sess.run(c,
  562. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  563. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  564. Assert.AreEqual((double)o, doubleResult);
  565. }
  566. // Testing `operator *(Tensor x, double y)
  567. c = tf.reduce_sum(tf.reduce_sum(a * secondFloatVal, 1));
  568. using (var sess = tf.Session())
  569. {
  570. var o = sess.run(c,
  571. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  572. Assert.AreEqual((double)o, doubleResult);
  573. }
  574. // Testing `operator *(double x, Tensor y)
  575. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal * b, 1));
  576. using (var sess = tf.Session())
  577. {
  578. var o = sess.run(c,
  579. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  580. Assert.AreEqual((double) o, doubleResult);
  581. }
  582. #endregion
  583. }
  584. [TestMethod]
  585. public void divOpTests()
  586. {
  587. const int rows = 2; // to avoid broadcasting effect
  588. const int cols = 10;
  589. #region intTest
  590. const int firstIntVal = 6;
  591. const int secondIntVal = 3;
  592. var firstIntFeed = Enumerable.Repeat(firstIntVal, rows * cols).ToArray();
  593. var secondIntFeed = Enumerable.Repeat(secondIntVal, rows * cols).ToArray();
  594. var intResult = (int)(firstIntFeed.Sum() / (float)secondIntVal);
  595. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  596. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  597. var c = tf.reduce_sum(tf.reduce_sum(gen_math_ops.floor_div(a, b), 1));
  598. using (var sess = tf.Session())
  599. {
  600. var o = sess.run(c,
  601. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  602. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  603. Assert.AreEqual((int)o, intResult);
  604. }
  605. // Testing `operator /(Tensor x, Tensor y)
  606. c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
  607. using (var sess = tf.Session())
  608. {
  609. var o = sess.run(c,
  610. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  611. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  612. Assert.AreEqual((int)o, intResult);
  613. }
  614. // Testing `operator /(Tensor x, int y)
  615. c = tf.reduce_sum(tf.reduce_sum(a / secondIntVal, 1));
  616. using (var sess = tf.Session())
  617. {
  618. var o = sess.run(c,
  619. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  620. Assert.AreEqual((int)o, intResult);
  621. }
  622. // Testing `operator /(int x, Tensor y)
  623. c = tf.reduce_sum(tf.reduce_sum(firstIntVal / b, 1));
  624. using (var sess = tf.Session())
  625. {
  626. var o = sess.run(c,
  627. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  628. Assert.AreEqual((int)o, intResult);
  629. }
  630. #endregion
  631. #region floatTest
  632. const float firstFloatVal = 6.0f;
  633. const float secondFloatVal = 3.0f;
  634. var firstFloatFeed = Enumerable.Repeat(firstFloatVal, rows * cols).ToArray();
  635. var secondFloatFeed = Enumerable.Repeat(secondFloatVal, rows * cols).ToArray();
  636. var floatResult = MultiplyArray(firstFloatFeed, secondFloatFeed.Select(x => 1/x).ToArray()).Sum();
  637. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  638. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  639. c = tf.reduce_sum(tf.reduce_sum(tf.divide(a, b), 1));
  640. using (var sess = tf.Session())
  641. {
  642. var o = sess.run(c,
  643. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  644. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  645. Assert.AreEqual((float)o, floatResult);
  646. }
  647. // Testing `operator /(Tensor x, Tensor y)
  648. c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
  649. using (var sess = tf.Session())
  650. {
  651. var o = sess.run(c,
  652. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  653. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  654. Assert.AreEqual((float)o, floatResult);
  655. }
  656. // Testing `operator /(Tensor x, float y)
  657. c = tf.reduce_sum(tf.reduce_sum(a / secondFloatVal, 1));
  658. using (var sess = tf.Session())
  659. {
  660. var o = sess.run(c,
  661. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  662. Assert.AreEqual((float)o, floatResult);
  663. }
  664. // Testing `operator /(float x, Tensor y)
  665. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal / b, 1));
  666. using (var sess = tf.Session())
  667. {
  668. var o = sess.run(c,
  669. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  670. Assert.AreEqual((float)o, floatResult);
  671. }
  672. #endregion
  673. #region doubleTest
  674. const double firstDoubleVal = 6.0;
  675. const double secondDoubleVal = 3.0;
  676. var firstDoubleFeed = Enumerable.Repeat(firstDoubleVal, rows * cols).ToArray();
  677. var secondDoubleFeed = Enumerable.Repeat(secondDoubleVal, rows * cols).ToArray();
  678. var doubleResult = MultiplyArray(firstDoubleFeed, secondDoubleFeed.Select(x => 1/x).ToArray()).Sum();
  679. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  680. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  681. c = tf.reduce_sum(tf.reduce_sum(tf.divide(a, b), 1));
  682. using (var sess = tf.Session())
  683. {
  684. var o = sess.run(c,
  685. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  686. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  687. Assert.AreEqual((double)o, doubleResult);
  688. }
  689. // Testing `operator /(Tensor x, Tensor y)
  690. c = tf.reduce_sum(tf.reduce_sum(a / b, 1));
  691. using (var sess = tf.Session())
  692. {
  693. var o = sess.run(c,
  694. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  695. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  696. Assert.AreEqual((double)o, doubleResult);
  697. }
  698. // Testing `operator /(Tensor x, double y)
  699. c = tf.reduce_sum(tf.reduce_sum(a / secondFloatVal, 1));
  700. using (var sess = tf.Session())
  701. {
  702. var o = sess.run(c,
  703. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  704. Assert.AreEqual((double)o, doubleResult);
  705. }
  706. // Testing `operator /(double x, Tensor y)
  707. c = tf.reduce_sum(tf.reduce_sum(firstFloatVal / b, 1));
  708. using (var sess = tf.Session())
  709. {
  710. var o = sess.run(c,
  711. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  712. Assert.AreEqual((double)o, doubleResult);
  713. }
  714. #endregion
  715. }
  716. [TestMethod]
  717. public void greaterThanOpTests()
  718. {
  719. const int rows = 2; // to avoid broadcasting effect
  720. const int cols = 10;
  721. #region intTest
  722. const int intThreshold = 10;
  723. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  724. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  725. var intResult = firstIntFeed.Count(elem => elem > intThreshold);
  726. var intResultTwo = firstIntFeed.Count(elem => elem < intThreshold);
  727. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  728. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  729. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
  730. using (var sess = tf.Session())
  731. {
  732. var o = sess.run(c,
  733. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  734. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  735. Assert.AreEqual((int)o, intResult);
  736. }
  737. // Testing `operator >(Tensor x, Tensor y)
  738. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
  739. using (var sess = tf.Session())
  740. {
  741. var o = sess.run(c,
  742. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  743. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  744. Assert.AreEqual((int)o, intResult);
  745. }
  746. // Testing `operator >(Tensor x, int y)
  747. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > intThreshold, tf.int32), 1));
  748. using (var sess = tf.Session())
  749. {
  750. var o = sess.run(c,
  751. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  752. Assert.AreEqual((int)o, intResult);
  753. }
  754. // Testing `operator >(int x, Tensor y)
  755. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold > a, tf.int32), 1));
  756. using (var sess = tf.Session())
  757. {
  758. var o = sess.run(c,
  759. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  760. Assert.AreEqual((int)o, intResultTwo);
  761. }
  762. #endregion
  763. #region floatTest
  764. const float floatThreshold = 10.0f;
  765. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  766. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  767. var floatResult = firstFloatFeed.Count(elem => elem > floatThreshold);
  768. var floatResultTwo = firstFloatFeed.Count(elem => elem < floatThreshold);
  769. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  770. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  771. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
  772. using (var sess = tf.Session())
  773. {
  774. var o = sess.run(c,
  775. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  776. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  777. Assert.AreEqual((int)o, floatResult);
  778. }
  779. // Testing `operator >(Tensor x, Tensor y)
  780. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
  781. using (var sess = tf.Session())
  782. {
  783. var o = sess.run(c,
  784. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  785. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  786. Assert.AreEqual((int)o, floatResult);
  787. }
  788. // Testing `operator >(Tensor x, float y)
  789. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > floatThreshold, tf.int32), 1));
  790. using (var sess = tf.Session())
  791. {
  792. var o = sess.run(c,
  793. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  794. Assert.AreEqual((int)o, floatResult);
  795. }
  796. // Testing `operator >(float x, Tensor y)
  797. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold > a, tf.int32), 1));
  798. using (var sess = tf.Session())
  799. {
  800. var o = sess.run(c,
  801. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  802. Assert.AreEqual((int)o, floatResultTwo);
  803. }
  804. #endregion
  805. #region doubleTest
  806. const double doubleThreshold = 10.0;
  807. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  808. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  809. var doubleResult = firstDoubleFeed.Count(elem => elem > doubleThreshold);
  810. var doubleResultTwo = firstDoubleFeed.Count(elem => elem < doubleThreshold);
  811. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  812. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  813. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.greater(a, b), tf.int32), 1));
  814. using (var sess = tf.Session())
  815. {
  816. var o = sess.run(c,
  817. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  818. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  819. Assert.AreEqual((int)o, doubleResult);
  820. }
  821. // Testing `operator >(Tensor x, Tensor y)
  822. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > b, tf.int32), 1));
  823. using (var sess = tf.Session())
  824. {
  825. var o = sess.run(c,
  826. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  827. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  828. Assert.AreEqual((int)o, doubleResult);
  829. }
  830. // Testing `operator >(Tensor x, double y)
  831. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a > doubleThreshold, tf.int32), 1));
  832. using (var sess = tf.Session())
  833. {
  834. var o = sess.run(c,
  835. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  836. Assert.AreEqual((int)o, doubleResult);
  837. }
  838. // Testing `operator >(double x, Tensor y)
  839. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold > a, tf.int32), 1));
  840. using (var sess = tf.Session())
  841. {
  842. var o = sess.run(c,
  843. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  844. Assert.AreEqual((int)o, doubleResultTwo);
  845. }
  846. #endregion
  847. }
  848. [TestMethod]
  849. public void lessThanOpTests()
  850. {
  851. const int rows = 2; // to avoid broadcasting effect
  852. const int cols = 10;
  853. #region intTest
  854. const int intThreshold = 10;
  855. var firstIntFeed = Enumerable.Range(0, rows * cols).ToArray();
  856. var secondIntFeed = Enumerable.Repeat(intThreshold, rows * cols).ToArray();
  857. var intResult = firstIntFeed.Count(elem => elem < intThreshold);
  858. var intResultTwo = firstIntFeed.Count(elem => elem > intThreshold);
  859. var a = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  860. var b = tf.placeholder(tf.int32, shape: new TensorShape(rows, cols));
  861. var c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
  862. using (var sess = tf.Session())
  863. {
  864. var o = sess.run(c,
  865. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  866. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  867. Assert.AreEqual((int)o, intResult);
  868. }
  869. // Testing `operator <(Tensor x, Tensor y)
  870. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
  871. using (var sess = tf.Session())
  872. {
  873. var o = sess.run(c,
  874. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))),
  875. new FeedItem(b, new NDArray(secondIntFeed, new Shape(rows, cols))));
  876. Assert.AreEqual((int)o, intResult);
  877. }
  878. // Testing `operator <(Tensor x, int y)
  879. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < intThreshold, tf.int32), 1));
  880. using (var sess = tf.Session())
  881. {
  882. var o = sess.run(c,
  883. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  884. Assert.AreEqual((int)o, intResult);
  885. }
  886. // Testing `operator <(int x, Tensor y)
  887. c = tf.reduce_sum(tf.reduce_sum(tf.cast(intThreshold < a, tf.int32), 1));
  888. using (var sess = tf.Session())
  889. {
  890. var o = sess.run(c,
  891. new FeedItem(a, new NDArray(firstIntFeed, new Shape(rows, cols))));
  892. Assert.AreEqual((int)o, intResultTwo);
  893. }
  894. #endregion
  895. #region floatTest
  896. const float floatThreshold = 10.0f;
  897. var firstFloatFeed = Enumerable.Range(0, rows * cols).Select(elem => (float)elem).ToArray();
  898. var secondFloatFeed = Enumerable.Repeat(floatThreshold, rows * cols).ToArray();
  899. var floatResult = firstFloatFeed.Count(elem => elem < floatThreshold);
  900. var floatResultTwo = firstFloatFeed.Count(elem => elem > floatThreshold);
  901. a = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  902. b = tf.placeholder(tf.float32, shape: new TensorShape(rows, cols));
  903. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
  904. using (var sess = tf.Session())
  905. {
  906. var o = sess.run(c,
  907. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  908. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  909. Assert.AreEqual((int)o, floatResult);
  910. }
  911. // Testing `operator <(Tensor x, Tensor y)
  912. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
  913. using (var sess = tf.Session())
  914. {
  915. var o = sess.run(c,
  916. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))),
  917. new FeedItem(b, new NDArray(secondFloatFeed, new Shape(rows, cols))));
  918. Assert.AreEqual((int)o, floatResult);
  919. }
  920. // Testing `operator <(Tensor x, float y)
  921. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < floatThreshold, tf.int32), 1));
  922. using (var sess = tf.Session())
  923. {
  924. var o = sess.run(c,
  925. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  926. Assert.AreEqual((int)o, floatResult);
  927. }
  928. // Testing `operator <(float x, Tensor y)
  929. c = tf.reduce_sum(tf.reduce_sum(tf.cast(floatThreshold < a, tf.int32), 1));
  930. using (var sess = tf.Session())
  931. {
  932. var o = sess.run(c,
  933. new FeedItem(a, new NDArray(firstFloatFeed, new Shape(rows, cols))));
  934. Assert.AreEqual((int)o, floatResultTwo);
  935. }
  936. #endregion
  937. #region doubleTest
  938. const double doubleThreshold = 10.0;
  939. var firstDoubleFeed = Enumerable.Repeat(0, rows * cols).Select(elem => (double)elem).ToArray();
  940. var secondDoubleFeed = Enumerable.Repeat(doubleThreshold, rows * cols).ToArray();
  941. var doubleResult = firstDoubleFeed.Count(elem => elem < doubleThreshold);
  942. var doubleResultTwo = firstDoubleFeed.Count(elem => elem > doubleThreshold);
  943. a = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  944. b = tf.placeholder(tf.float64, shape: new TensorShape(rows, cols));
  945. c = tf.reduce_sum(tf.reduce_sum(tf.cast(tf.less(a, b), tf.int32), 1));
  946. using (var sess = tf.Session())
  947. {
  948. var o = sess.run(c,
  949. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  950. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  951. Assert.AreEqual((int)o, doubleResult);
  952. }
  953. // Testing `operator <(Tensor x, Tensor y)
  954. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < b, tf.int32), 1));
  955. using (var sess = tf.Session())
  956. {
  957. var o = sess.run(c,
  958. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))),
  959. new FeedItem(b, new NDArray(secondDoubleFeed, new Shape(rows, cols))));
  960. Assert.AreEqual((int)o, doubleResult);
  961. }
  962. // Testing `operator <(Tensor x, double y)
  963. c = tf.reduce_sum(tf.reduce_sum(tf.cast(a < doubleThreshold, tf.int32), 1));
  964. using (var sess = tf.Session())
  965. {
  966. var o = sess.run(c,
  967. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  968. Assert.AreEqual((int)o, doubleResult);
  969. }
  970. // Testing `operator <(double x, Tensor y)
  971. c = tf.reduce_sum(tf.reduce_sum(tf.cast(doubleThreshold < a, tf.int32), 1));
  972. using (var sess = tf.Session())
  973. {
  974. var o = sess.run(c,
  975. new FeedItem(a, new NDArray(firstDoubleFeed, new Shape(rows, cols))));
  976. Assert.AreEqual((int)o, doubleResultTwo);
  977. }
  978. #endregion
  979. }
  980. }
  981. }