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

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